Skip to content

Fix "new trace_macros doesn't work if there's an error during expansion" #44088

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

Merged
merged 6 commits into from
Sep 17, 2017
Merged
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
13 changes: 13 additions & 0 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
"consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
suggested_limit));
err.emit();
self.cx.trace_macros_diag();
panic!(FatalError);
}

Expand Down Expand Up @@ -439,11 +440,13 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
}
ProcMacroDerive(..) | BuiltinDerive(..) => {
self.cx.span_err(attr.span, &format!("`{}` is a derive mode", attr.path));
self.cx.trace_macros_diag();
kind.dummy(attr.span)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need self.cx.trace_macros_diag(); when we're not panicking immediately after.

}
_ => {
let msg = &format!("macro `{}` may not be used in attributes", attr.path);
self.cx.span_err(attr.span, msg);
self.cx.trace_macros_diag();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this here? This error shouldn't abort expansion.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The order of the trace notes is unstable otherwise.

kind.dummy(attr.span)
}
}
Expand Down Expand Up @@ -482,6 +485,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
if let Err(msg) = validate_and_set_expn_info(def_span.map(|(_, s)| s),
false, false) {
self.cx.span_err(path.span, &msg);
self.cx.trace_macros_diag();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this, and the most of the others in this file.
We use dummy expansions (kind.dummy(span)) so that we don't have to abort.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

return kind.dummy(span);
}
kind.make_from(expand.expand(self.cx, span, mac.node.stream()))
Expand All @@ -497,6 +501,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
allow_internal_unstable,
allow_internal_unsafe) {
self.cx.span_err(path.span, &msg);
self.cx.trace_macros_diag();
return kind.dummy(span);
}
kind.make_from(expander.expand(self.cx, span, mac.node.stream()))
Expand All @@ -506,6 +511,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
if ident.name == keywords::Invalid.name() {
self.cx.span_err(path.span,
&format!("macro {}! expects an ident argument", path));
self.cx.trace_macros_diag();
return kind.dummy(span);
};

Expand All @@ -526,11 +532,13 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
MultiDecorator(..) | MultiModifier(..) | AttrProcMacro(..) => {
self.cx.span_err(path.span,
&format!("`{}` can only be used in attributes", path));
self.cx.trace_macros_diag();
return kind.dummy(span);
}

ProcMacroDerive(..) | BuiltinDerive(..) => {
self.cx.span_err(path.span, &format!("`{}` is a derive mode", path));
self.cx.trace_macros_diag();
return kind.dummy(span);
}

Expand All @@ -539,6 +547,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
let msg =
format!("macro {}! expects no ident argument, given '{}'", path, ident);
self.cx.span_err(path.span, &msg);
self.cx.trace_macros_diag();
return kind.dummy(span);
}

Expand All @@ -564,6 +573,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
let msg = format!("non-{kind} macro in {kind} position: {name}",
name = path.segments[0].identifier.name, kind = kind.name());
self.cx.span_err(path.span, &msg);
self.cx.trace_macros_diag();
kind.dummy(span)
})
}
Expand Down Expand Up @@ -617,6 +627,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
_ => {
let msg = &format!("macro `{}` may not be used for derive attributes", attr.path);
self.cx.span_err(span, msg);
self.cx.trace_macros_diag();
kind.dummy(span)
}
}
Expand Down Expand Up @@ -691,6 +702,7 @@ impl<'a> Parser<'a> {
of `{}!` is likely invalid in {} context",
macro_path, kind_name);
err.span_note(span, &msg).emit();
self.cx.trace_macros_diag();
}
}
}
Expand Down Expand Up @@ -739,6 +751,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
if !traits.is_empty() &&
(kind == ExpansionKind::TraitItems || kind == ExpansionKind::ImplItems) {
self.cx.span_err(traits[0].span, "`derive` can be only be applied to items");
self.cx.trace_macros_diag();
return kind.expect_from_annotatables(::std::iter::once(item));
}
self.collect(kind, InvocationKind::Attr { attr: attr, traits: traits, item: item })
Expand Down