Skip to content

Commit

Permalink
Rollup merge of rust-lang#125276 - dev-ardi:no-main-diag, r=fmease
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fmease authored May 21, 2024
2 parents 43bf82a + 972633f commit 71a14c6
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 7 deletions.
12 changes: 9 additions & 3 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,15 @@ impl<'a> Parser<'a> {
let attrs = self.parse_inner_attributes()?;

let post_attr_lo = self.token.span;
let mut items = ThinVec::new();
while let Some(item) = self.parse_item(ForceCollect::No)? {
self.maybe_consume_incorrect_semicolon(Some(&item));
let mut items: ThinVec<P<_>> = ThinVec::new();

// There shouldn't be any stray semicolons before or after items.
// `parse_item` consumes the appropriate semicolons so any leftover is an error.
loop {
while self.maybe_consume_incorrect_semicolon(items.last().map(|x| &**x)) {} // Eat all bad semicolons
let Some(item) = self.parse_item(ForceCollect::No)? else {
break;
};
items.push(item);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Regression test for issue #124935
// Tests that we do not erroneously emit an error about
// missing main function when the mod starts with a `;`

; //~ ERROR expected item, found `;`
fn main() { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: expected item, found `;`
--> $DIR/fn-no-semicolon-issue-124935-semi-after-item.rs:5:1
|
LL | ;
| ^ help: remove this semicolon

error: aborting due to 1 previous error

2 changes: 1 addition & 1 deletion tests/ui/parser/issues/issue-49040.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#![allow(unused_variables)]; //~ ERROR expected item, found `;`
//~^ ERROR `main` function
fn foo() {}
//~^ ERROR `main` function
6 changes: 3 additions & 3 deletions tests/ui/parser/issues/issue-49040.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ LL | #![allow(unused_variables)];
| ^ help: remove this semicolon

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

error: aborting due to 2 previous errors

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Regression test for issue #124935
// Tests that we still emit an error after an item.

fn main() { }
; //~ ERROR expected item, found `;`
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: expected item, found `;`
--> $DIR/missing-main-issue-124935-semi-after-item.rs:5:1
|
LL | ;
| ^ help: remove this semicolon
|
= help: function declarations are not followed by a semicolon

error: aborting due to 1 previous error

0 comments on commit 71a14c6

Please sign in to comment.