Skip to content

Commit 899bd1e

Browse files
authoredMay 21, 2024
Unrolled build for rust-lang#125276
Rollup merge of rust-lang#125276 - dev-ardi:no-main-diag, r=fmease Fix parsing of erroneously placed semicolons This closes rust-lang#124935, is a continuation of rust-lang#125245 after rebasing rust-lang#125117. Thanks ```@gurry``` for your code and sorry for making it confusing :P r? fmease
2 parents 5065123 + 972633f commit 899bd1e

7 files changed

+42
-7
lines changed
 

‎compiler/rustc_parse/src/parser/item.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,15 @@ impl<'a> Parser<'a> {
5858
let attrs = self.parse_inner_attributes()?;
5959

6060
let post_attr_lo = self.token.span;
61-
let mut items = ThinVec::new();
62-
while let Some(item) = self.parse_item(ForceCollect::No)? {
63-
self.maybe_consume_incorrect_semicolon(Some(&item));
61+
let mut items: ThinVec<P<_>> = ThinVec::new();
62+
63+
// There shouldn't be any stray semicolons before or after items.
64+
// `parse_item` consumes the appropriate semicolons so any leftover is an error.
65+
loop {
66+
while self.maybe_consume_incorrect_semicolon(items.last().map(|x| &**x)) {} // Eat all bad semicolons
67+
let Some(item) = self.parse_item(ForceCollect::No)? else {
68+
break;
69+
};
6470
items.push(item);
6571
}
6672

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Regression test for issue #124935
2+
// Tests that we do not erroneously emit an error about
3+
// missing main function when the mod starts with a `;`
4+
5+
; //~ ERROR expected item, found `;`
6+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: expected item, found `;`
2+
--> $DIR/fn-no-semicolon-issue-124935-semi-after-item.rs:5:1
3+
|
4+
LL | ;
5+
| ^ help: remove this semicolon
6+
7+
error: aborting due to 1 previous error
8+

‎tests/ui/parser/issues/issue-49040.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#![allow(unused_variables)]; //~ ERROR expected item, found `;`
2-
//~^ ERROR `main` function
32
fn foo() {}
3+
//~^ ERROR `main` function

‎tests/ui/parser/issues/issue-49040.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ LL | #![allow(unused_variables)];
55
| ^ help: remove this semicolon
66

77
error[E0601]: `main` function not found in crate `issue_49040`
8-
--> $DIR/issue-49040.rs:1:29
8+
--> $DIR/issue-49040.rs:2:12
99
|
10-
LL | #![allow(unused_variables)];
11-
| ^ consider adding a `main` function to `$DIR/issue-49040.rs`
10+
LL | fn foo() {}
11+
| ^ consider adding a `main` function to `$DIR/issue-49040.rs`
1212

1313
error: aborting due to 2 previous errors
1414

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Regression test for issue #124935
2+
// Tests that we still emit an error after an item.
3+
4+
fn main() { }
5+
; //~ ERROR expected item, found `;`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: expected item, found `;`
2+
--> $DIR/missing-main-issue-124935-semi-after-item.rs:5:1
3+
|
4+
LL | ;
5+
| ^ help: remove this semicolon
6+
|
7+
= help: function declarations are not followed by a semicolon
8+
9+
error: aborting due to 1 previous error
10+

0 commit comments

Comments
 (0)