Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly recover from trailing attr in body #118182

Merged
merged 1 commit into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,13 +792,28 @@ impl<'a> Parser<'a> {
&& let [segment] = &attr_kind.item.path.segments[..]
&& segment.ident.name == sym::cfg
&& let Some(args_span) = attr_kind.item.args.span()
&& let Ok(next_attr) = snapshot.parse_attribute(InnerAttrPolicy::Forbidden(None))
&& let next_attr = match snapshot.parse_attribute(InnerAttrPolicy::Forbidden(None))
{
Ok(next_attr) => next_attr,
Err(inner_err) => {
err.cancel();
inner_err.cancel();
return;
}
}
&& let ast::AttrKind::Normal(next_attr_kind) = next_attr.kind
&& let Some(next_attr_args_span) = next_attr_kind.item.args.span()
&& let [next_segment] = &next_attr_kind.item.path.segments[..]
&& segment.ident.name == sym::cfg
&& let Ok(next_expr) = snapshot.parse_expr()
{
let next_expr = match snapshot.parse_expr() {
Ok(next_expr) => next_expr,
Err(inner_err) => {
err.cancel();
inner_err.cancel();
return;
}
};
// We have for sure
// #[cfg(..)]
// expr
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Issue #118164: recovery path leaving unemitted error behind
fn bar() -> String {
#[cfg(feature = )]
[1, 2, 3].iter().map().collect::<String>() //~ ERROR expected `;`, found `#`
#[attr] //~ ERROR expected statement after outer attribute
}
fn main() {
let _ = bar();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error: expected `;`, found `#`
--> $DIR/properly-recover-from-trailing-outer-attribute-in-body.rs:4:47
|
LL | #[cfg(feature = )]
| ------------------ only `;` terminated statements or tail expressions are allowed after this attribute
LL | [1, 2, 3].iter().map().collect::<String>()
| ^ expected `;` here
LL | #[attr]
| - unexpected token
|
help: add `;` here
|
LL | [1, 2, 3].iter().map().collect::<String>();
| +
help: alternatively, consider surrounding the expression with a block
|
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
|
LL | #[attr]
| ^^^^^^^

error: aborting due to 2 previous errors

Loading