Skip to content

Commit 506ea79

Browse files
committed
Move two rustc_parse error diagnostics.
Every diagnostic struct in `rustc_parse` is in the `errors` module, except for `ForbiddenLetReason` and `MisspelledKw`. There's no good reason for this, and presumably it is just an accidental inconsistency. This commit moves them into `errors`.
1 parent 2135bc1 commit 506ea79

4 files changed

Lines changed: 50 additions & 50 deletions

File tree

compiler/rustc_parse/src/errors.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_span::edition::{Edition, LATEST_STABLE_EDITION};
1717
use rustc_span::{Ident, Span, Symbol};
1818

1919
use crate::fluent_generated as fluent;
20-
use crate::parser::{ForbiddenLetReason, TokenDescription};
20+
use crate::parser::TokenDescription;
2121

2222
#[derive(Diagnostic)]
2323
#[diag(parse_maybe_report_ambiguous_plus)]
@@ -3703,3 +3703,35 @@ pub(crate) struct StructLiteralWithoutPathLate {
37033703
#[suggestion(applicability = "has-placeholders", code = "/* Type */ ", style = "verbose")]
37043704
pub suggestion_span: Span,
37053705
}
3706+
3707+
/// Used to forbid `let` expressions in certain syntactic locations.
3708+
#[derive(Clone, Copy, Subdiagnostic)]
3709+
pub(crate) enum ForbiddenLetReason {
3710+
/// `let` is not valid and the source environment is not important
3711+
OtherForbidden,
3712+
/// A let chain with the `||` operator
3713+
#[note(parse_not_supported_or)]
3714+
NotSupportedOr(#[primary_span] Span),
3715+
/// A let chain with invalid parentheses
3716+
///
3717+
/// For example, `let 1 = 1 && (expr && expr)` is allowed
3718+
/// but `(let 1 = 1 && (let 1 = 1 && (let 1 = 1))) && let a = 1` is not
3719+
#[note(parse_not_supported_parentheses)]
3720+
NotSupportedParentheses(#[primary_span] Span),
3721+
}
3722+
3723+
#[derive(Debug, rustc_macros::Subdiagnostic)]
3724+
#[suggestion(
3725+
parse_misspelled_kw,
3726+
applicability = "machine-applicable",
3727+
code = "{similar_kw}",
3728+
style = "verbose"
3729+
)]
3730+
pub(crate) struct MisspelledKw {
3731+
// We use a String here because `Symbol::into_diag_arg` calls `Symbol::to_ident_string`, which
3732+
// prefix the keyword with a `r#` because it aims to print the symbol as an identifier.
3733+
pub similar_kw: String,
3734+
#[primary_span]
3735+
pub span: Span,
3736+
pub is_incorrect_case: bool,
3737+
}

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ use crate::errors::{
3535
ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg, GenericParamsWithoutAngleBrackets,
3636
GenericParamsWithoutAngleBracketsSugg, HelpIdentifierStartsWithNumber, HelpUseLatestEdition,
3737
InInTypo, IncorrectAwait, IncorrectSemicolon, IncorrectUseOfAwait, IncorrectUseOfUse,
38-
PatternMethodParamWithoutBody, QuestionMarkInType, QuestionMarkInTypeSugg, SelfParamNotFirst,
39-
StructLiteralBodyWithoutPath, StructLiteralBodyWithoutPathSugg, SuggAddMissingLetStmt,
40-
SuggEscapeIdentifier, SuggRemoveComma, TernaryOperator, TernaryOperatorSuggestion,
41-
UnexpectedConstInGenericParam, UnexpectedConstParamDeclaration,
38+
MisspelledKw, PatternMethodParamWithoutBody, QuestionMarkInType, QuestionMarkInTypeSugg,
39+
SelfParamNotFirst, StructLiteralBodyWithoutPath, StructLiteralBodyWithoutPathSugg,
40+
SuggAddMissingLetStmt, SuggEscapeIdentifier, SuggRemoveComma, TernaryOperator,
41+
TernaryOperatorSuggestion, UnexpectedConstInGenericParam, UnexpectedConstParamDeclaration,
4242
UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets, UseEqInstead, WrapType,
4343
};
4444
use crate::parser::FnContext;
@@ -212,22 +212,6 @@ impl std::fmt::Display for UnaryFixity {
212212
}
213213
}
214214

