Skip to content

Commit e9fbfa2

Browse files
authored
Rollup merge of rust-lang#100396 - chenyukang:fix-100394, r=petrochenkov
Suggest const and static for global variable Fixing rust-lang#100394
2 parents aa2b716 + 98518c2 commit e9fbfa2

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

compiler/rustc_parse/src/parser/item.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,12 @@ impl<'a> Parser<'a> {
6868
if !self.maybe_consume_incorrect_semicolon(&items) {
6969
let msg = &format!("expected item, found {token_str}");
7070
let mut err = self.struct_span_err(self.token.span, msg);
71-
err.span_label(self.token.span, "expected item");
71+
let label = if self.is_kw_followed_by_ident(kw::Let) {
72+
"consider using `const` or `static` instead of `let` for global variables"
73+
} else {
74+
"expected item"
75+
};
76+
err.span_label(self.token.span, label);
7277
return Err(err);
7378
}
7479
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
let X: i32 = 12;
2+
//~^ ERROR expected item, found keyword `let`
3+
4+
fn main() {
5+
println!("{}", X);
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: expected item, found keyword `let`
2+
--> $DIR/suggest-const-for-global-var.rs:1:1
3+
|
4+
LL | let X: i32 = 12;
5+
| ^^^ consider using `const` or `static` instead of `let` for global variables
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)