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

Use token_descr more in error messages #132332

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Tweak more warnings.
Much like the previous commit.

I think the removal of "the token" in each message is fine here. There
are many more error messages that mention tokens without saying "the
token" than those that do say it.
  • Loading branch information
nnethercote committed Oct 28, 2024
commit dd2b027d5d509dd95c5ef6f83ea6283f3e98d5ed
13 changes: 3 additions & 10 deletions compiler/rustc_expand/src/mbe/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use std::borrow::Cow;

use rustc_ast::token::{self, Token, TokenKind};
use rustc_ast::tokenstream::TokenStream;
use rustc_ast_pretty::pprust;
use rustc_errors::{Applicability, Diag, DiagCtxtHandle, DiagMessage};
use rustc_macros::Subdiagnostic;
use rustc_parse::parser::{Parser, Recovery};
use rustc_parse::parser::{Parser, Recovery, token_descr};
use rustc_session::parse::ParseSess;
use rustc_span::source_map::SourceMap;
use rustc_span::symbol::Ident;
Expand Down Expand Up @@ -336,17 +335,11 @@ pub(super) fn annotate_doc_comment(err: &mut Diag<'_>, sm: &SourceMap, span: Spa
/// other tokens, this is "unexpected token...".
pub(super) fn parse_failure_msg(tok: &Token, expected_token: Option<&Token>) -> Cow<'static, str> {
if let Some(expected_token) = expected_token {
Cow::from(format!(
"expected `{}`, found `{}`",
pprust::token_to_string(expected_token),
pprust::token_to_string(tok),
))
Cow::from(format!("expected {}, found {}", token_descr(expected_token), token_descr(tok)))
} else {
match tok.kind {
token::Eof => Cow::from("unexpected end of macro invocation"),
_ => {
Cow::from(format!("no rules expected the token `{}`", pprust::token_to_string(tok)))
}
_ => Cow::from(format!("no rules expected {}", token_descr(tok))),
}
}
}
5 changes: 2 additions & 3 deletions compiler/rustc_expand/src/mbe/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,10 @@ use std::rc::Rc;
pub(crate) use NamedMatch::*;
pub(crate) use ParseResult::*;
use rustc_ast::token::{self, DocComment, NonterminalKind, Token};
use rustc_ast_pretty::pprust;
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::ErrorGuaranteed;
use rustc_lint_defs::pluralize;
use rustc_parse::parser::{ParseNtResult, Parser};
use rustc_parse::parser::{ParseNtResult, Parser, token_descr};
use rustc_span::Span;
use rustc_span::symbol::{Ident, MacroRulesNormalizedIdent};