215-
#[derive(Debug, rustc_macros::Subdiagnostic)]
216-
#[suggestion(
217-
parse_misspelled_kw,
218-
applicability = "machine-applicable",
219-
code = "{similar_kw}",
220-
style = "verbose"
221-
)]
222-
struct MisspelledKw {
223-
// We use a String here because `Symbol::into_diag_arg` calls `Symbol::to_ident_string`, which
224-
// prefix the keyword with a `r#` because it aims to print the symbol as an identifier.
225-
similar_kw: String,
226-
#[primary_span]
227-
span: Span,
228-
is_incorrect_case: bool,
229-
}
230-
231215
/// Checks if the given `lookup` identifier is similar to any keyword symbol in `candidates`.
232216
///
233217
/// This is a specialized version of [`Symbol::find_similar`] that constructs an error when a

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use rustc_ast::{
2121
use rustc_data_structures::stack::ensure_sufficient_stack;
2222
use rustc_errors::{Applicability, Diag, PResult, StashKey, Subdiagnostic};
2323
use rustc_literal_escaper::unescape_char;
24-
use rustc_macros::Subdiagnostic;
2524
use rustc_session::errors::{ExprParenthesesNeeded, report_lit_error};
2625
use rustc_session::lint::BuiltinLintDiag;
2726
use rustc_session::lint::builtin::BREAK_WITH_LABEL_AND_LOOP;
@@ -2770,7 +2769,7 @@ impl<'a> Parser<'a> {
27702769
let recovered = if !restrictions.contains(Restrictions::ALLOW_LET) {
27712770
let err = errors::ExpectedExpressionFoundLet {
27722771
span: self.token.span,
2773-
reason: ForbiddenLetReason::OtherForbidden,
2772+
reason: errors::ForbiddenLetReason::OtherForbidden,
27742773
missing_let: None,
27752774
comparison: None,
27762775
};
@@ -4169,22 +4168,6 @@ pub(crate) fn could_be_unclosed_char_literal(ident: Ident) -> bool {
41694168
&& unescape_char(ident.without_first_quote().name.as_str()).is_ok()
41704169
}
41714170

4172-
/// Used to forbid `let` expressions in certain syntactic locations.
4173-
#[derive(Clone, Copy, Subdiagnostic)]
4174-
pub(crate) enum ForbiddenLetReason {
4175-
/// `let` is not valid and the source environment is not important
4176-
OtherForbidden,
4177-
/// A let chain with the `||` operator
4178-
#[note(parse_not_supported_or)]
4179-
NotSupportedOr(#[primary_span] Span),
4180-
/// A let chain with invalid parentheses
4181-
///
4182-
/// For example, `let 1 = 1 && (expr && expr)` is allowed
4183-
/// but `(let 1 = 1 && (let 1 = 1 && (let 1 = 1))) && let a = 1` is not
4184-
#[note(parse_not_supported_parentheses)]
4185-
NotSupportedParentheses(#[primary_span] Span),
4186-
}
4187-
41884171
/// Whether let chains are allowed on all editions, or it's edition dependent (allowed only on
41894172
/// 2024 and later). In case of edition dependence, specify the currently present edition.
41904173
pub enum LetChainsPolicy {
@@ -4205,7 +4188,7 @@ struct CondChecker<'a> {
42054188
parser: &'a Parser<'a>,
42064189
let_chains_policy: LetChainsPolicy,
42074190
depth: u32,
4208-
forbid_let_reason: Option<ForbiddenLetReason>,
4191+
forbid_let_reason: Option<errors::ForbiddenLetReason>,
42094192
missing_let: Option<errors::MaybeMissingLet>,
42104193
comparison: Option<errors::MaybeComparison>,
42114194
}
@@ -4226,14 +4209,13 @@ impl<'a> CondChecker<'a> {
42264209
impl MutVisitor for CondChecker<'_> {
42274210
fn visit_expr(&mut self, e: &mut Expr) {
42284211
self.depth += 1;
4229-
use ForbiddenLetReason::*;
42304212

42314213
let span = e.span;
42324214
match e.kind {
42334215
ExprKind::Let(_, _, _, ref mut recovered @ Recovered::No) => {
42344216
if let Some(reason) = self.forbid_let_reason {
42354217
let error = match reason {
4236-
NotSupportedOr(or_span) => {
4218+
errors::ForbiddenLetReason::NotSupportedOr(or_span) => {
42374219
self.parser.dcx().emit_err(errors::OrInLetChain { span: or_span })
42384220
}
42394221
_ => self.parser.dcx().emit_err(errors::ExpectedExpressionFoundLet {
@@ -4260,24 +4242,27 @@ impl MutVisitor for CondChecker<'_> {
42604242
mut_visit::walk_expr(self, e);
42614243
}
42624244
ExprKind::Binary(Spanned { node: BinOpKind::Or, span: or_span }, _, _)
4263-
if let None | Some(NotSupportedOr(_)) = self.forbid_let_reason =>
4245+
if let None | Some(errors::ForbiddenLetReason::NotSupportedOr(_)) =
4246+
self.forbid_let_reason =>
42644247
{
42654248
let forbid_let_reason = self.forbid_let_reason;
4266-
self.forbid_let_reason = Some(NotSupportedOr(or_span));
4249+
self.forbid_let_reason = Some(errors::ForbiddenLetReason::NotSupportedOr(or_span));
42674250
mut_visit::walk_expr(self, e);
42684251
self.forbid_let_reason = forbid_let_reason;
42694252
}
42704253
ExprKind::Paren(ref inner)
4271-
if let None | Some(NotSupportedParentheses(_)) = self.forbid_let_reason =>
4254+
if let None | Some(errors::ForbiddenLetReason::NotSupportedParentheses(_)) =
4255+
self.forbid_let_reason =>
42724256
{
42734257
let forbid_let_reason = self.forbid_let_reason;
4274-
self.forbid_let_reason = Some(NotSupportedParentheses(inner.span));
4258+
self.forbid_let_reason =
4259+
Some(errors::ForbiddenLetReason::NotSupportedParentheses(inner.span));
42754260
mut_visit::walk_expr(self, e);
42764261
self.forbid_let_reason = forbid_let_reason;
42774262
}
42784263
ExprKind::Assign(ref lhs, _, span) => {
42794264
let forbid_let_reason = self.forbid_let_reason;
4280-
self.forbid_let_reason = Some(OtherForbidden);
4265+
self.forbid_let_reason = Some(errors::ForbiddenLetReason::OtherForbidden);
42814266
let missing_let = self.missing_let;
42824267
if let ExprKind::Binary(_, _, rhs) = &lhs.kind
42834268
&& let ExprKind::Path(_, _)
@@ -4310,15 +4295,15 @@ impl MutVisitor for CondChecker<'_> {
43104295
| ExprKind::Tup(_)
43114296
| ExprKind::Paren(_) => {
43124297
let forbid_let_reason = self.forbid_let_reason;
4313-
self.forbid_let_reason = Some(OtherForbidden);
4298+
self.forbid_let_reason = Some(errors::ForbiddenLetReason::OtherForbidden);
43144299
mut_visit::walk_expr(self, e);
43154300
self.forbid_let_reason = forbid_let_reason;
43164301
}
43174302
ExprKind::Cast(ref mut op, _)
43184303
| ExprKind::Type(ref mut op, _)
43194304
| ExprKind::UnsafeBinderCast(_, ref mut op, _) => {
43204305
let forbid_let_reason = self.forbid_let_reason;
4321-
self.forbid_let_reason = Some(OtherForbidden);
4306+
self.forbid_let_reason = Some(errors::ForbiddenLetReason::OtherForbidden);
43224307
self.visit_expr(op);
43234308
self.forbid_let_reason = forbid_let_reason;
43244309
}

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use std::{fmt, mem, slice};
2020

2121
use attr_wrapper::{AttrWrapper, UsePreAttrPos};
2222
pub use diagnostics::AttemptLocalParseRecovery;
23-
pub(crate) use expr::ForbiddenLetReason;
2423
// Public to use it for custom `if` expressions in rustfmt forks like https://github.com/tucant/rustfmt
2524
pub use expr::LetChainsPolicy;
2625
pub(crate) use item::{FnContext, FnParseMode};

0 commit comments

Comments
 (0)