Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix parsing of erroneously placed semicolons
There were two issues here: a. we weren't consuming
any semicolons that occurred at the start of a mod
before all the items b. we weren't consuming multiple
consecutive wrongly placed semicolons; we consumed
only one and then tried to consume an item.
  • Loading branch information
Gurinder Singh committed May 18, 2024
commit 18ef63604be48779ce39b4f229f3d71d446133b6
13 changes: 10 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,16 @@ impl<'a> Parser<'a> {

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

loop {
// Consume all incorrect semicolons before trying
// to parse an item
while self.maybe_consume_incorrect_semicolon(&items) {}

match self.parse_item(ForceCollect::No)? {
Some(item) => items.push(item),
None => break,
}
}

if !self.eat(term) {
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/incorrect-semicolon-missing-main-err-124935.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