Skip to content

Commit

Permalink
Properly emit expected ; on #[attr] expr
Browse files Browse the repository at this point in the history
  • Loading branch information
ShE3py committed Feb 26, 2024
1 parent 829308e commit 1658ca0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
6 changes: 2 additions & 4 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]
| ^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -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!( (); (); );
}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1658ca0

Please sign in to comment.