diff options
| author | yukang <moorekang@gmail.com> | 2023-06-11 14:36:20 +0800 |
|---|---|---|
| committer | yukang <moorekang@gmail.com> | 2023-06-11 14:36:20 +0800 |
| commit | 0220c0b765e92dcb3565f0a8bd3c422afbd77f88 (patch) | |
| tree | 5b6a5c880ca34aaeee73cdac0176dd019fc4a1bf | |
| parent | b8a50010de397df570b38fe67bda435b665e2d86 (diff) | |
| download | rust-0220c0b765e92dcb3565f0a8bd3c422afbd77f88.tar.gz rust-0220c0b765e92dcb3565f0a8bd3c422afbd77f88.zip | |
Detect actual span for getting unexpected token from parsing macros
| -rw-r--r-- | compiler/rustc_parse/src/parser/expr.rs | 10 | ||||
| -rw-r--r-- | tests/ui/parser/issues/issue-112458.rs | 4 | ||||
| -rw-r--r-- | tests/ui/parser/issues/issue-112458.stderr | 15 |
3 files changed, 27 insertions, 2 deletions
diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index cea2a71c988..d81b3899201 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -1013,9 +1013,15 @@ impl<'a> Parser<'a> { } fn error_unexpected_after_dot(&self) { - // FIXME Could factor this out into non_fatal_unexpected or something. let actual = pprust::token_to_string(&self.token); - self.sess.emit_err(errors::UnexpectedTokenAfterDot { span: self.token.span, actual }); + let span = self.token.span; + let sm = self.sess.source_map(); + let (span, actual) = match (&self.token.kind, self.subparser_name) { + (token::Eof, Some(_)) if let Ok(actual) = sm.span_to_snippet(sm.next_point(span)) => + (span.shrink_to_hi(), actual.into()), + _ => (span, actual), + }; + self.sess.emit_err(errors::UnexpectedTokenAfterDot { span, actual }); } // We need an identifier or integer, but the next token is a float. diff --git a/tests/ui/parser/issues/issue-112458.rs b/tests/ui/parser/issues/issue-112458.rs new file mode 100644 index 00000000000..36895450cd9 --- /dev/null +++ b/tests/ui/parser/issues/issue-112458.rs @@ -0,0 +1,4 @@ +fn main() { + println!("{}", x.); //~ ERROR unexpected token: `)` + //~^ ERROR cannot find value `x` in this scope +} diff --git a/tests/ui/parser/issues/issue-112458.stderr b/tests/ui/parser/issues/issue-112458.stderr new file mode 100644 index 00000000000..54a8f1d03b0 --- /dev/null +++ b/tests/ui/parser/issues/issue-112458.stderr @@ -0,0 +1,15 @@ +error: unexpected token: `)` + --> $DIR/issue-112458.rs:2:22 + | +LL | println!("{}", x.); + | ^ + +error[E0425]: cannot find value `x` in this scope + --> $DIR/issue-112458.rs:2:20 + | +LL | println!("{}", x.); + | ^ not found in this scope + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0425`. |
