diff options
| author | bors <bors@rust-lang.org> | 2022-08-20 07:59:48 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-08-20 07:59:48 +0000 |
| commit | 36e530cb08950f1d03ab733e43ecec2802d099cf (patch) | |
| tree | e065b491027f2d360e8085f023daffcbe49c89a5 /compiler/rustc_parse/src | |
| parent | e1b28cd2f16bd5b832183d7968cae3bb9213e78d (diff) | |
| parent | 60edec9ddf9f59c6e5ec86456070440031a87943 (diff) | |
| download | rust-36e530cb08950f1d03ab733e43ecec2802d099cf.tar.gz rust-36e530cb08950f1d03ab733e43ecec2802d099cf.zip | |
Auto merge of #100793 - matthiaskrgr:rollup-dy7rfdh, r=matthiaskrgr
Rollup of 10 pull requests Successful merges: - #100186 (Mention `as_mut` alongside `as_ref` in borrowck error message) - #100383 (Mitigate stale data reads on SGX platform) - #100507 (suggest `once_cell::Lazy` for non-const statics) - #100617 (Suggest the right help message for as_ref) - #100667 (Migrate "invalid variable declaration" errors to SessionDiagnostic) - #100709 (Migrate typeck's `used` expected symbol diagnostic to `SessionDiagnostic`) - #100723 (Add the diagnostic translation lints to crates that don't emit them) - #100729 (Avoid zeroing a 1kb stack buffer on every call to `std::sys::windows::fill_utf16_buf`) - #100750 (improved diagnostic for function defined with `def`, `fun`, `func`, or `function` instead of `fn`) - #100763 (triagebot: Autolabel `A-rustdoc-json`) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_parse/src')
| -rw-r--r-- | compiler/rustc_parse/src/parser/diagnostics.rs | 38 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/stmt.rs | 31 |
2 files changed, 50 insertions, 19 deletions
diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 93e70e9abda..744ec7e6114 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -334,6 +334,35 @@ struct InInTypo { sugg_span: Span, } +#[derive(SessionDiagnostic)] +#[error(parser::invalid_variable_declaration)] +pub struct InvalidVariableDeclaration { + #[primary_span] + pub span: Span, + #[subdiagnostic] + pub sub: InvalidVariableDeclarationSub, +} + +#[derive(SessionSubdiagnostic)] +pub enum InvalidVariableDeclarationSub { + #[suggestion( + parser::switch_mut_let_order, + applicability = "maybe-incorrect", + code = "let mut" + )] + SwitchMutLetOrder(#[primary_span] Span), + #[suggestion( + parser::missing_let_before_mut, + applicability = "machine-applicable", + code = "let mut" + )] + MissingLet(#[primary_span] Span), + #[suggestion(parser::use_let_not_auto, applicability = "machine-applicable", code = "let")] + UseLetNotAuto(#[primary_span] Span), + #[suggestion(parser::use_let_not_var, applicability = "machine-applicable", code = "let")] + UseLetNotVar(#[primary_span] Span), +} + // SnapshotParser is used to create a snapshot of the parser // without causing duplicate errors being emitted when the `Parser` // is dropped. @@ -611,6 +640,15 @@ impl<'a> Parser<'a> { appl, ); } + + if ["def", "fun", "func", "function"].contains(&symbol.as_str()) { + err.span_suggestion_short( + self.prev_token.span, + &format!("write `fn` instead of `{symbol}` to declare a function"), + "fn", + appl, + ); + } } // Add suggestion for a missing closing angle bracket if '>' is included in expected_tokens diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index cac39f8f25f..875636bdc28 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -1,5 +1,7 @@ use super::attr::DEFAULT_INNER_ATTR_FORBIDDEN; -use super::diagnostics::{AttemptLocalParseRecovery, Error}; +use super::diagnostics::{ + AttemptLocalParseRecovery, Error, InvalidVariableDeclaration, InvalidVariableDeclarationSub, +}; use super::expr::LhsExpr; use super::pat::RecoverComma; use super::path::PathStyle; @@ -58,28 +60,22 @@ impl<'a> Parser<'a> { if self.token.is_keyword(kw::Mut) && self.is_keyword_ahead(1, &[kw::Let]) { self.bump(); let mut_let_span = lo.to(self.token.span); - self.struct_span_err(mut_let_span, "invalid variable declaration") - .span_suggestion( - mut_let_span, - "switch the order of `mut` and `let`", - "let mut", - Applicability::MaybeIncorrect, - ) - .emit(); + self.sess.emit_err(InvalidVariableDeclaration { + span: mut_let_span, + sub: InvalidVariableDeclarationSub::SwitchMutLetOrder(mut_let_span), + }); } Ok(Some(if self.token.is_keyword(kw::Let) { self.parse_local_mk(lo, attrs, capture_semi, force_collect)? } else if self.is_kw_followed_by_ident(kw::Mut) { - self.recover_stmt_local(lo, attrs, "missing keyword", "let mut")? + self.recover_stmt_local(lo, attrs, InvalidVariableDeclarationSub::MissingLet)? } else if self.is_kw_followed_by_ident(kw::Auto) { self.bump(); // `auto` - let msg = "write `let` instead of `auto` to introduce a new variable"; - self.recover_stmt_local(lo, attrs, msg, "let")? + self.recover_stmt_local(lo, attrs, InvalidVariableDeclarationSub::UseLetNotAuto)? } else if self.is_kw_followed_by_ident(sym::var) { self.bump(); // `var` - let msg = "write `let` instead of `var` to introduce a new variable"; - self.recover_stmt_local(lo, attrs, msg, "let")? + self.recover_stmt_local(lo, attrs, InvalidVariableDeclarationSub::UseLetNotVar)? } else if self.check_path() && !self.token.is_qpath_start() && !self.is_path_start_item() { // We have avoided contextual keywords like `union`, items with `crate` visibility, // or `auto trait` items. We aim to parse an arbitrary path `a::b` but not something @@ -217,13 +213,10 @@ impl<'a> Parser<'a> { &mut self, lo: Span, attrs: AttrWrapper, - msg: &str, - sugg: &str, + subdiagnostic: fn(Span) -> InvalidVariableDeclarationSub, ) -> PResult<'a, Stmt> { let stmt = self.recover_local_after_let(lo, attrs)?; - self.struct_span_err(lo, "invalid variable declaration") - .span_suggestion(lo, msg, sugg, Applicability::MachineApplicable) - .emit(); + self.sess.emit_err(InvalidVariableDeclaration { span: lo, sub: subdiagnostic(lo) }); Ok(stmt) } |
