Skip to content

Commit cc8e769

Browse files
committed
Show the unexpected token when parsing derive macro output.
1 parent 5e1bf56 commit cc8e769

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

compiler/rustc_expand/src/proc_macro.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,21 @@ impl MultiItemModifier for ProcMacroDerive {
133133
}
134134
}
135135

136-
// fail if there have been errors emitted,
137-
// or if there's any unparsed tokens left.
138-
if parser.token != token::Eof
139-
|| ecx.sess.parse_sess.span_diagnostic.err_count() > error_count_before
140-
{
136+
// check if the entire stream was parsed
137+
if parser.token != token::Eof {
138+
ecx.struct_span_err(
139+
parser.token.span,
140+
&format!(
141+
"expected item, found {}",
142+
rustc_parse::parser::token_descr(&parser.token)
143+
),
144+
)
145+
.span_label(parser.token.span, "expected item")
146+
.emit();
147+
}
148+
149+
// fail if there have been errors emitted
150+
if ecx.sess.parse_sess.span_diagnostic.err_count() > error_count_before {
141151
ecx.struct_span_err(span, "proc-macro derive produced unparseable tokens").emit();
142152
}
143153

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ fn token_descr_opt(token: &Token) -> Option<&'static str> {
394394
})
395395
}
396396

397-
pub(super) fn token_descr(token: &Token) -> String {
397+
pub fn token_descr(token: &Token) -> String {
398398
let token_str = pprust::token_to_string(token);
399399
match token_descr_opt(token) {
400400
Some(prefix) => format!("{} `{}`", prefix, token_str),

src/test/ui/proc-macro/derive-bad.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ struct A; //~ ERROR the name `A` is defined multiple times
1010

1111
#[derive(B)]
1212
//~^ ERROR proc-macro derive produced unparseable tokens
13+
//~| ERROR expected item, found `{`
1314
struct B;
1415

1516
fn main() {}

src/test/ui/proc-macro/derive-bad.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,20 @@ LL | struct A;
2323
|
2424
= note: `A` must be defined only once in the type namespace of this module
2525

26+
error: expected item, found `{`
27+
--> $DIR/derive-bad.rs:11:10
28+
|
29+
LL | #[derive(B)]
30+
| ^ expected item
31+
|
32+
= note: this error originates in the derive macro `B` (in Nightly builds, run with -Z macro-backtrace for more info)
33+
2634
error: proc-macro derive produced unparseable tokens
2735
--> $DIR/derive-bad.rs:11:10
2836
|
2937
LL | #[derive(B)]
3038
| ^
3139

32-
error: aborting due to 4 previous errors
40+
error: aborting due to 5 previous errors
3341

3442
For more information about this error, try `rustc --explain E0428`.

0 commit comments

Comments
 (0)