From 1658ca082a1d510480a7c228b0b6bfb83863d0e9 Mon Sep 17 00:00:00 2001 From: Lieselotte <52315535+she3py@users.noreply.github.com> Date: Mon, 26 Feb 2024 21:47:10 +0100 Subject: [PATCH] Properly emit `expected ;` on `#[attr] expr` --- .../rustc_parse/src/parser/diagnostics.rs | 6 ++--- ...rom-trailing-outer-attribute-in-body-1.rs} | 0 ...trailing-outer-attribute-in-body-1.stderr} | 4 +-- ...from-trailing-outer-attribute-in-body-2.rs | 15 +++++++++++ ...-trailing-outer-attribute-in-body-2.stderr | 26 +++++++++++++++++++ 5 files changed, 45 insertions(+), 6 deletions(-) rename tests/ui/parser/attribute/{properly-recover-from-trailing-outer-attribute-in-body.rs => properly-recover-from-trailing-outer-attribute-in-body-1.rs} (100%) rename tests/ui/parser/attribute/{properly-recover-from-trailing-outer-attribute-in-body.stderr => properly-recover-from-trailing-outer-attribute-in-body-1.stderr} (97%) create mode 100644 tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body-2.rs create mode 100644 tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body-2.stderr diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index cc1f7c8ac7d58..995e140b32921 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -800,9 +800,8 @@ impl<'a> Parser<'a> { { Ok(next_attr) => next_attr, Err(inner_err) => { - err.cancel(); inner_err.cancel(); - return self.dcx().span_delayed_bug(expr.span, "not a tail expression"); + return err.emit(); } } && let ast::AttrKind::Normal(next_attr_kind) = next_attr.kind @@ -813,9 +812,8 @@ impl<'a> Parser<'a> { let next_expr = match snapshot.parse_expr() { Ok(next_expr) => next_expr, Err(inner_err) => { - err.cancel(); inner_err.cancel(); - return self.dcx().span_delayed_bug(expr.span, "not a tail expression"); + return err.emit(); } }; // We have for sure diff --git a/tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body.rs b/tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body-1.rs similarity index 100% rename from tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body.rs rename to tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body-1.rs diff --git a/tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body.stderr b/tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body-1.stderr similarity index 97% rename from tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body.stderr rename to tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body-1.stderr index dd0081cc2dff9..dcc2e92c47ad8 100644 --- a/tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body.stderr +++ b/tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body-1.stderr @@ -1,5 +1,5 @@ error: expected `;`, found `#` - --> $DIR/properly-recover-from-trailing-outer-attribute-in-body.rs:4:47 + --> $DIR/properly-recover-from-trailing-outer-attribute-in-body-1.rs:4:47 | LL | #[cfg(feature = )] | ------------------ only `;` terminated statements or tail expressions are allowed after this attribute @@ -18,7 +18,7 @@ LL | { [1, 2, 3].iter().map().collect::() } | + + error: expected statement after outer attribute - --> $DIR/properly-recover-from-trailing-outer-attribute-in-body.rs:5:5 + --> $DIR/properly-recover-from-trailing-outer-attribute-in-body-1.rs:5:5 | LL | #[attr] | ^^^^^^^ diff --git a/tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body-2.rs b/tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body-2.rs new file mode 100644 index 0000000000000..ad9e7ad707b53 --- /dev/null +++ b/tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body-2.rs @@ -0,0 +1,15 @@ +// Issue #121647: recovery path leaving unemitted error behind + +macro_rules! the_macro { + ( $foo:stmt ; $bar:stmt ; ) => { + #[cfg()] + $foo //~ ERROR expected `;`, found `#` + + #[cfg(bar)] + $bar + }; +} + +fn main() { + the_macro!( (); (); ); +} diff --git a/tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body-2.stderr b/tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body-2.stderr new file mode 100644 index 0000000000000..7b9b8319674e4 --- /dev/null +++ b/tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body-2.stderr @@ -0,0 +1,26 @@ +error: expected `;`, found `#` + --> $DIR/properly-recover-from-trailing-outer-attribute-in-body-2.rs:6:13 + | +LL | #[cfg()] + | -------- only `;` terminated statements or tail expressions are allowed after this attribute +LL | $foo + | ^ expected `;` here +LL | +LL | #[cfg(bar)] + | - unexpected token +... +LL | the_macro!( (); (); ); + | --------------------- in this macro invocation + | + = note: this error originates in the macro `the_macro` (in Nightly builds, run with -Z macro-backtrace for more info) +help: add `;` here + | +LL | $foo; + | + +help: alternatively, consider surrounding the expression with a block + | +LL | the_macro!( { () }; (); ); + | + + + +error: aborting due to 1 previous error +