Skip to content

Error when proc macro derive output doesn't fully parse #87316

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 14 additions & 17 deletions compiler/rustc_expand/src/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use rustc_ast::tokenstream::{CanSynthesizeMissingTokens, TokenStream, TokenTree}
use rustc_data_structures::sync::Lrc;
use rustc_errors::ErrorReported;
use rustc_parse::nt_to_tokenstream;
use rustc_parse::parser::ForceCollect;
use rustc_span::{Span, DUMMY_SP};

const EXEC_STRATEGY: pm::bridge::server::SameThread = pm::bridge::server::SameThread;
Expand Down Expand Up @@ -114,24 +113,22 @@ impl MultiItemModifier for ProcMacroDerive {
let error_count_before = ecx.sess.parse_sess.span_diagnostic.err_count();
let mut parser =
rustc_parse::stream_to_parser(&ecx.sess.parse_sess, stream, Some("proc-macro derive"));
let mut items = vec![];

loop {
match parser.parse_item(ForceCollect::No) {
Ok(None) => break,
Ok(Some(item)) => {
if is_stmt {
items.push(Annotatable::Stmt(P(ecx.stmt_item(span, item))));
} else {
items.push(Annotatable::Item(item));
}
}
Err(mut err) => {
err.emit();
break;
let items = parser
.parse_items(&token::Eof)
.unwrap_or_else(|mut e| {
e.emit();
Vec::new()
})
.into_iter()
.map(|item| {
if is_stmt {
Annotatable::Stmt(P(ecx.stmt_item(span, item)))
} else {
Annotatable::Item(item)
}
}
}
})
.collect();

// fail if there have been errors emitted
if ecx.sess.parse_sess.span_diagnostic.err_count() > error_count_before {
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ impl<'a> Parser<'a> {
) -> PResult<'a, (Vec<Attribute>, Vec<P<Item>>, Span)> {
let lo = self.token.span;
let attrs = self.parse_inner_attributes()?;
let items = self.parse_items(term)?;
Ok((attrs, items, lo.to(self.prev_token.span)))
}

pub fn parse_items(&mut self, term: &TokenKind) -> PResult<'a, Vec<P<Item>>> {
let mut items = vec![];
while let Some(item) = self.parse_item(ForceCollect::No)? {
items.push(item);
Expand All @@ -71,7 +75,7 @@ impl<'a> Parser<'a> {
}
}

Ok((attrs, items, lo.to(self.prev_token.span)))
Ok(items)
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/proc-macro/auxiliary/derive-bad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ use proc_macro::TokenStream;
pub fn derive_a(_input: TokenStream) -> TokenStream {
"struct A { inner }".parse().unwrap()
}

#[proc_macro_derive(B)]
pub fn derive_b(_input: TokenStream) -> TokenStream {
"const _: () = (); { HEY! }".parse().unwrap()
}
5 changes: 5 additions & 0 deletions src/test/ui/proc-macro/derive-bad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ extern crate derive_bad;
//~| ERROR expected `:`, found `}`
struct A; //~ ERROR the name `A` is defined multiple times

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

fn main() {}
16 changes: 15 additions & 1 deletion src/test/ui/proc-macro/derive-bad.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ LL | struct A;
|
= note: `A` must be defined only once in the type namespace of this module

error: aborting due to 3 previous errors
error: expected item, found `{`
--> $DIR/derive-bad.rs:11:10
|
LL | #[derive(B)]
| ^ expected item
|
= note: this error originates in the derive macro `B` (in Nightly builds, run with -Z macro-backtrace for more info)

error: proc-macro derive produced unparseable tokens
--> $DIR/derive-bad.rs:11:10
|
LL | #[derive(B)]
| ^

error: aborting due to 5 previous errors

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