Skip to content

Commit b4ef338

Browse files
authored
Rollup merge of rust-lang#115473 - gurry:113110-expected-item, r=compiler-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 ```
2 parents 6023808 + 6a286e7 commit b4ef338

14 files changed

+41
-4
lines changed

Diff for: compiler/rustc_parse/src/parser/item.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,16 @@ impl<'a> Parser<'a> {
7373
if !self.maybe_consume_incorrect_semicolon(&items) {
7474
let msg = format!("expected item, found {token_str}");
7575
let mut err = self.struct_span_err(self.token.span, msg);
76-
let label = if self.is_kw_followed_by_ident(kw::Let) {
77-
"consider using `const` or `static` instead of `let` for global variables"
76+
let span = self.token.span;
77+
if self.is_kw_followed_by_ident(kw::Let) {
78+
err.span_label(
79+
span,
80+
"consider using `const` or `static` instead of `let` for global variables",
81+
);
7882
} else {
79-
"expected item"
83+
err.span_label(span, "expected item")
84+
.note("for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>");
8085
};
81-
err.span_label(self.token.span, label);
8286
return Err(err);
8387
}
8488
}

Diff for: tests/ui/fn/keyword-order.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ error: expected item, found keyword `pub`
1111
|
1212
LL | default pub const async unsafe extern fn err() {}
1313
| ^^^ expected item
14+
|
15+
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
1416

1517
error: aborting due to 2 previous errors
1618

Diff for: tests/ui/parser/default-unmatched.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ error: expected item, found reserved keyword `do`
1111
|
1212
LL | default do
1313
| ^^ expected item
14+
|
15+
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
1416

1517
error: aborting due to 2 previous errors
1618

Diff for: tests/ui/parser/impl-parsing.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ error: expected item, found keyword `unsafe`
3535
|
3636
LL | default unsafe FAIL
3737
| ^^^^^^ expected item
38+
|
39+
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
3840

3941
error: aborting due to 6 previous errors
4042

Diff for: tests/ui/parser/issue-101477-enum.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ error: expected item, found `==`
1111
|
1212
LL | B == 2
1313
| ^^ expected item
14+
|
15+
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
1416

1517
error: aborting due to 2 previous errors
1618

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

Diff for: tests/ui/parser/issues/issue-17904-2.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error: expected item, found keyword `where`
33
|
44
LL | struct Bar<T> { x: T } where T: Copy
55
| ^^^^^ expected item
6+
|
7+
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
68

79
error: aborting due to previous error
810

Diff for: tests/ui/parser/issues/issue-43196.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ error: expected item, found `|`
1111
|
1212
LL | |
1313
| ^ expected item
14+
|
15+
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
1416

1517
error: aborting due to 2 previous errors
1618

Diff for: tests/ui/parser/issues/issue-62913.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ error: expected item, found `"\u\"`
1717
|
1818
LL | "\u\"
1919
| ^^^^^^ expected item
20+
|
21+
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
2022

2123
error: aborting due to 3 previous errors
2224

Diff for: tests/ui/parser/issues/issue-68890.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ error: expected item, found `)`
1515
|
1616
LL | enum e{A((?'a a+?+l))}
1717
| ^ expected item
18+
|
19+
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
1820

1921
error: aborting due to 3 previous errors
2022

Diff for: tests/ui/parser/shebang/shebang-doc-comment.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error: expected item, found `[`
33
|
44
LL | [allow(unused_variables)]
55
| ^ expected item
6+
|
7+
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
68

79
error: aborting due to previous error
810

Diff for: tests/ui/parser/virtual-structs.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error: expected item, found reserved keyword `virtual`
33
|
44
LL | virtual struct SuperStruct {
55
| ^^^^^^^ expected item
6+
|
7+
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
68

79
error: aborting due to previous error
810

Diff for: tests/ui/pub/pub-restricted-error-fn.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ error: expected item, found `(`
1111
|
1212
LL | pub(crate) () fn foo() {}
1313
| ^ expected item
14+
|
15+
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
1416

1517
error: aborting due to 2 previous errors
1618

0 commit comments

Comments
 (0)