-
Notifications
You must be signed in to change notification settings - Fork 13k
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
macros: fix inert attributes from proc_macro_derives
with #![feature(proc_macro)]
#39572
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,10 +27,10 @@ use syntax::ext::base::{NormalTT, Resolver as SyntaxResolver, SyntaxExtension}; | |
use syntax::ext::expand::{Expansion, mark_tts}; | ||
use syntax::ext::hygiene::Mark; | ||
use syntax::ext::tt::macro_rules; | ||
use syntax::feature_gate::{emit_feature_err, GateIssue, is_builtin_attr}; | ||
use syntax::feature_gate::{self, emit_feature_err, GateIssue}; | ||
use syntax::fold::{self, Folder}; | ||
use syntax::ptr::P; | ||
use syntax::symbol::keywords; | ||
use syntax::symbol::{Symbol, keywords}; | ||
use syntax::util::lev_distance::find_best_match_for_name; | ||
use syntax::visit::Visitor; | ||
use syntax_pos::{Span, DUMMY_SP}; | ||
|
@@ -130,12 +130,16 @@ impl<'a> base::Resolver for Resolver<'a> { | |
self.whitelisted_legacy_custom_derives.contains(&name) | ||
} | ||
|
||
fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion) { | ||
fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion, derives: &[Mark]) { | ||
let invocation = self.invocations[&mark]; | ||
self.collect_def_ids(invocation, expansion); | ||
|
||
self.current_module = invocation.module.get(); | ||
self.current_module.unresolved_invocations.borrow_mut().remove(&mark); | ||
self.current_module.unresolved_invocations.borrow_mut().extend(derives); | ||
for &derive in derives { | ||
self.invocations.insert(derive, invocation); | ||
} | ||
let mut visitor = BuildReducedGraphVisitor { | ||
resolver: self, | ||
legacy_scope: LegacyScope::Invocation(invocation), | ||
|
@@ -172,7 +176,9 @@ impl<'a> base::Resolver for Resolver<'a> { | |
ImportResolver { resolver: self }.resolve_imports() | ||
} | ||
|
||
fn find_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>) -> Option<ast::Attribute> { | ||
// Resolves attribute and derive legacy macros from `#![plugin(..)]`. | ||
fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>) | ||
-> Option<ast::Attribute> { | ||
for i in 0..attrs.len() { | ||
match self.builtin_macros.get(&attrs[i].name()).cloned() { | ||
Some(binding) => match *binding.get_macro(self) { | ||
|
@@ -183,11 +189,50 @@ impl<'a> base::Resolver for Resolver<'a> { | |
}, | ||
None => {} | ||
} | ||
} | ||
|
||
if self.proc_macro_enabled && !is_builtin_attr(&attrs[i]) { | ||
return Some(attrs.remove(i)); | ||
// Check for legacy derives | ||
for i in 0..attrs.len() { | ||
if attrs[i].name() == "derive" { | ||
let mut traits = match attrs[i].meta_item_list() { | ||
Some(traits) if !traits.is_empty() => traits.to_owned(), | ||
_ => continue, | ||
}; | ||
|
||
for j in 0..traits.len() { | ||
let legacy_name = Symbol::intern(&match traits[j].word() { | ||
Some(..) => format!("derive_{}", traits[j].name().unwrap()), | ||
None => continue, | ||
}); | ||
if !self.builtin_macros.contains_key(&legacy_name) { | ||
continue | ||
} | ||
let span = traits.remove(j).span; | ||
self.gate_legacy_custom_derive(legacy_name, span); | ||
if traits.is_empty() { | ||
attrs.remove(i); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this attribute be marked as used, or does that happen elsewhere? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It gets marked as used during expansion (here). |
||
} else { | ||
attrs[i].value = ast::MetaItem { | ||
name: attrs[i].name(), | ||
span: attrs[i].span, | ||
node: ast::MetaItemKind::List(traits), | ||
}; | ||
} | ||
return Some(ast::Attribute { | ||
value: ast::MetaItem { | ||
name: legacy_name, | ||
span: span, | ||
node: ast::MetaItemKind::Word, | ||
}, | ||
id: attr::mk_attr_id(), | ||
style: ast::AttrStyle::Outer, | ||
is_sugared_doc: false, | ||
span: span, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
None | ||
} | ||
|
||
|
@@ -236,7 +281,7 @@ impl<'a> base::Resolver for Resolver<'a> { | |
Ok(binding) => Ok(binding.get_macro(self)), | ||
Err(Determinacy::Undetermined) if !force => return Err(Determinacy::Undetermined), | ||
_ => { | ||
let msg = format!("macro undefined: '{}!'", name); | ||
let msg = format!("macro undefined: `{}`", name); | ||
let mut err = self.session.struct_span_err(span, &msg); | ||
self.suggest_macro_name(&name.as_str(), &mut err); | ||
err.emit(); | ||
|
@@ -251,13 +296,6 @@ impl<'a> base::Resolver for Resolver<'a> { | |
result | ||
} | ||
|
||
fn resolve_builtin_macro(&mut self, tname: Name) -> Result<Rc<SyntaxExtension>, Determinacy> { | ||
match self.builtin_macros.get(&tname).cloned() { | ||
Some(binding) => Ok(binding.get_macro(self)), | ||
None => Err(Determinacy::Undetermined), | ||
} | ||
} | ||
|
||
fn resolve_derive_macro(&mut self, scope: Mark, path: &ast::Path, force: bool) | ||
-> Result<Rc<SyntaxExtension>, Determinacy> { | ||
let ast::Path { span, .. } = *path; | ||
|
@@ -540,4 +578,14 @@ impl<'a> Resolver<'a> { | |
`use {}::{};`", crate_name, name)) | ||
.emit(); | ||
} | ||
|
||
fn gate_legacy_custom_derive(&mut self, name: Symbol, span: Span) { | ||
if !self.session.features.borrow().custom_derive { | ||
let sess = &self.session.parse_sess; | ||
let explain = feature_gate::EXPLAIN_CUSTOM_DERIVE; | ||
emit_feature_err(sess, "custom_derive", span, GateIssue::Language, explain); | ||
} else if !self.is_whitelisted_legacy_custom_derive(name) { | ||
self.session.span_warn(span, feature_gate::EXPLAIN_DEPR_CUSTOM_DERIVE); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason you can't just store the ProcMacroDerive struct without boxing? Seems a waste to have two separate Vecs for the same thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could, but it would require moving more code from
libsyntax_ext
intolibsyntax
. Still might be worth it, but probably best for another PR (perf. impact is negligible).