Skip to content

Commit e927137

Browse files
committed
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.
1 parent ad7c621 commit e927137

23 files changed

+39
-36
lines changed

compiler/rustc_expand/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ expand_helper_attribute_name_invalid =
5050
`{$name}` cannot be a name of derive helper attribute
5151
5252
expand_incomplete_parse =
53-
macro expansion ignores token `{$token}` and any following
53+
macro expansion ignores {$descr} and any tokens following
5454
.label = caused by the macro expansion here
5555
.note = the usage of `{$macro_path}!` is likely invalid in {$kind_name} context
5656
.suggestion_add_semi = you might be missing a semicolon here

compiler/rustc_expand/src/errors.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use rustc_session::Limit;
44
use rustc_span::edition::Edition;
55
use rustc_span::symbol::{Ident, MacroRulesNormalizedIdent};
66
use rustc_span::{Span, Symbol};
7-
use std::borrow::Cow;
87

98
#[derive(Diagnostic)]
109
#[diag(expand_expr_repeat_no_syntax_vars)]
@@ -299,7 +298,7 @@ pub(crate) struct UnsupportedKeyValue {
299298
pub(crate) struct IncompleteParse<'a> {
300299
#[primary_span]
301300
pub span: Span,
302-
pub token: Cow<'a, str>,
301+
pub descr: String,
303302
#[label]
304303
pub label_span: Span,
305304
pub macro_path: &'a ast::Path,

compiler/rustc_expand/src/expand.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ use rustc_data_structures::sync::Lrc;
2525
use rustc_errors::PResult;
2626
use rustc_feature::Features;
2727
use rustc_parse::parser::{
28-
AttemptLocalParseRecovery, CommaRecoveryMode, ForceCollect, Parser, RecoverColon, RecoverComma,
28+
token_descr, AttemptLocalParseRecovery, CommaRecoveryMode, ForceCollect, Parser, RecoverColon,
29+
RecoverComma,
2930
};
3031
use rustc_parse::validate_attr;
3132
use rustc_session::lint::builtin::{UNUSED_ATTRIBUTES, UNUSED_DOC_COMMENTS};
@@ -943,7 +944,7 @@ pub fn ensure_complete_parse<'a>(
943944
span: Span,
944945
) {
945946
if parser.token != token::Eof {
946-
let token = pprust::token_to_string(&parser.token);
947+
let descr = token_descr(&parser.token);
947948
// Avoid emitting backtrace info twice.
948949
let def_site_span = parser.token.span.with_ctxt(SyntaxContext::root());
949950

@@ -957,7 +958,7 @@ pub fn ensure_complete_parse<'a>(
957958

958959
parser.sess.emit_err(IncompleteParse {
959960
span: def_site_span,
960-
token,
961+
descr,
961962
label_span: span,
962963
macro_path,
963964
kind_name,

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,10 @@ impl TokenDescription {
370370
}
371371
}
372372

373-
pub(super) fn token_descr(token: &Token) -> String {
373+
/// Provide a description of a token for error messages. In most cases the
374+
/// result is the same as pretty-printing it, but for a few token kinds we can
375+
/// do better.
376+
pub fn token_descr(token: &Token) -> String {
374377
use TokenDescription::*;
375378

376379
let s = pprust::token_to_string(token).to_string();

tests/ui/issues/issue-30007.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
macro_rules! t {
2-
() => ( String ; ); //~ ERROR macro expansion ignores token `;`
2+
() => ( String ; ); //~ ERROR macro expansion ignores `;`
33
}
44

55
fn main() {

tests/ui/issues/issue-30007.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: macro expansion ignores token `;` and any following
1+
error: macro expansion ignores `;` and any tokens following
22
--> $DIR/issue-30007.rs:2:20
33
|
44
LL | () => ( String ; );

tests/ui/macros/issue-54441.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
macro_rules! m {
22
() => {
3-
let //~ ERROR macro expansion ignores token `let` and any following
3+
let //~ ERROR macro expansion ignores keyword `let` and any tokens following
44
};
55
}
66

tests/ui/macros/issue-54441.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: macro expansion ignores token `let` and any following
1+
error: macro expansion ignores keyword `let` and any tokens following
22
--> $DIR/issue-54441.rs:3:9
33
|
44
LL | let

tests/ui/macros/macro-context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// (typeof used because it's surprisingly hard to find an unparsed token after a stmt)
22
macro_rules! m {
33
() => ( i ; typeof ); //~ ERROR expected expression, found reserved keyword `typeof`
4-
//~| ERROR macro expansion ignores token `typeof`
5-
//~| ERROR macro expansion ignores token `;`
6-
//~| ERROR macro expansion ignores token `;`
4+
//~| ERROR macro expansion ignores reserved keyword `typeof`
5+
//~| ERROR macro expansion ignores `;`
6+
//~| ERROR macro expansion ignores `;`
77
//~| ERROR cannot find type `i` in this scope
88
//~| ERROR cannot find value `i` in this scope
99
//~| WARN trailing semicolon in macro

tests/ui/macros/macro-context.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: macro expansion ignores token `;` and any following
1+
error: macro expansion ignores `;` and any tokens following
22
--> $DIR/macro-context.rs:3:15
33
|
44
LL | () => ( i ; typeof );
@@ -9,7 +9,7 @@ LL | let a: m!();
99
|
1010
= note: the usage of `m!` is likely invalid in type context
1111

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

23-
error: macro expansion ignores token `;` and any following
23+
error: macro expansion ignores `;` and any tokens following
2424
--> $DIR/macro-context.rs:3:15
2525
|
2626
LL | () => ( i ; typeof );

0 commit comments

Comments
 (0)