about summary refs log tree commit diff
path: root/compiler/rustc_parse/src/parser
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-11-18 04:17:11 +0000
committerbors <bors@rust-lang.org>2024-11-18 04:17:11 +0000
commitbf6adec108e83c5ddfcbb443a9177203db5eb945 (patch)
tree7dad50a965a6cd5b4f22936421ade7a3083a7107 /compiler/rustc_parse/src/parser
parent3fb7e441aecc3c054d71eb4d752d06e7776e8888 (diff)
parentf6374b4b7135479ccfe6751f1ed45ba5a95910f0 (diff)
downloadrust-bf6adec108e83c5ddfcbb443a9177203db5eb945.tar.gz
rust-bf6adec108e83c5ddfcbb443a9177203db5eb945.zip
Auto merge of #133152 - jhpratt:rollup-wkqs5ud, r=jhpratt
Rollup of 7 pull requests

Successful merges:

 - #132795 (Check `use<..>` in RPITIT for refinement)
 - #132944 (add parentheses when unboxing suggestion needed)
 - #132993 (Make rustc consider itself a stable compiler when `RUSTC_BOOTSTRAP=-1`)
 - #133130 (`suggest_borrow_generic_arg`: instantiate clauses properly)
 - #133133 (rustdoc-search: add standalone trailing `::` test)
 - #133143 (Diagnostics for let mut in item context)
 - #133147 (Fixup some test directives)

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_parse/src/parser')
-rw-r--r--compiler/rustc_parse/src/parser/item.rs33
1 files changed, 25 insertions, 8 deletions
diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs
index 6b4e2d0f4e2..fddbf5896ad 100644
--- a/compiler/rustc_parse/src/parser/item.rs
+++ b/compiler/rustc_parse/src/parser/item.rs
@@ -77,18 +77,35 @@ impl<'a> Parser<'a> {
         if !self.eat(term) {
             let token_str = super::token_descr(&self.token);
             if !self.maybe_consume_incorrect_semicolon(items.last().map(|x| &**x)) {
+                let is_let = self.token.is_keyword(kw::Let);
+                let is_let_mut = is_let && self.look_ahead(1, |t| t.is_keyword(kw::Mut));
+                let let_has_ident = is_let && !is_let_mut && self.is_kw_followed_by_ident(kw::Let);
+
                 let msg = format!("expected item, found {token_str}");
                 let mut err = self.dcx().struct_span_err(self.token.span, msg);
-                let span = self.token.span;
-                if self.is_kw_followed_by_ident(kw::Let) {
-                    err.span_label(
-                        span,
-                        "consider using `const` or `static` instead of `let` for global variables",
-                    );
+
+                let label = if is_let {
+                    "`let` cannot be used for global variables"
                 } else {
-                    err.span_label(span, "expected item")
-                        .note("for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>");
+                    "expected item"
                 };
+                err.span_label(self.token.span, label);
+
+                if is_let {
+                    if is_let_mut {
+                        err.help("consider using `static` and a `Mutex` instead of `let mut`");
+                    } else if let_has_ident {
+                        err.span_suggestion_short(
+                            self.token.span,
+                            "consider using `static` or `const` instead of `let`",
+                            "static",
+                            Applicability::MaybeIncorrect,
+                        );
+                    } else {
+                        err.help("consider using `static` or `const` instead of `let`");
+                    }
+                }
+                err.note("for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>");
                 return Err(err);
             }
         }