diff options
| author | Lieselotte <52315535+she3py@users.noreply.github.com> | 2024-02-26 21:47:10 +0100 | 
|---|---|---|
| committer | Lieselotte <52315535+she3py@users.noreply.github.com> | 2024-02-26 21:47:10 +0100 | 
| commit | 1658ca082a1d510480a7c228b0b6bfb83863d0e9 (patch) | |
| tree | abb0f2ae4b5649622cd3bc0dff437316562be279 | |
| parent | 829308e9af1a1f55540d2c8b0e007e1d0cb9c8c6 (diff) | |
| download | rust-1658ca082a1d510480a7c228b0b6bfb83863d0e9.tar.gz rust-1658ca082a1d510480a7c228b0b6bfb83863d0e9.zip  | |
Properly emit `expected ;` on `#[attr] expr`
| -rw-r--r-- | compiler/rustc_parse/src/parser/diagnostics.rs | 6 | ||||
| -rw-r--r-- | tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body-1.rs (renamed from tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body.rs) | 0 | ||||
| -rw-r--r-- | tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body-1.stderr (renamed from tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body.stderr) | 4 | ||||
| -rw-r--r-- | tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body-2.rs | 15 | ||||
| -rw-r--r-- | tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body-2.stderr | 26 | 
5 files changed, 45 insertions, 6 deletions
diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index cc1f7c8ac7d..995e140b329 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 index a7412f51782..a7412f51782 100644 --- 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 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 index dd0081cc2df..dcc2e92c47a 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::<String>() } | + + 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 00000000000..ad9e7ad707b --- /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 00000000000..7b9b8319674 --- /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 +  | 
