From 18ef63604be48779ce39b4f229f3d71d446133b6 Mon Sep 17 00:00:00 2001 From: Gurinder Singh Date: Sat, 18 May 2024 16:59:39 +0530 Subject: [PATCH] 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. --- compiler/rustc_parse/src/parser/item.rs | 13 ++++++++++--- .../incorrect-semicolon-missing-main-err-124935.rs | 6 ++++++ ...correct-semicolon-missing-main-err-124935.stderr | 8 ++++++++ tests/ui/parser/issues/issue-49040.rs | 2 +- tests/ui/parser/issues/issue-49040.stderr | 6 +++--- 5 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 tests/ui/parser/issues/incorrect-semicolon-missing-main-err-124935.rs create mode 100644 tests/ui/parser/issues/incorrect-semicolon-missing-main-err-124935.stderr diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 5a3bfb8372520..4308155e0041e 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -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) { diff --git a/tests/ui/parser/issues/incorrect-semicolon-missing-main-err-124935.rs b/tests/ui/parser/issues/incorrect-semicolon-missing-main-err-124935.rs new file mode 100644 index 0000000000000..3c0059ba3e3e3 --- /dev/null +++ b/tests/ui/parser/issues/incorrect-semicolon-missing-main-err-124935.rs @@ -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() { } diff --git a/tests/ui/parser/issues/incorrect-semicolon-missing-main-err-124935.stderr b/tests/ui/parser/issues/incorrect-semicolon-missing-main-err-124935.stderr new file mode 100644 index 0000000000000..493dbb6621e8a --- /dev/null +++ b/tests/ui/parser/issues/incorrect-semicolon-missing-main-err-124935.stderr @@ -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 + diff --git a/tests/ui/parser/issues/issue-49040.rs b/tests/ui/parser/issues/issue-49040.rs index b7a541dd6642a..68e7cc9f80ebe 100644 --- a/tests/ui/parser/issues/issue-49040.rs +++ b/tests/ui/parser/issues/issue-49040.rs @@ -1,3 +1,3 @@ #![allow(unused_variables)]; //~ ERROR expected item, found `;` -//~^ ERROR `main` function fn foo() {} +//~^ ERROR `main` function diff --git a/tests/ui/parser/issues/issue-49040.stderr b/tests/ui/parser/issues/issue-49040.stderr index 8af7838c79138..11ef5e1aadfd8 100644 --- a/tests/ui/parser/issues/issue-49040.stderr +++ b/tests/ui/parser/issues/issue-49040.stderr @@ -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