diff options
| author | finalchild <finalchild2@gmail.com> | 2022-08-17 23:51:01 +0900 |
|---|---|---|
| committer | finalchild <finalchild2@gmail.com> | 2022-08-22 00:57:21 +0900 |
| commit | d6fdf14eb783969b4e20e91d39a97bb56b50f1c6 (patch) | |
| tree | 34ad565b37e242e77e2de2c1de3fcc950756a209 /compiler/rustc_ast_passes/src | |
| parent | 4b695f7c4e1a02d160fe7e159abd0f87027c0fcf (diff) | |
| download | rust-d6fdf14eb783969b4e20e91d39a97bb56b50f1c6.tar.gz rust-d6fdf14eb783969b4e20e91d39a97bb56b50f1c6.zip | |
Migrate forbidden_let
Diffstat (limited to 'compiler/rustc_ast_passes/src')
| -rw-r--r-- | compiler/rustc_ast_passes/src/ast_validation.rs | 22 | ||||
| -rw-r--r-- | compiler/rustc_ast_passes/src/errors.rs | 32 | ||||
| -rw-r--r-- | compiler/rustc_ast_passes/src/lib.rs | 1 |
3 files changed, 37 insertions, 18 deletions
diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index e61dfef7bd3..f8eca6a9337 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -27,6 +27,8 @@ use rustc_target::spec::abi; use std::mem; use std::ops::{Deref, DerefMut}; +use crate::errors::ForbiddenLet; + const MORE_EXTERN: &str = "for more information, visit https://doc.rust-lang.org/std/keyword.extern.html"; @@ -117,23 +119,7 @@ impl<'a> AstValidator<'a> { /// Emits an error banning the `let` expression provided in the given location. fn ban_let_expr(&self, expr: &'a Expr, forbidden_let_reason: ForbiddenLetReason) { - let err = "`let` expressions are not supported here"; - let mut diag = self.session.struct_span_err(expr.span, err); - diag.note("only supported directly in conditions of `if` and `while` expressions"); - match forbidden_let_reason { - ForbiddenLetReason::GenericForbidden => {} - ForbiddenLetReason::NotSupportedOr(span) => { - diag.span_note(span, "`||` operators are not supported in let chain expressions"); - } - ForbiddenLetReason::NotSupportedParentheses(span) => { - diag.span_note( - span, - "`let`s wrapped in parentheses are not supported in a context with let \ - chains", - ); - } - } - diag.emit(); + self.session.emit_err(ForbiddenLet { span: expr.span, reason: forbidden_let_reason }); } fn check_gat_where( @@ -1876,7 +1862,7 @@ pub fn check_crate(session: &Session, krate: &Crate, lints: &mut LintBuffer) -> /// Used to forbid `let` expressions in certain syntactic locations. #[derive(Clone, Copy)] -enum ForbiddenLetReason { +pub(crate) enum ForbiddenLetReason { /// `let` is not valid and the source environment is not important GenericForbidden, /// A let chain with the `||` operator diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs new file mode 100644 index 00000000000..397acd3b609 --- /dev/null +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -0,0 +1,32 @@ +//! Errors emitted by ast_passes. + +use rustc_errors::fluent; +use rustc_errors::{AddSubdiagnostic, Diagnostic}; +use rustc_macros::SessionDiagnostic; +use rustc_span::Span; + +use crate::ast_validation::ForbiddenLetReason; + +#[derive(SessionDiagnostic)] +#[error(ast_passes::forbidden_let)] +#[note] +pub struct ForbiddenLet { + #[primary_span] + pub span: Span, + #[subdiagnostic] + pub(crate) reason: ForbiddenLetReason, +} + +impl AddSubdiagnostic for ForbiddenLetReason { + fn add_to_diagnostic(self, diag: &mut Diagnostic) { + match self { + Self::GenericForbidden => {} + Self::NotSupportedOr(span) => { + diag.span_note(span, fluent::ast_passes::not_supported_or); + } + Self::NotSupportedParentheses(span) => { + diag.span_note(span, fluent::ast_passes::not_supported_parentheses); + }, + } + } +} diff --git a/compiler/rustc_ast_passes/src/lib.rs b/compiler/rustc_ast_passes/src/lib.rs index 2bc3b6f3616..6a826298985 100644 --- a/compiler/rustc_ast_passes/src/lib.rs +++ b/compiler/rustc_ast_passes/src/lib.rs @@ -12,6 +12,7 @@ #![recursion_limit = "256"] pub mod ast_validation; +mod errors; pub mod feature_gate; pub mod node_count; pub mod show_span; |
