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
Next Next commit
Tweak expand_incomplete_parse warning.
By using `token_descr`, as is done for many other errors, we can get
slightly better descriptions in error messages, e.g.
"macro expansion ignores token `let` and any following" becomes
"macro expansion ignores keyword `let` and any tokens following".

This will be more important once invisible delimiters start being
mentioned in error messages -- without this commit, that leads to error
messages such as "error at ``" because invisible delimiters are
pretty printed as an empty string.
  • Loading branch information
nnethercote committed Oct 28, 2024
commit a201fab20881499d79e5694ee8f195ce50c5b724
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
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/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
2 changes: 1 addition & 1 deletion tests/ui/macros/issue-54441.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
macro_rules! m {
() => {
let //~ ERROR macro expansion ignores token `let` and any following
let //~ ERROR macro expansion ignores keyword `let` and any tokens following
};
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/issue-54441.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: macro expansion ignores token `let` and any following
error: macro expansion ignores keyword `let` and any tokens following
--> $DIR/issue-54441.rs:3:9
|
LL | let
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/macros/macro-context.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// (typeof used because it's surprisingly hard to find an unparsed token after a stmt)
macro_rules! m {
() => ( i ; typeof ); //~ ERROR expected expression, found reserved keyword `typeof`
//~| ERROR macro expansion ignores token `typeof`
//~| ERROR macro expansion ignores token `;`
//~| ERROR macro expansion ignores token `;`
//~| ERROR macro expansion ignores reserved keyword `typeof`
//~| ERROR macro expansion ignores `;`
//~| ERROR macro expansion ignores `;`
//~| ERROR cannot find type `i` in this scope
//~| ERROR cannot find value `i` in this scope
//~| WARN trailing semicolon in macro
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/macros/macro-context.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/macro-context.rs:3:15
|
LL | () => ( i ; typeof );
Expand All @@ -9,7 +9,7 @@ LL | let a: m!();
|
= note: the usage of `m!` is likely invalid in type context

error: macro expansion ignores token `typeof` and any following
error: macro expansion ignores reserved keyword `typeof` and any tokens following
--> $DIR/macro-context.rs:3:17
|
LL | () => ( i ; typeof );
Expand All @@ -20,7 +20,7 @@ LL | let i = m!();
|
= note: the usage of `m!` is likely invalid in expression context

error: macro expansion ignores token `;` and any following
error: macro expansion ignores `;` and any tokens following
--> $DIR/macro-context.rs:3:15
|
LL | () => ( i ; typeof );
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/macro-in-expression-context.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ macro_rules! foo {
//~| NOTE `#[warn(semicolon_in_expressions_from_macros)]` on by default
assert_eq!("B", "B");
}
//~^^ ERROR macro expansion ignores token `assert_eq` and any following
//~^^ ERROR macro expansion ignores `assert_eq` and any tokens following
//~| NOTE the usage of `foo!` is likely invalid in expression context
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/macro-in-expression-context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ macro_rules! foo {
//~| NOTE `#[warn(semicolon_in_expressions_from_macros)]` on by default
assert_eq!("B", "B");
}
//~^^ ERROR macro expansion ignores token `assert_eq` and any following
//~^^ ERROR macro expansion ignores `assert_eq` and any tokens following
//~| NOTE the usage of `foo!` is likely invalid in expression context
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/macro-in-expression-context.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: macro expansion ignores token `assert_eq` and any following
error: macro expansion ignores `assert_eq` and any tokens following
--> $DIR/macro-in-expression-context.rs:12:9
|
LL | assert_eq!("B", "B");
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/syntax-error-recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ macro_rules! values {
};
}
//~^^^^^ ERROR expected one of `(`, `,`, `=`, `{`, or `}`, found type `(String)`
//~| ERROR macro expansion ignores token `(String)` and any following
//~| ERROR macro expansion ignores type `(String)` and any tokens following

values!(STRING(1) as (String) => cfg(test),);
//~^ ERROR expected one of `!` or `::`, found `<eof>`
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/syntax-error-recovery.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ LL | values!(STRING(1) as (String) => cfg(test),);
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
= note: this error originates in the macro `values` (in Nightly builds, run with -Z macro-backtrace for more info)

error: macro expansion ignores token `(String)` and any following
error: macro expansion ignores type `(String)` and any tokens following
--> $DIR/syntax-error-recovery.rs:7:26
|
LL | $token $($inner)? = $value,
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/parser/macro/macro-expand-to-match-arm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
macro_rules! arm {
($pattern:pat => $block:block) => {
$pattern => $block
//~^ ERROR macro expansion ignores token `=>` and any following
//~^ ERROR macro expansion ignores `=>` and any tokens following
//~| NOTE the usage of `arm!` is likely invalid in pattern context
//~| NOTE macros cannot expand to match arms
};
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/parser/macro/macro-expand-to-match-arm.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/macro-expand-to-match-arm.rs:3:18
|
LL | $pattern => $block
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/parser/macro/macro-incomplete-parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ macro_rules! ignored_item {
() => {
fn foo() {}
fn bar() {}
, //~ ERROR macro expansion ignores token `,`
, //~ ERROR macro expansion ignores `,`
}
}

Expand All @@ -13,7 +13,7 @@ macro_rules! ignored_expr {
}

macro_rules! ignored_pat {
() => ( 1, 2 ) //~ ERROR macro expansion ignores token `,`
() => ( 1, 2 ) //~ ERROR macro expansion ignores `,`
}

ignored_item!();
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/parser/macro/macro-incomplete-parse.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/macro-incomplete-parse.rs:5:9
|
LL | ,
Expand All @@ -20,7 +20,7 @@ LL | ignored_expr!();
|
= note: this error originates in the macro `ignored_expr` (in Nightly builds, run with -Z macro-backtrace for more info)

error: macro expansion ignores token `,` and any following
error: macro expansion ignores `,` and any tokens following
--> $DIR/macro-incomplete-parse.rs:16:14
|
LL | () => ( 1, 2 )
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/parser/macro/trait-non-item-macros.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
macro_rules! bah {
($a:expr) => {
$a
}; //~^ ERROR macro expansion ignores token `2` and any following
}; //~^ ERROR macro expansion ignores expression `2` and any tokens following
}

trait Bar {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/parser/macro/trait-non-item-macros.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: macro expansion ignores token `2` and any following
error: macro expansion ignores expression `2` and any tokens following
--> $DIR/trait-non-item-macros.rs:3:9
|
LL | $a
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/proc-macro/attr-invalid-exprs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
//~^ ERROR expected expression, found end of macro arguments

let _ = #[duplicate] "Hello, world!";
//~^ ERROR macro expansion ignores token `,` and any following
//~^ ERROR macro expansion ignores `,` and any tokens following

let _ = {
#[no_output]
Expand All @@ -22,7 +22,7 @@ fn main() {

let _ = {
#[duplicate]
//~^ ERROR macro expansion ignores token `,` and any following
//~^ ERROR macro expansion ignores `,` and any tokens following
"Hello, world!"
};
}
4 changes: 2 additions & 2 deletions tests/ui/proc-macro/attr-invalid-exprs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error: expected expression, found end of macro arguments
LL | let _ = #[no_output] "Hello, world!";
| ^^^^^^^^^^^^

error: macro expansion ignores token `,` and any following
error: macro expansion ignores `,` and any tokens following
--> $DIR/attr-invalid-exprs.rs:15:13
|
LL | let _ = #[duplicate] "Hello, world!";
Expand All @@ -16,7 +16,7 @@ help: you might be missing a semicolon here
LL | let _ = #[duplicate]; "Hello, world!";
| +

error: macro expansion ignores token `,` and any following
error: macro expansion ignores `,` and any tokens following
--> $DIR/attr-invalid-exprs.rs:24:9
|
LL | #[duplicate]
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/proc-macro/expand-expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ expand_expr_fail!(echo_pm!($)); //~ ERROR: expected expression, found `$`

// We get errors reported and recover during macro expansion if the macro
// doesn't produce a valid expression.
expand_expr_is!("string", echo_tts!("string"; hello)); //~ ERROR: macro expansion ignores token `hello` and any following
expand_expr_is!("string", echo_pm!("string"; hello)); //~ ERROR: macro expansion ignores token `;` and any following
expand_expr_is!("string", echo_tts!("string"; hello)); //~ ERROR: macro expansion ignores `hello` and any tokens following
expand_expr_is!("string", echo_pm!("string"; hello)); //~ ERROR: macro expansion ignores `;` and any tokens following

// For now, fail if a non-literal expression is expanded.
expand_expr_fail!(arbitrary_expression() + "etc");
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/proc-macro/expand-expr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ error: expected expression, found `$`
LL | expand_expr_fail!(echo_pm!($));
| ^ expected expression

error: macro expansion ignores token `hello` and any following
error: macro expansion ignores `hello` and any tokens following
--> $DIR/expand-expr.rs:117:47
|
LL | expand_expr_is!("string", echo_tts!("string"; hello));
Expand All @@ -34,7 +34,7 @@ help: you might be missing a semicolon here
LL | expand_expr_is!("string", echo_tts!("string"; hello););
| +

error: macro expansion ignores token `;` and any following
error: macro expansion ignores `;` and any tokens following
--> $DIR/expand-expr.rs:118:44
|
LL | expand_expr_is!("string", echo_pm!("string"; hello));
Expand Down