Add explanatory note to 'expected item' error

This commit is contained in:
Gurinder Singh 2023-09-06 09:05:07 +05:30
parent 9dc11a13fa
commit 6a286e775c
14 changed files with 41 additions and 4 deletions

View File

@ -73,12 +73,16 @@ pub fn parse_mod(
if !self.maybe_consume_incorrect_semicolon(&items) { if !self.maybe_consume_incorrect_semicolon(&items) {
let msg = format!("expected item, found {token_str}"); let msg = format!("expected item, found {token_str}");
let mut err = self.struct_span_err(self.token.span, msg); let mut err = self.struct_span_err(self.token.span, msg);
let label = if self.is_kw_followed_by_ident(kw::Let) { let span = self.token.span;
"consider using `const` or `static` instead of `let` for global variables" if self.is_kw_followed_by_ident(kw::Let) {
err.span_label(
span,
"consider using `const` or `static` instead of `let` for global variables",
);
} else { } else {
"expected item" 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>");
}; };
err.span_label(self.token.span, label);
return Err(err); return Err(err);
} }
} }

View File

@ -11,6 +11,8 @@ error: expected item, found keyword `pub`
| |
LL | default pub const async unsafe extern fn err() {} LL | default pub const async unsafe extern fn err() {}
| ^^^ expected item | ^^^ expected item
|
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View File

@ -11,6 +11,8 @@ error: expected item, found reserved keyword `do`
| |
LL | default do LL | default do
| ^^ expected item | ^^ expected item
|
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View File

@ -35,6 +35,8 @@ error: expected item, found keyword `unsafe`
| |
LL | default unsafe FAIL LL | default unsafe FAIL
| ^^^^^^ expected item | ^^^^^^ expected item
|
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
error: aborting due to 6 previous errors error: aborting due to 6 previous errors

View File

@ -11,6 +11,8 @@ error: expected item, found `==`
| |
LL | B == 2 LL | B == 2
| ^^ expected item | ^^ expected item
|
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View File

@ -0,0 +1 @@
5 //~ ERROR expected item, found `5`

View File

@ -0,0 +1,10 @@
error: expected item, found `5`
--> $DIR/issue-113110-non-item-at-module-root.rs:1:2
|
LL | 5
| ^ expected item
|
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
error: aborting due to previous error

View File

@ -3,6 +3,8 @@ error: expected item, found keyword `where`
| |
LL | struct Bar<T> { x: T } where T: Copy LL | struct Bar<T> { x: T } where T: Copy
| ^^^^^ expected item | ^^^^^ expected item
|
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
error: aborting due to previous error error: aborting due to previous error

View File

@ -11,6 +11,8 @@ error: expected item, found `|`
| |
LL | | LL | |
| ^ expected item | ^ expected item
|
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View File

@ -17,6 +17,8 @@ error: expected item, found `"\u\"`
| |
LL | "\u\" LL | "\u\"
| ^^^^^^ expected item | ^^^^^^ expected item
|
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
error: aborting due to 3 previous errors error: aborting due to 3 previous errors

View File

@ -15,6 +15,8 @@ error: expected item, found `)`
| |
LL | enum e{A((?'a a+?+l))} LL | enum e{A((?'a a+?+l))}
| ^ expected item | ^ expected item
|
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
error: aborting due to 3 previous errors error: aborting due to 3 previous errors

View File

@ -3,6 +3,8 @@ error: expected item, found `[`
| |
LL | [allow(unused_variables)] LL | [allow(unused_variables)]
| ^ expected item | ^ expected item
|
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
error: aborting due to previous error error: aborting due to previous error

View File

@ -3,6 +3,8 @@ error: expected item, found reserved keyword `virtual`
| |
LL | virtual struct SuperStruct { LL | virtual struct SuperStruct {
| ^^^^^^^ expected item | ^^^^^^^ expected item
|
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
error: aborting due to previous error error: aborting due to previous error

View File

@ -11,6 +11,8 @@ error: expected item, found `(`
| |
LL | pub(crate) () fn foo() {} LL | pub(crate) () fn foo() {}
| ^ expected item | ^ expected item
|
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
error: aborting due to 2 previous errors error: aborting due to 2 previous errors