diff options
| author | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-11-22 16:48:42 +1100 |
|---|---|---|
| committer | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-11-23 12:11:14 +1100 |
| commit | 7c3f631ddf59b86b88b0e72ea3cb279e15f2bf07 (patch) | |
| tree | 7a84a925008a37e38899babee8bb8cd36630913c | |
| parent | e221616639fb87de9dca21e252ee8a2565ec51d0 (diff) | |
| download | rust-7c3f631ddf59b86b88b0e72ea3cb279e15f2bf07.tar.gz rust-7c3f631ddf59b86b88b0e72ea3cb279e15f2bf07.zip | |
Fix an ICE parsing a malformed attribute.
Fixes #104620.
| -rw-r--r-- | compiler/rustc_ast/src/attr/mod.rs | 9 | ||||
| -rw-r--r-- | src/test/ui/parser/issue-104620.rs | 4 | ||||
| -rw-r--r-- | src/test/ui/parser/issue-104620.stderr | 8 |
3 files changed, 18 insertions, 3 deletions
diff --git a/compiler/rustc_ast/src/attr/mod.rs b/compiler/rustc_ast/src/attr/mod.rs index 2f7c7a29492..3e012953115 100644 --- a/compiler/rustc_ast/src/attr/mod.rs +++ b/compiler/rustc_ast/src/attr/mod.rs @@ -618,9 +618,12 @@ impl MetaItemKind { }) => MetaItemKind::list_from_tokens(tokens.clone()), AttrArgs::Delimited(..) => None, AttrArgs::Eq(_, AttrArgsEq::Ast(expr)) => match expr.kind { - ast::ExprKind::Lit(token_lit) => Some(MetaItemKind::NameValue( - Lit::from_token_lit(token_lit, expr.span).expect("token_lit in from_attr_args"), - )), + ast::ExprKind::Lit(token_lit) => { + // Turn failures to `None`, we'll get parse errors elsewhere. + Lit::from_token_lit(token_lit, expr.span) + .ok() + .map(|lit| MetaItemKind::NameValue(lit)) + } _ => None, }, AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => Some(MetaItemKind::NameValue(lit.clone())), diff --git a/src/test/ui/parser/issue-104620.rs b/src/test/ui/parser/issue-104620.rs new file mode 100644 index 00000000000..f49476c4408 --- /dev/null +++ b/src/test/ui/parser/issue-104620.rs @@ -0,0 +1,4 @@ +#![feature(rustc_attrs)] + +#![rustc_dummy=5z] //~ ERROR unexpected expression: `5z` +fn main() {} diff --git a/src/test/ui/parser/issue-104620.stderr b/src/test/ui/parser/issue-104620.stderr new file mode 100644 index 00000000000..d06a6b2554b --- /dev/null +++ b/src/test/ui/parser/issue-104620.stderr @@ -0,0 +1,8 @@ +error: unexpected expression: `5z` + --> $DIR/issue-104620.rs:3:16 + | +LL | #![rustc_dummy=5z] + | ^^ + +error: aborting due to previous error + |
