Rollup merge of #115473 - gurry:113110-expected-item, r=compiler-errors

Add explanatory note to 'expected item' error

Fixes #113110

It changes the diagnostic from this:

```
error: expected item, found `5`
 --> ../test.rs:1:1
  |
1 | 5
  | ^ expected item
 ```
to this:
```
error: expected item, found `5`
 --> ../test.rs:1:1
  |
1 | 5
  | ^ expected item
  |
  = note: items are things that can appear at the root of a module
  = note: for a full list see https://doc.rust-lang.org/reference/items.html
```
This commit is contained in:
Matthias Krüger 2023-09-06 19:31:48 +02:00 committed by GitHub
commit 4a31cc859b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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) {
let msg = format!("expected item, found {token_str}");
let mut err = self.struct_span_err(self.token.span, msg);
let label = if self.is_kw_followed_by_ident(kw::Let) {
"consider using `const` or `static` instead of `let` for global variables"
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",
);
} 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);
}
}

View File

@ -11,6 +11,8 @@ error: expected item, found keyword `pub`
|
LL | default pub const async unsafe extern fn err() {}
| ^^^ 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

View File

@ -11,6 +11,8 @@ error: expected item, found reserved keyword `do`
|
LL | default do
| ^^ 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

View File

@ -35,6 +35,8 @@ error: expected item, found keyword `unsafe`
|
LL | default unsafe FAIL
| ^^^^^^ 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

View File

@ -11,6 +11,8 @@ error: expected item, found `==`
|
LL | B == 2
| ^^ 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

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
| ^^^^^ 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

@ -11,6 +11,8 @@ error: expected item, found `|`
|
LL | |
| ^ 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

View File

@ -17,6 +17,8 @@ error: expected item, found `"\u\"`
|
LL | "\u\"
| ^^^^^^ 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

View File

@ -15,6 +15,8 @@ error: expected item, found `)`
|
LL | enum e{A((?'a a+?+l))}
| ^ 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

View File

@ -3,6 +3,8 @@ error: expected item, found `[`
|
LL | [allow(unused_variables)]
| ^ 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 reserved keyword `virtual`
|
LL | virtual struct SuperStruct {
| ^^^^^^^ 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

@ -11,6 +11,8 @@ error: expected item, found `(`
|
LL | pub(crate) () fn foo() {}
| ^ 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