Properly emit expected ; on #[attr] expr

This commit is contained in:
Lieselotte 2024-02-26 21:47:10 +01:00
parent 829308e9af
commit 1658ca082a
No known key found for this signature in database
GPG Key ID: 43A6A32F83A6F9B1
5 changed files with 45 additions and 6 deletions

View File

@ -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

View File

@ -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]
| ^^^^^^^

View File

@ -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!( (); (); );
}

View File

@ -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