Skip to content

Commit ec5c5b7

Browse files
authored
Rollup merge of rust-lang#121651 - ShE3py:issue-121647, r=estebank
Properly emit `expected ;` on `#[attr] expr` Fixes rust-lang#121647 See rust-lang#118182, rust-lang#120586 --- r? estebank
2 parents 94609db + 1658ca0 commit ec5c5b7

5 files changed

+45
-6
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -800,9 +800,8 @@ impl<'a> Parser<'a> {
800800
{
801801
Ok(next_attr) => next_attr,
802802
Err(inner_err) => {
803-
err.cancel();
804803
inner_err.cancel();
805-
return self.dcx().span_delayed_bug(expr.span, "not a tail expression");
804+
return err.emit();
806805
}
807806
}
808807
&& let ast::AttrKind::Normal(next_attr_kind) = next_attr.kind
@@ -813,9 +812,8 @@ impl<'a> Parser<'a> {
813812
let next_expr = match snapshot.parse_expr() {
814813
Ok(next_expr) => next_expr,
815814
Err(inner_err) => {
816-
err.cancel();
817815
inner_err.cancel();
818-
return self.dcx().span_delayed_bug(expr.span, "not a tail expression");
816+
return err.emit();
819817
}
820818
};
821819
// We have for sure

tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body.stderr renamed to tests/ui/parser/attribute/properly-recover-from-trailing-outer-attribute-in-body-1.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: expected `;`, found `#`
2-
--> $DIR/properly-recover-from-trailing-outer-attribute-in-body.rs:4:47
2+
--> $DIR/properly-recover-from-trailing-outer-attribute-in-body-1.rs:4:47
33
|
44
LL | #[cfg(feature = )]
55
| ------------------ only `;` terminated statements or tail expressions are allowed after this attribute
@@ -18,7 +18,7 @@ LL | { [1, 2, 3].iter().map().collect::<String>() }
1818
| + +
1919

2020
error: expected statement after outer attribute
21-
--> $DIR/properly-recover-from-trailing-outer-attribute-in-body.rs:5:5
21+
--> $DIR/properly-recover-from-trailing-outer-attribute-in-body-1.rs:5:5
2222
|
2323
LL | #[attr]
2424
| ^^^^^^^
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Issue #121647: recovery path leaving unemitted error behind
2+
3+
macro_rules! the_macro {
4+
( $foo:stmt ; $bar:stmt ; ) => {
5+
#[cfg()]
6+
$foo //~ ERROR expected `;`, found `#`
7+
8+
#[cfg(bar)]
9+
$bar
10+
};
11+
}
12+
13+
fn main() {
14+
the_macro!( (); (); );
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: expected `;`, found `#`
2+
--> $DIR/properly-recover-from-trailing-outer-attribute-in-body-2.rs:6:13
3+
|
4+
LL | #[cfg()]
5+
| -------- only `;` terminated statements or tail expressions are allowed after this attribute
6+
LL | $foo
7+
| ^ expected `;` here
8+
LL |
9+
LL | #[cfg(bar)]
10+
| - unexpected token
11+
...
12+
LL | the_macro!( (); (); );
13+
| --------------------- in this macro invocation
14+
|
15+
= note: this error originates in the macro `the_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
help: add `;` here
17+
|
18+
LL | $foo;
19+
| +
20+
help: alternatively, consider surrounding the expression with a block
21+
|
22+
LL | the_macro!( { () }; (); );
23+
| + +
24+
25+
error: aborting due to 1 previous error
26+

0 commit comments

Comments
 (0)