Skip to content

Commit

Permalink
Rollup merge of #132332 - nnethercote:use-token_descr-more, r=estebank
Browse files Browse the repository at this point in the history
Use `token_descr` more in error messages

This is the first two commits from #124141, put into their own PR to get things rolling. Commit messages have the details.

r? ``@estebank``
cc ``@petrochenkov``
  • Loading branch information
matthiaskrgr authored Oct 30, 2024
2 parents af33bc3 + dd2b027 commit 2480e3b
Show file tree
Hide file tree
Showing 77 changed files with 152 additions and 159 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_expand/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ expand_helper_attribute_name_invalid =
`{$name}` cannot be a name of derive helper attribute
expand_incomplete_parse =
macro expansion ignores token `{$token}` and any following
macro expansion ignores {$descr} and any tokens following
.label = caused by the macro expansion here
.note = the usage of `{$macro_path}!` is likely invalid in {$kind_name} context
.suggestion_add_semi = you might be missing a semicolon here
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ pub(crate) struct UnsupportedKeyValue {
pub(crate) struct IncompleteParse<'a> {
#[primary_span]
pub span: Span,
pub token: Cow<'a, str>,
pub descr: String,
#[label]
pub label_span: Span,
pub macro_path: &'a ast::Path,
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use rustc_errors::PResult;
use rustc_feature::Features;
use rustc_parse::parser::{
AttemptLocalParseRecovery, CommaRecoveryMode, ForceCollect, Parser, RecoverColon, RecoverComma,
token_descr,
};
use rustc_parse::validate_attr;
use rustc_session::lint::BuiltinLintDiag;
Expand Down Expand Up @@ -1013,7 +1014,7 @@ pub(crate) fn ensure_complete_parse<'a>(
span: Span,
) {
if parser.token != token::Eof {
let token = pprust::token_to_string(&parser.token);
let descr = token_descr(&parser.token);
// Avoid emitting backtrace info twice.
let def_site_span = parser.token.span.with_ctxt(SyntaxContext::root());

Expand All @@ -1029,7 +1030,7 @@ pub(crate) fn ensure_complete_parse<'a>(

parser.dcx().emit_err(IncompleteParse {
span: def_site_span,
token,
descr,
label_span: span,
macro_path,
kind_name,
Expand Down
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 compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ impl TokenDescription {
}
}

pub(super) fn token_descr(token: &Token) -> String {
pub fn token_descr(token: &Token) -> String {
let name = pprust::token_to_string(token).to_string();

let kind = match (TokenDescription::from_token(token), &token.kind) {
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 `_`
}
2 changes: 1 addition & 1 deletion tests/ui/macros/issue-118786.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
macro_rules! make_macro {
($macro_name:tt) => {
macro_rules! $macro_name {
//~^ ERROR macro expansion ignores token `{` and any following
//~^ ERROR macro expansion ignores `{` and any tokens following
//~| ERROR cannot find macro `macro_rules` in this scope
//~| put a macro name here
() => {}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/issue-118786.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ help: add a semicolon
LL | macro_rules! $macro_name; {
| +

error: macro expansion ignores token `{` and any following
error: macro expansion ignores `{` and any tokens following
--> $DIR/issue-118786.rs:7:34
|
LL | macro_rules! $macro_name {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/issue-30007.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
macro_rules! t {
() => ( String ; ); //~ ERROR macro expansion ignores token `;`
() => ( String ; ); //~ ERROR macro expansion ignores `;`
}

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/issue-30007.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: macro expansion ignores token `;` and any following
error: macro expansion ignores `;` and any tokens following
--> $DIR/issue-30007.rs:2:20
|
LL | () => ( String ; );
Expand Down
Loading

0 comments on commit 2480e3b

Please sign in to comment.