Expand Down Expand Up @@ -150,7 +149,7 @@ impl Display for MatcherLoc {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MatcherLoc::Token { token } | MatcherLoc::SequenceSep { separator: token } => {
write!(f, "`{}`", pprust::token_to_string(token))
write!(f, "{}", token_descr(token))
}
MatcherLoc::MetaVarDecl { bind, kind, .. } => {
write!(f, "meta-variable `${bind}")?;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/array-slice-vec/vec-macro-with-comma-only.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub fn main() {
vec![,]; //~ ERROR no rules expected the token `,`
vec![,]; //~ ERROR no rules expected `,`
}
2 changes: 1 addition & 1 deletion tests/ui/array-slice-vec/vec-macro-with-comma-only.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: no rules expected the token `,`
error: no rules expected `,`
--> $DIR/vec-macro-with-comma-only.rs:2:10
|
LL | vec![,];
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/editions/edition-keywords-2015-2015-parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub fn check_async() {
let mut r#async = 1; // OK

r#async = consumes_async!(async); // OK
r#async = consumes_async!(r#async); //~ ERROR no rules expected the token `r#async`
r#async = consumes_async_raw!(async); //~ ERROR no rules expected the token `async`
r#async = consumes_async!(r#async); //~ ERROR no rules expected `r#async`
r#async = consumes_async_raw!(async); //~ ERROR no rules expected `async`
r#async = consumes_async_raw!(r#async); // OK

if passes_ident!(async) == 1 {} // OK
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/editions/edition-keywords-2015-2015-parsing.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: no rules expected the token `r#async`
error: no rules expected `r#async`
--> $DIR/edition-keywords-2015-2015-parsing.rs:16:31
|
LL | r#async = consumes_async!(r#async);
Expand All @@ -10,7 +10,7 @@ note: while trying to match `async`
LL | (async) => (1)
| ^^^^^

error: no rules expected the token `async`
error: no rules expected `async`
--> $DIR/edition-keywords-2015-2015-parsing.rs:17:35
|
LL | r#async = consumes_async_raw!(async);
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/editions/edition-keywords-2015-2018-parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub fn check_async() {
let mut r#async = 1; // OK

r#async = consumes_async!(async); // OK
r#async = consumes_async!(r#async); //~ ERROR no rules expected the token `r#async`
r#async = consumes_async_raw!(async); //~ ERROR no rules expected the token `async`
r#async = consumes_async!(r#async); //~ ERROR no rules expected `r#async`
r#async = consumes_async_raw!(async); //~ ERROR no rules expected `async`
r#async = consumes_async_raw!(r#async); // OK

if passes_ident!(async) == 1 {} // OK
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/editions/edition-keywords-2015-2018-parsing.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: no rules expected the token `r#async`
error: no rules expected `r#async`
--> $DIR/edition-keywords-2015-2018-parsing.rs:16:31
|
LL | r#async = consumes_async!(r#async);
Expand All @@ -10,7 +10,7 @@ note: while trying to match `async`
LL | (async) => (1)
| ^^^^^

error: no rules expected the token `async`
error: no rules expected `async`
--> $DIR/edition-keywords-2015-2018-parsing.rs:17:35
|
LL | r#async = consumes_async_raw!(async);
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/editions/edition-keywords-2018-2015-parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub fn check_async() {
let mut r#async = 1; // OK

r#async = consumes_async!(async); // OK
r#async = consumes_async!(r#async); //~ ERROR no rules expected the token `r#async`
r#async = consumes_async_raw!(async); //~ ERROR no rules expected the token `async`
r#async = consumes_async!(r#async); //~ ERROR no rules expected `r#async`
r#async = consumes_async_raw!(async); //~ ERROR no rules expected keyword `async`
r#async = consumes_async_raw!(r#async); // OK

if passes_ident!(async) == 1 {} // FIXME: Edition hygiene bug, async here is 2018 and reserved
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/editions/edition-keywords-2018-2015-parsing.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ help: escape `async` to use it as an identifier
LL | module::r#async();
| ++

error: no rules expected the token `r#async`
error: no rules expected `r#async`
--> $DIR/edition-keywords-2018-2015-parsing.rs:20:31
|
LL | r#async = consumes_async!(r#async);
| ^^^^^^^ no rules expected this token in macro call
|
note: while trying to match `async`
note: while trying to match keyword `async`
--> $DIR/auxiliary/edition-kw-macro-2015.rs:17:6
|
LL | (async) => (1)
| ^^^^^

error: no rules expected the token `async`
error: no rules expected keyword `async`
--> $DIR/edition-keywords-2018-2015-parsing.rs:21:35
|
LL | r#async = consumes_async_raw!(async);
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/editions/edition-keywords-2018-2018-parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub fn check_async() {
let mut r#async = 1; // OK

r#async = consumes_async!(async); // OK
r#async = consumes_async!(r#async); //~ ERROR no rules expected the token `r#async`
r#async = consumes_async_raw!(async); //~ ERROR no rules expected the token `async`
r#async = consumes_async!(r#async); //~ ERROR no rules expected `r#async`
r#async = consumes_async_raw!(async); //~ ERROR no rules expected keyword `async`
r#async = consumes_async_raw!(r#async); // OK

if passes_ident!(async) == 1 {} // FIXME: Edition hygiene bug, async here is 2018 and reserved
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/editions/edition-keywords-2018-2018-parsing.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ help: escape `async` to use it as an identifier
LL | module::r#async();
| ++

error: no rules expected the token `r#async`
error: no rules expected `r#async`
--> $DIR/edition-keywords-2018-2018-parsing.rs:27:31
|
LL | r#async = consumes_async!(r#async);
| ^^^^^^^ no rules expected this token in macro call
|
note: while trying to match `async`
note: while trying to match keyword `async`
--> $DIR/auxiliary/edition-kw-macro-2018.rs:17:6
|
LL | (async) => (1)
| ^^^^^

error: no rules expected the token `async`
error: no rules expected keyword `async`
--> $DIR/edition-keywords-2018-2018-parsing.rs:28:35
|
LL | r#async = consumes_async_raw!(async);
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/fail-simple.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main() {
panic!(@); //~ ERROR no rules expected the token `@`
panic!(@); //~ ERROR no rules expected `@`
}
2 changes: 1 addition & 1 deletion tests/ui/fail-simple.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: no rules expected the token `@`
error: no rules expected `@`
--> $DIR/fail-simple.rs:2:12
|
LL | panic!(@);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ error: expected one of `,`, `.`, `?`, or an operator, found `some`
LL | assert!(true some extra junk);
| ^^^^ expected one of `,`, `.`, `?`, or an operator

error: no rules expected the token `blah`
error: no rules expected `blah`
--> $DIR/assert-trailing-junk.rs:15:30
|
LL | assert!(true, "whatever" blah);
Expand All @@ -28,7 +28,7 @@ LL | assert!(true "whatever" blah);
| |
| help: try adding a comma

error: no rules expected the token `blah`
error: no rules expected `blah`
--> $DIR/assert-trailing-junk.rs:18:29
|
LL | assert!(true "whatever" blah);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ error: expected one of `,`, `.`, `?`, or an operator, found `some`
LL | assert!(true some extra junk);
| ^^^^ expected one of `,`, `.`, `?`, or an operator

error: no rules expected the token `blah`
error: no rules expected `blah`
--> $DIR/assert-trailing-junk.rs:15:30
|
LL | assert!(true, "whatever" blah);
Expand All @@ -28,7 +28,7 @@ LL | assert!(true "whatever" blah);
| |
| help: try adding a comma

error: no rules expected the token `blah`
error: no rules expected `blah`
--> $DIR/assert-trailing-junk.rs:18:29
|
LL | assert!(true "whatever" blah);
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/best-failure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ macro_rules! number {
(neg false, $self:ident) => { $self };
($signed:tt => $ty:ty;) => {
number!(neg $signed, $self);
//~^ ERROR no rules expected the token `$`
//~^ ERROR no rules expected `$`
};
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/best-failure.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: no rules expected the token `$`
error: no rules expected `$`
--> $DIR/best-failure.rs:4:30
|
LL | macro_rules! number {
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/macros/expr_2021_inline_const.edi2021.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: no rules expected the token `const`
error: no rules expected keyword `const`
--> $DIR/expr_2021_inline_const.rs:23:12
|
LL | macro_rules! m2021 {
Expand All @@ -13,7 +13,7 @@ note: while trying to match meta-variable `$e:expr_2021`
LL | ($e:expr_2021) => {
| ^^^^^^^^^^^^

error: no rules expected the token `const`
error: no rules expected keyword `const`
--> $DIR/expr_2021_inline_const.rs:24:12
|
LL | macro_rules! m2024 {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/expr_2021_inline_const.edi2024.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: no rules expected the token `const`
error: no rules expected keyword `const`
--> $DIR/expr_2021_inline_const.rs:23:12
|
LL | macro_rules! m2021 {
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/macros/expr_2021_inline_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ macro_rules! test {
}

fn main() {
m2021!(const { 1 }); //~ ERROR: no rules expected the token `const`
m2024!(const { 1 }); //[edi2021]~ ERROR: no rules expected the token `const`
m2021!(const { 1 }); //~ ERROR: no rules expected keyword `const`
m2024!(const { 1 }); //[edi2021]~ ERROR: no rules expected keyword `const`

test!(expr);
}
4 changes: 2 additions & 2 deletions tests/ui/macros/expr_2024_underscore_expr.edi2021.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: no rules expected the token `_`
error: no rules expected reserved identifier `_`
--> $DIR/expr_2024_underscore_expr.rs:19:12
|
LL | macro_rules! m2021 {
Expand All @@ -13,7 +13,7 @@ note: while trying to match meta-variable `$e:expr_2021`
LL | ($e:expr_2021) => {
| ^^^^^^^^^^^^

error: no rules expected the token `_`
error: no rules expected reserved identifier `_`
--> $DIR/expr_2024_underscore_expr.rs:20:12
|
LL | macro_rules! m2024 {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/expr_2024_underscore_expr.edi2024.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: no rules expected the token `_`
error: no rules expected reserved identifier `_`
--> $DIR/expr_2024_underscore_expr.rs:19:12
|
LL | macro_rules! m2021 {
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/macros/expr_2024_underscore_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ macro_rules! m2024 {
}

fn main() {
m2021!(_); //~ ERROR: no rules expected the token `_`
m2024!(_); //[edi2021]~ ERROR: no rules expected the token `_`
m2021!(_); //~ ERROR: no rules expected reserved identifier `_`
m2024!(_); //[edi2021]~ ERROR: no rules expected reserved identifier `_`
}
14 changes: 7 additions & 7 deletions tests/ui/macros/macro-at-most-once-rep-2015.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ macro_rules! barstar {
pub fn main() {
foo!();
foo!(a);
foo!(a?); //~ ERROR no rules expected the token `?`
foo!(a?a); //~ ERROR no rules expected the token `?`
foo!(a?a?a); //~ ERROR no rules expected the token `?`
foo!(a?); //~ ERROR no rules expected `?`
foo!(a?a); //~ ERROR no rules expected `?`
foo!(a?a?a); //~ ERROR no rules expected `?`

barplus!(); //~ERROR unexpected end of macro invocation
barplus!(a); //~ERROR unexpected end of macro invocation
barplus!(a?); //~ ERROR no rules expected the token `?`
barplus!(a?a); //~ ERROR no rules expected the token `?`
barplus!(a?); //~ ERROR no rules expected `?`
barplus!(a?a); //~ ERROR no rules expected `?`
barplus!(a+);
barplus!(+);

barstar!(); //~ERROR unexpected end of macro invocation
barstar!(a); //~ERROR unexpected end of macro invocation
barstar!(a?); //~ ERROR no rules expected the token `?`
barstar!(a?a); //~ ERROR no rules expected the token `?`
barstar!(a?); //~ ERROR no rules expected `?`
barstar!(a?a); //~ ERROR no rules expected `?`
barstar!(a*);
barstar!(*);
}
Loading
Loading