From 9ef580fa6f2b5019b35e6a9cf979f6f13d21de00 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 15 Jun 2023 01:55:30 +0000 Subject: [PATCH] Handle interpolated literal errors --- compiler/rustc_parse/src/parser/expr.rs | 9 +++------ tests/ui/parser/lit-err-in-macro.rs | 10 ++++++++++ tests/ui/parser/lit-err-in-macro.stderr | 8 ++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 tests/ui/parser/lit-err-in-macro.rs create mode 100644 tests/ui/parser/lit-err-in-macro.stderr diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index cea2a71c98821..f00bc54589a7f 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -2023,17 +2023,14 @@ impl<'a> Parser<'a> { let recovered = self.recover_after_dot(); let token = recovered.as_ref().unwrap_or(&self.token); match token::Lit::from_token(token) { - Some(token_lit) => { - match MetaItemLit::from_token_lit(token_lit, token.span) { + Some(lit) => { + match MetaItemLit::from_token_lit(lit, token.span) { Ok(lit) => { self.bump(); Some(lit) } Err(err) => { - let span = token.span; - let token::Literal(lit) = token.kind else { - unreachable!(); - }; + let span = token.uninterpolated_span(); self.bump(); report_lit_error(&self.sess, err, lit, span); // Pack possible quotes and prefixes from the original literal into diff --git a/tests/ui/parser/lit-err-in-macro.rs b/tests/ui/parser/lit-err-in-macro.rs new file mode 100644 index 0000000000000..cff8ee6b40ca6 --- /dev/null +++ b/tests/ui/parser/lit-err-in-macro.rs @@ -0,0 +1,10 @@ +macro_rules! f { + ($abi:literal) => { + extern $abi fn f() {} + } +} + +f!("Foo"__); +//~^ ERROR suffixes on string literals are invalid + +fn main() {} diff --git a/tests/ui/parser/lit-err-in-macro.stderr b/tests/ui/parser/lit-err-in-macro.stderr new file mode 100644 index 0000000000000..a61fb5c85d492 --- /dev/null +++ b/tests/ui/parser/lit-err-in-macro.stderr @@ -0,0 +1,8 @@ +error: suffixes on string literals are invalid + --> $DIR/lit-err-in-macro.rs:7:4 + | +LL | f!("Foo"__); + | ^^^^^^^ invalid suffix `__` + +error: aborting due to previous error +