Skip to content

Commit

Permalink
Rollup merge of rust-lang#115473 - gurry:113110-expected-item, r=comp…
Browse files Browse the repository at this point in the history
…iler-errors

Add explanatory note to 'expected item' error

Fixes rust-lang#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
```
  • Loading branch information
fmease authored Sep 6, 2023
2 parents 6023808 + 6a286e7 commit b4ef338
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 4 deletions.
12 changes: 8 additions & 4 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,16 @@ impl<'a> Parser<'a> {
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);
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/fn/keyword-order.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 2 additions & 0 deletions tests/ui/parser/default-unmatched.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 2 additions & 0 deletions tests/ui/parser/impl-parsing.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 2 additions & 0 deletions tests/ui/parser/issue-101477-enum.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5 //~ ERROR expected item, found `5`
10 changes: 10 additions & 0 deletions tests/ui/parser/issues/issue-113110-non-item-at-module-root.stderr
Original file line number Diff line number Diff line change
@@ -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

2 changes: 2 additions & 0 deletions tests/ui/parser/issues/issue-17904-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 2 additions & 0 deletions tests/ui/parser/issues/issue-43196.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 2 additions & 0 deletions tests/ui/parser/issues/issue-62913.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 2 additions & 0 deletions tests/ui/parser/issues/issue-68890.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 2 additions & 0 deletions tests/ui/parser/shebang/shebang-doc-comment.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 2 additions & 0 deletions tests/ui/parser/virtual-structs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 2 additions & 0 deletions tests/ui/pub/pub-restricted-error-fn.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b4ef338

Please sign in to comment.