Skip to content

Commit d6fdf14

Browse files
committed
Migrate forbidden_let
1 parent 4b695f7 commit d6fdf14

File tree

7 files changed

+45
-18
lines changed

7 files changed

+45
-18
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3595,6 +3595,7 @@ dependencies = [
35953595
"rustc_data_structures",
35963596
"rustc_errors",
35973597
"rustc_feature",
3598+
"rustc_macros",
35983599
"rustc_parse",
35993600
"rustc_session",
36003601
"rustc_span",

compiler/rustc_ast_passes/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ rustc_attr = { path = "../rustc_attr" }
1111
rustc_data_structures = { path = "../rustc_data_structures" }
1212
rustc_errors = { path = "../rustc_errors" }
1313
rustc_feature = { path = "../rustc_feature" }
14+
rustc_macros = { path = "../rustc_macros" }
1415
rustc_parse = { path = "../rustc_parse" }
1516
rustc_session = { path = "../rustc_session" }
1617
rustc_span = { path = "../rustc_span" }

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ use rustc_target::spec::abi;
2727
use std::mem;
2828
use std::ops::{Deref, DerefMut};
2929

30+
use crate::errors::ForbiddenLet;
31+
3032
const MORE_EXTERN: &str =
3133
"for more information, visit https://doc.rust-lang.org/std/keyword.extern.html";
3234

@@ -117,23 +119,7 @@ impl<'a> AstValidator<'a> {
117119

118120
/// Emits an error banning the `let` expression provided in the given location.
119121
fn ban_let_expr(&self, expr: &'a Expr, forbidden_let_reason: ForbiddenLetReason) {
120-
let err = "`let` expressions are not supported here";
121-
let mut diag = self.session.struct_span_err(expr.span, err);
122-
diag.note("only supported directly in conditions of `if` and `while` expressions");
123-
match forbidden_let_reason {
124-
ForbiddenLetReason::GenericForbidden => {}
125-
ForbiddenLetReason::NotSupportedOr(span) => {
126-
diag.span_note(span, "`||` operators are not supported in let chain expressions");
127-
}
128-
ForbiddenLetReason::NotSupportedParentheses(span) => {
129-
diag.span_note(
130-
span,
131-
"`let`s wrapped in parentheses are not supported in a context with let \
132-
chains",
133-
);
134-
}
135-
}
136-
diag.emit();
122+
self.session.emit_err(ForbiddenLet { span: expr.span, reason: forbidden_let_reason });
137123
}
138124

139125
fn check_gat_where(
@@ -1876,7 +1862,7 @@ pub fn check_crate(session: &Session, krate: &Crate, lints: &mut LintBuffer) ->
18761862

18771863
/// Used to forbid `let` expressions in certain syntactic locations.
18781864
#[derive(Clone, Copy)]
1879-
enum ForbiddenLetReason {
1865+
pub(crate) enum ForbiddenLetReason {
18801866
/// `let` is not valid and the source environment is not important
18811867
GenericForbidden,
18821868
/// A let chain with the `||` operator
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//! Errors emitted by ast_passes.
2+
3+
use rustc_errors::fluent;
4+
use rustc_errors::{AddSubdiagnostic, Diagnostic};
5+
use rustc_macros::SessionDiagnostic;
6+
use rustc_span::Span;
7+
8+
use crate::ast_validation::ForbiddenLetReason;
9+
10+
#[derive(SessionDiagnostic)]
11+
#[error(ast_passes::forbidden_let)]
12+
#[note]
13+
pub struct ForbiddenLet {
14+
#[primary_span]
15+
pub span: Span,
16+
#[subdiagnostic]
17+
pub(crate) reason: ForbiddenLetReason,
18+
}
19+
20+
impl AddSubdiagnostic for ForbiddenLetReason {
21+
fn add_to_diagnostic(self, diag: &mut Diagnostic) {
22+
match self {
23+
Self::GenericForbidden => {}
24+
Self::NotSupportedOr(span) => {
25+
diag.span_note(span, fluent::ast_passes::not_supported_or);
26+
}
27+
Self::NotSupportedParentheses(span) => {
28+
diag.span_note(span, fluent::ast_passes::not_supported_parentheses);
29+
},
30+
}
31+
}
32+
}

compiler/rustc_ast_passes/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#![recursion_limit = "256"]
1313

1414
pub mod ast_validation;
15+
mod errors;
1516
pub mod feature_gate;
1617
pub mod node_count;
1718
pub mod show_span;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ast_passes_forbidden_let =
2+
`let` expressions are not supported here
3+
.note = only supported directly in conditions of `if` and `while` expressions
4+
.not_supported_or = `||` operators are not supported in let chain expressions
5+
.not_supported_parentheses = `let`s wrapped in parentheses are not supported in a context with let chains

compiler/rustc_error_messages/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub use unic_langid::{langid, LanguageIdentifier};
3232

3333
// Generates `DEFAULT_LOCALE_RESOURCES` static and `fluent_generated` module.
3434
fluent_messages! {
35+
ast_passes => "../locales/en-US/ast_passes.ftl",
3536
borrowck => "../locales/en-US/borrowck.ftl",
3637
builtin_macros => "../locales/en-US/builtin_macros.ftl",
3738
const_eval => "../locales/en-US/const_eval.ftl",

0 commit comments

Comments
 (0)