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.
This commit is contained in:
Nicholas Nethercote 2024-04-17 17:08:58 +10:00
parent df4ca44d3f
commit a201fab208
27 changed files with 39 additions and 38 deletions

View File

@ -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

View File

@ -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,

View File

@ -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;
@ -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());
@ -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,

View File

@ -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) {

View File

@ -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
() => {}

View File

@ -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 {

View File

@ -1,5 +1,5 @@
macro_rules! t {
() => ( String ; ); //~ ERROR macro expansion ignores token `;`
() => ( String ; ); //~ ERROR macro expansion ignores `;`
}
fn main() {

View File

@ -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 ; );

View File

@ -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
};
}

View File

@ -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

View File

@ -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

View File

@ -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 );
@ -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 );
@ -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 );

View File

@ -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
}

View File

@ -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
}

View File

@ -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");

View File

@ -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>`

View File

@ -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,

View File

@ -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
};

View File

@ -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

View File

@ -2,7 +2,7 @@ macro_rules! ignored_item {
() => {
fn foo() {}
fn bar() {}
, //~ ERROR macro expansion ignores token `,`
, //~ ERROR macro expansion ignores `,`
}
}
@ -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!();

View File

@ -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 | ,
@ -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 )

View File

@ -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 {

View File

@ -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

View File

@ -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]
@ -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!"
};
}

View File

@ -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!";
@ -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]

View File

@ -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");

View File

@ -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));
@ -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));