Allow using inert attributes from proc_macro_derives with #![feature(proc_macro)].
This commit is contained in:
@@ -616,10 +616,9 @@ impl<'a> CrateLoader<'a> {
|
|||||||
trait_name: &str,
|
trait_name: &str,
|
||||||
expand: fn(TokenStream) -> TokenStream,
|
expand: fn(TokenStream) -> TokenStream,
|
||||||
attributes: &[&'static str]) {
|
attributes: &[&'static str]) {
|
||||||
let attrs = attributes.iter().cloned().map(Symbol::intern).collect();
|
let attrs = attributes.iter().cloned().map(Symbol::intern).collect::<Vec<_>>();
|
||||||
let derive = SyntaxExtension::ProcMacroDerive(
|
let derive = ProcMacroDerive::new(expand, attrs.clone());
|
||||||
Box::new(ProcMacroDerive::new(expand, attrs))
|
let derive = SyntaxExtension::ProcMacroDerive(Box::new(derive), attrs);
|
||||||
);
|
|
||||||
self.0.push((Symbol::intern(trait_name), Rc::new(derive)));
|
self.0.push((Symbol::intern(trait_name), Rc::new(derive)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ use syntax::ext::base::{NormalTT, Resolver as SyntaxResolver, SyntaxExtension};
|
|||||||
use syntax::ext::expand::{Expansion, mark_tts};
|
use syntax::ext::expand::{Expansion, mark_tts};
|
||||||
use syntax::ext::hygiene::Mark;
|
use syntax::ext::hygiene::Mark;
|
||||||
use syntax::ext::tt::macro_rules;
|
use syntax::ext::tt::macro_rules;
|
||||||
use syntax::feature_gate::{self, emit_feature_err, GateIssue, is_builtin_attr};
|
use syntax::feature_gate::{self, emit_feature_err, GateIssue};
|
||||||
use syntax::fold::{self, Folder};
|
use syntax::fold::{self, Folder};
|
||||||
use syntax::ptr::P;
|
use syntax::ptr::P;
|
||||||
use syntax::symbol::{Symbol, keywords};
|
use syntax::symbol::{Symbol, keywords};
|
||||||
@@ -130,12 +130,16 @@ impl<'a> base::Resolver for Resolver<'a> {
|
|||||||
self.whitelisted_legacy_custom_derives.contains(&name)
|
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];
|
let invocation = self.invocations[&mark];
|
||||||
self.collect_def_ids(invocation, expansion);
|
self.collect_def_ids(invocation, expansion);
|
||||||
|
|
||||||
self.current_module = invocation.module.get();
|
self.current_module = invocation.module.get();
|
||||||
self.current_module.unresolved_invocations.borrow_mut().remove(&mark);
|
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 {
|
let mut visitor = BuildReducedGraphVisitor {
|
||||||
resolver: self,
|
resolver: self,
|
||||||
legacy_scope: LegacyScope::Invocation(invocation),
|
legacy_scope: LegacyScope::Invocation(invocation),
|
||||||
@@ -172,7 +176,9 @@ impl<'a> base::Resolver for Resolver<'a> {
|
|||||||
ImportResolver { resolver: self }.resolve_imports()
|
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() {
|
for i in 0..attrs.len() {
|
||||||
match self.builtin_macros.get(&attrs[i].name()).cloned() {
|
match self.builtin_macros.get(&attrs[i].name()).cloned() {
|
||||||
Some(binding) => match *binding.get_macro(self) {
|
Some(binding) => match *binding.get_macro(self) {
|
||||||
@@ -183,10 +189,6 @@ impl<'a> base::Resolver for Resolver<'a> {
|
|||||||
},
|
},
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.proc_macro_enabled && !is_builtin_attr(&attrs[i]) {
|
|
||||||
return Some(attrs.remove(i));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for legacy derives
|
// Check for legacy derives
|
||||||
@@ -212,7 +214,7 @@ impl<'a> base::Resolver for Resolver<'a> {
|
|||||||
} else {
|
} else {
|
||||||
attrs[i].value = ast::MetaItem {
|
attrs[i].value = ast::MetaItem {
|
||||||
name: attrs[i].name(),
|
name: attrs[i].name(),
|
||||||
span: span,
|
span: attrs[i].span,
|
||||||
node: ast::MetaItemKind::List(traits),
|
node: ast::MetaItemKind::List(traits),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -279,7 +281,7 @@ impl<'a> base::Resolver for Resolver<'a> {
|
|||||||
Ok(binding) => Ok(binding.get_macro(self)),
|
Ok(binding) => Ok(binding.get_macro(self)),
|
||||||
Err(Determinacy::Undetermined) if !force => return Err(Determinacy::Undetermined),
|
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);
|
let mut err = self.session.struct_span_err(span, &msg);
|
||||||
self.suggest_macro_name(&name.as_str(), &mut err);
|
self.suggest_macro_name(&name.as_str(), &mut err);
|
||||||
err.emit();
|
err.emit();
|
||||||
@@ -294,13 +296,6 @@ impl<'a> base::Resolver for Resolver<'a> {
|
|||||||
result
|
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)
|
fn resolve_derive_macro(&mut self, scope: Mark, path: &ast::Path, force: bool)
|
||||||
-> Result<Rc<SyntaxExtension>, Determinacy> {
|
-> Result<Rc<SyntaxExtension>, Determinacy> {
|
||||||
let ast::Path { span, .. } = *path;
|
let ast::Path { span, .. } = *path;
|
||||||
|
|||||||
@@ -514,7 +514,7 @@ pub enum SyntaxExtension {
|
|||||||
/// The input is the annotated item.
|
/// The input is the annotated item.
|
||||||
/// Allows generating code to implement a Trait for a given struct
|
/// Allows generating code to implement a Trait for a given struct
|
||||||
/// or enum item.
|
/// or enum item.
|
||||||
ProcMacroDerive(Box<MultiItemModifier>),
|
ProcMacroDerive(Box<MultiItemModifier>, Vec<Symbol> /* inert attribute names */),
|
||||||
|
|
||||||
/// An attribute-like procedural macro that derives a builtin trait.
|
/// An attribute-like procedural macro that derives a builtin trait.
|
||||||
BuiltinDerive(BuiltinDeriveFn),
|
BuiltinDerive(BuiltinDeriveFn),
|
||||||
@@ -528,15 +528,15 @@ pub trait Resolver {
|
|||||||
fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item>;
|
fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item>;
|
||||||
fn is_whitelisted_legacy_custom_derive(&self, name: Name) -> bool;
|
fn is_whitelisted_legacy_custom_derive(&self, name: Name) -> bool;
|
||||||
|
|
||||||
fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion);
|
fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion, derives: &[Mark]);
|
||||||
fn add_ext(&mut self, ident: ast::Ident, ext: Rc<SyntaxExtension>);
|
fn add_ext(&mut self, ident: ast::Ident, ext: Rc<SyntaxExtension>);
|
||||||
fn add_expansions_at_stmt(&mut self, id: ast::NodeId, macros: Vec<Mark>);
|
fn add_expansions_at_stmt(&mut self, id: ast::NodeId, macros: Vec<Mark>);
|
||||||
|
|
||||||
fn resolve_imports(&mut self);
|
fn resolve_imports(&mut self);
|
||||||
fn find_attr_invoc(&mut self, attrs: &mut Vec<Attribute>) -> Option<Attribute>;
|
// Resolves attribute and derive legacy macros from `#![plugin(..)]`.
|
||||||
|
fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<Attribute>) -> Option<Attribute>;
|
||||||
fn resolve_macro(&mut self, scope: Mark, path: &ast::Path, force: bool)
|
fn resolve_macro(&mut self, scope: Mark, path: &ast::Path, force: bool)
|
||||||
-> Result<Rc<SyntaxExtension>, Determinacy>;
|
-> Result<Rc<SyntaxExtension>, Determinacy>;
|
||||||
fn resolve_builtin_macro(&mut self, tname: Name) -> Result<Rc<SyntaxExtension>, Determinacy>;
|
|
||||||
fn resolve_derive_macro(&mut self, scope: Mark, path: &ast::Path, force: bool)
|
fn resolve_derive_macro(&mut self, scope: Mark, path: &ast::Path, force: bool)
|
||||||
-> Result<Rc<SyntaxExtension>, Determinacy>;
|
-> Result<Rc<SyntaxExtension>, Determinacy>;
|
||||||
}
|
}
|
||||||
@@ -555,19 +555,16 @@ impl Resolver for DummyResolver {
|
|||||||
fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item> { item }
|
fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item> { item }
|
||||||
fn is_whitelisted_legacy_custom_derive(&self, _name: Name) -> bool { false }
|
fn is_whitelisted_legacy_custom_derive(&self, _name: Name) -> bool { false }
|
||||||
|
|
||||||
fn visit_expansion(&mut self, _invoc: Mark, _expansion: &Expansion) {}
|
fn visit_expansion(&mut self, _invoc: Mark, _expansion: &Expansion, _derives: &[Mark]) {}
|
||||||
fn add_ext(&mut self, _ident: ast::Ident, _ext: Rc<SyntaxExtension>) {}
|
fn add_ext(&mut self, _ident: ast::Ident, _ext: Rc<SyntaxExtension>) {}
|
||||||
fn add_expansions_at_stmt(&mut self, _id: ast::NodeId, _macros: Vec<Mark>) {}
|
fn add_expansions_at_stmt(&mut self, _id: ast::NodeId, _macros: Vec<Mark>) {}
|
||||||
|
|
||||||
fn resolve_imports(&mut self) {}
|
fn resolve_imports(&mut self) {}
|
||||||
fn find_attr_invoc(&mut self, _attrs: &mut Vec<Attribute>) -> Option<Attribute> { None }
|
fn find_legacy_attr_invoc(&mut self, _attrs: &mut Vec<Attribute>) -> Option<Attribute> { None }
|
||||||
fn resolve_macro(&mut self, _scope: Mark, _path: &ast::Path, _force: bool)
|
fn resolve_macro(&mut self, _scope: Mark, _path: &ast::Path, _force: bool)
|
||||||
-> Result<Rc<SyntaxExtension>, Determinacy> {
|
-> Result<Rc<SyntaxExtension>, Determinacy> {
|
||||||
Err(Determinacy::Determined)
|
Err(Determinacy::Determined)
|
||||||
}
|
}
|
||||||
fn resolve_builtin_macro(&mut self, _tname: Name) -> Result<Rc<SyntaxExtension>, Determinacy> {
|
|
||||||
Err(Determinacy::Determined)
|
|
||||||
}
|
|
||||||
fn resolve_derive_macro(&mut self, _scope: Mark, _path: &ast::Path, _force: bool)
|
fn resolve_derive_macro(&mut self, _scope: Mark, _path: &ast::Path, _force: bool)
|
||||||
-> Result<Rc<SyntaxExtension>, Determinacy> {
|
-> Result<Rc<SyntaxExtension>, Determinacy> {
|
||||||
Err(Determinacy::Determined)
|
Err(Determinacy::Determined)
|
||||||
|
|||||||
@@ -8,124 +8,42 @@
|
|||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
use ast::Name;
|
use attr::HasAttrs;
|
||||||
use attr;
|
use {ast, codemap};
|
||||||
use ast::{self, NestedMetaItem}; use ext::base::{ExtCtxt, SyntaxExtension};
|
use ext::base::ExtCtxt;
|
||||||
use codemap;
|
|
||||||
use ext::build::AstBuilder;
|
use ext::build::AstBuilder;
|
||||||
use symbol::Symbol;
|
use symbol::Symbol;
|
||||||
use syntax_pos::Span;
|
use syntax_pos::Span;
|
||||||
|
|
||||||
pub fn derive_attr_trait<'a>(cx: &mut ExtCtxt, attr: &'a ast::Attribute)
|
pub fn collect_derives(cx: &mut ExtCtxt, attrs: &mut Vec<ast::Attribute>) -> Vec<(Symbol, Span)> {
|
||||||
-> Option<&'a NestedMetaItem> {
|
let mut result = Vec::new();
|
||||||
if attr.name() != "derive" {
|
attrs.retain(|attr| {
|
||||||
return None;
|
|
||||||
}
|
|
||||||
if attr.value_str().is_some() {
|
|
||||||
cx.span_err(attr.span, "unexpected value in `derive`");
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
let traits = attr.meta_item_list().unwrap_or(&[]);
|
|
||||||
|
|
||||||
if traits.is_empty() {
|
|
||||||
cx.span_warn(attr.span, "empty trait list in `derive`");
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
return traits.get(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn verify_derive_attrs(cx: &mut ExtCtxt, attrs: &[ast::Attribute]) {
|
|
||||||
for attr in attrs {
|
|
||||||
if attr.name() != "derive" {
|
if attr.name() != "derive" {
|
||||||
continue;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if attr.value_str().is_some() {
|
if attr.value_str().is_some() {
|
||||||
cx.span_err(attr.span, "unexpected value in `derive`");
|
cx.span_err(attr.span, "unexpected value in `derive`");
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
let traits = attr.meta_item_list().unwrap_or(&[]).to_owned();
|
let traits = attr.meta_item_list().unwrap_or(&[]).to_owned();
|
||||||
|
|
||||||
if traits.is_empty() {
|
if traits.is_empty() {
|
||||||
cx.span_warn(attr.span, "empty trait list in `derive`");
|
cx.span_warn(attr.span, "empty trait list in `derive`");
|
||||||
attr::mark_used(&attr);
|
return false;
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for titem in traits {
|
for titem in traits {
|
||||||
if titem.word().is_none() {
|
if titem.word().is_none() {
|
||||||
cx.span_err(titem.span, "malformed `derive` entry");
|
cx.span_err(titem.span, "malformed `derive` entry");
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
result.push((titem.name().unwrap(), titem.span));
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(PartialEq, Debug, Clone, Copy)]
|
|
||||||
pub enum DeriveType {
|
|
||||||
ProcMacro,
|
|
||||||
Builtin
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DeriveType {
|
|
||||||
// Classify a derive trait name by resolving the macro.
|
|
||||||
pub fn classify(cx: &mut ExtCtxt, tname: Name) -> DeriveType {
|
|
||||||
match cx.resolver.resolve_builtin_macro(tname) {
|
|
||||||
Ok(ext) => match *ext {
|
|
||||||
SyntaxExtension::BuiltinDerive(..) => DeriveType::Builtin,
|
|
||||||
_ => DeriveType::ProcMacro,
|
|
||||||
},
|
|
||||||
Err(_) => DeriveType::ProcMacro,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_derive_attr(cx: &mut ExtCtxt, attrs: &mut Vec<ast::Attribute>,
|
|
||||||
derive_type: DeriveType) -> Option<ast::Attribute> {
|
|
||||||
for i in 0..attrs.len() {
|
|
||||||
if attrs[i].name() != "derive" {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if attrs[i].value_str().is_some() {
|
true
|
||||||
continue;
|
});
|
||||||
}
|
result
|
||||||
|
|
||||||
let mut traits = attrs[i].meta_item_list().unwrap_or(&[]).to_owned();
|
|
||||||
|
|
||||||
// First, weed out malformed #[derive]
|
|
||||||
traits.retain(|titem| titem.word().is_some());
|
|
||||||
|
|
||||||
let mut titem = None;
|
|
||||||
|
|
||||||
// See if we can find a matching trait.
|
|
||||||
for j in 0..traits.len() {
|
|
||||||
let tname = match traits[j].name() {
|
|
||||||
Some(tname) => tname,
|
|
||||||
_ => continue,
|
|
||||||
};
|
|
||||||
|
|
||||||
if DeriveType::classify(cx, tname) == derive_type {
|
|
||||||
titem = Some(traits.remove(j));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we find a trait, remove the trait from the attribute.
|
|
||||||
if let Some(titem) = titem {
|
|
||||||
if traits.len() == 0 {
|
|
||||||
attrs.remove(i);
|
|
||||||
} else {
|
|
||||||
let derive = Symbol::intern("derive");
|
|
||||||
let mitem = cx.meta_list(titem.span, derive, traits);
|
|
||||||
attrs[i] = cx.attribute(titem.span, mitem);
|
|
||||||
}
|
|
||||||
let derive = Symbol::intern("derive");
|
|
||||||
let mitem = cx.meta_list(titem.span, derive, vec![titem]);
|
|
||||||
return Some(cx.attribute(mitem.span, mitem));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return None;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn allow_unstable(cx: &mut ExtCtxt, span: Span, attr_name: &str) -> Span {
|
fn allow_unstable(cx: &mut ExtCtxt, span: Span, attr_name: &str) -> Span {
|
||||||
@@ -142,43 +60,25 @@ fn allow_unstable(cx: &mut ExtCtxt, span: Span, attr_name: &str) -> Span {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_derived_markers(cx: &mut ExtCtxt, attrs: &mut Vec<ast::Attribute>) {
|
pub fn add_derived_markers<T: HasAttrs>(cx: &mut ExtCtxt, traits: &[(Symbol, Span)], item: T) -> T {
|
||||||
if attrs.is_empty() {
|
let span = match traits.get(0) {
|
||||||
return;
|
Some(&(_, span)) => span,
|
||||||
}
|
None => return item,
|
||||||
|
};
|
||||||
|
|
||||||
let titems = attrs.iter().filter(|a| {
|
item.map_attrs(|mut attrs| {
|
||||||
a.name() == "derive"
|
if traits.iter().any(|&(name, _)| name == "PartialEq") &&
|
||||||
}).flat_map(|a| {
|
traits.iter().any(|&(name, _)| name == "Eq") {
|
||||||
a.meta_item_list().unwrap_or(&[]).iter()
|
let span = allow_unstable(cx, span, "derive(PartialEq, Eq)");
|
||||||
}).filter_map(|titem| {
|
let meta = cx.meta_word(span, Symbol::intern("structural_match"));
|
||||||
titem.name()
|
attrs.push(cx.attribute(span, meta));
|
||||||
}).collect::<Vec<_>>();
|
}
|
||||||
|
if traits.iter().any(|&(name, _)| name == "Copy") &&
|
||||||
let span = attrs[0].span;
|
traits.iter().any(|&(name, _)| name == "Clone") {
|
||||||
|
let span = allow_unstable(cx, span, "derive(Copy, Clone)");
|
||||||
if !attrs.iter().any(|a| a.name() == "structural_match") &&
|
let meta = cx.meta_word(span, Symbol::intern("rustc_copy_clone_marker"));
|
||||||
titems.iter().any(|t| *t == "PartialEq") && titems.iter().any(|t| *t == "Eq") {
|
attrs.push(cx.attribute(span, meta));
|
||||||
let structural_match = Symbol::intern("structural_match");
|
}
|
||||||
let span = allow_unstable(cx, span, "derive(PartialEq, Eq)");
|
attrs
|
||||||
let meta = cx.meta_word(span, structural_match);
|
|
||||||
attrs.push(cx.attribute(span, meta));
|
|
||||||
}
|
|
||||||
|
|
||||||
if !attrs.iter().any(|a| a.name() == "rustc_copy_clone_marker") &&
|
|
||||||
titems.iter().any(|t| *t == "Copy") && titems.iter().any(|t| *t == "Clone") {
|
|
||||||
let structural_match = Symbol::intern("rustc_copy_clone_marker");
|
|
||||||
let span = allow_unstable(cx, span, "derive(Copy, Clone)");
|
|
||||||
let meta = cx.meta_word(span, structural_match);
|
|
||||||
attrs.push(cx.attribute(span, meta));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn find_derive_attr(cx: &mut ExtCtxt, attrs: &mut Vec<ast::Attribute>)
|
|
||||||
-> Option<ast::Attribute> {
|
|
||||||
verify_derive_attrs(cx, attrs);
|
|
||||||
get_derive_attr(cx, attrs, DeriveType::ProcMacro).or_else(|| {
|
|
||||||
add_derived_markers(cx, attrs);
|
|
||||||
get_derive_attr(cx, attrs, DeriveType::Builtin)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,10 +14,10 @@ use attr::{self, HasAttrs};
|
|||||||
use codemap::{ExpnInfo, NameAndSpan, MacroBang, MacroAttribute};
|
use codemap::{ExpnInfo, NameAndSpan, MacroBang, MacroAttribute};
|
||||||
use config::{is_test_or_bench, StripUnconfigured};
|
use config::{is_test_or_bench, StripUnconfigured};
|
||||||
use ext::base::*;
|
use ext::base::*;
|
||||||
use ext::derive::{find_derive_attr, derive_attr_trait};
|
use ext::derive::{add_derived_markers, collect_derives};
|
||||||
use ext::hygiene::Mark;
|
use ext::hygiene::Mark;
|
||||||
use ext::placeholders::{placeholder, PlaceholderExpander};
|
use ext::placeholders::{placeholder, PlaceholderExpander};
|
||||||
use feature_gate::{self, Features};
|
use feature_gate::{self, Features, is_builtin_attr};
|
||||||
use fold;
|
use fold;
|
||||||
use fold::*;
|
use fold::*;
|
||||||
use parse::parser::Parser;
|
use parse::parser::Parser;
|
||||||
@@ -33,6 +33,7 @@ use tokenstream::{TokenTree, TokenStream};
|
|||||||
use util::small_vector::SmallVector;
|
use util::small_vector::SmallVector;
|
||||||
use visit::Visitor;
|
use visit::Visitor;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
@@ -164,11 +165,13 @@ pub enum InvocationKind {
|
|||||||
span: Span,
|
span: Span,
|
||||||
},
|
},
|
||||||
Attr {
|
Attr {
|
||||||
attr: ast::Attribute,
|
attr: Option<ast::Attribute>,
|
||||||
|
traits: Vec<(Symbol, Span)>,
|
||||||
item: Annotatable,
|
item: Annotatable,
|
||||||
},
|
},
|
||||||
Derive {
|
Derive {
|
||||||
attr: ast::Attribute,
|
name: Symbol,
|
||||||
|
span: Span,
|
||||||
item: Annotatable,
|
item: Annotatable,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -177,8 +180,9 @@ impl Invocation {
|
|||||||
fn span(&self) -> Span {
|
fn span(&self) -> Span {
|
||||||
match self.kind {
|
match self.kind {
|
||||||
InvocationKind::Bang { span, .. } => span,
|
InvocationKind::Bang { span, .. } => span,
|
||||||
InvocationKind::Attr { ref attr, .. } => attr.span,
|
InvocationKind::Attr { attr: Some(ref attr), .. } => attr.span,
|
||||||
InvocationKind::Derive { ref attr, .. } => attr.span,
|
InvocationKind::Attr { attr: None, .. } => syntax_pos::DUMMY_SP,
|
||||||
|
InvocationKind::Derive { span, .. } => span,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -227,15 +231,16 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
|
|||||||
let orig_expansion_data = self.cx.current_expansion.clone();
|
let orig_expansion_data = self.cx.current_expansion.clone();
|
||||||
self.cx.current_expansion.depth = 0;
|
self.cx.current_expansion.depth = 0;
|
||||||
|
|
||||||
let (expansion, mut invocations) = self.collect_invocations(expansion);
|
let (expansion, mut invocations) = self.collect_invocations(expansion, &[]);
|
||||||
self.resolve_imports();
|
self.resolve_imports();
|
||||||
invocations.reverse();
|
invocations.reverse();
|
||||||
|
|
||||||
let mut expansions = Vec::new();
|
let mut expansions = Vec::new();
|
||||||
|
let mut derives = HashMap::new();
|
||||||
let mut undetermined_invocations = Vec::new();
|
let mut undetermined_invocations = Vec::new();
|
||||||
let (mut progress, mut force) = (false, !self.monotonic);
|
let (mut progress, mut force) = (false, !self.monotonic);
|
||||||
loop {
|
loop {
|
||||||
let invoc = if let Some(invoc) = invocations.pop() {
|
let mut invoc = if let Some(invoc) = invocations.pop() {
|
||||||
invoc
|
invoc
|
||||||
} else {
|
} else {
|
||||||
self.resolve_imports();
|
self.resolve_imports();
|
||||||
@@ -247,24 +252,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
|
|||||||
|
|
||||||
let scope =
|
let scope =
|
||||||
if self.monotonic { invoc.expansion_data.mark } else { orig_expansion_data.mark };
|
if self.monotonic { invoc.expansion_data.mark } else { orig_expansion_data.mark };
|
||||||
let resolution = match invoc.kind {
|
let ext = match self.resolve_invoc(&mut invoc, scope, force) {
|
||||||
InvocationKind::Bang { ref mac, .. } => {
|
|
||||||
self.cx.resolver.resolve_macro(scope, &mac.node.path, force)
|
|
||||||
}
|
|
||||||
InvocationKind::Attr { ref attr, .. } => {
|
|
||||||
let ident = Ident::with_empty_ctxt(attr.name());
|
|
||||||
let path = ast::Path::from_ident(attr.span, ident);
|
|
||||||
self.cx.resolver.resolve_macro(scope, &path, force)
|
|
||||||
}
|
|
||||||
InvocationKind::Derive { ref attr, .. } => {
|
|
||||||
let titem = derive_attr_trait(self.cx, &attr).unwrap();
|
|
||||||
let tname = titem.name().expect("Expected derive macro name");
|
|
||||||
let ident = Ident::with_empty_ctxt(tname);
|
|
||||||
let path = ast::Path::from_ident(attr.span, ident);
|
|
||||||
self.cx.resolver.resolve_derive_macro(scope, &path, force)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let ext = match resolution {
|
|
||||||
Ok(ext) => Some(ext),
|
Ok(ext) => Some(ext),
|
||||||
Err(Determinacy::Determined) => None,
|
Err(Determinacy::Determined) => None,
|
||||||
Err(Determinacy::Undetermined) => {
|
Err(Determinacy::Undetermined) => {
|
||||||
@@ -278,12 +266,48 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
|
|||||||
self.cx.current_expansion = invoc.expansion_data.clone();
|
self.cx.current_expansion = invoc.expansion_data.clone();
|
||||||
|
|
||||||
self.cx.current_expansion.mark = scope;
|
self.cx.current_expansion.mark = scope;
|
||||||
let expansion = match ext {
|
// FIXME(jseyfried): Refactor out the following logic
|
||||||
Some(ext) => self.expand_invoc(invoc, ext),
|
let (expansion, new_invocations) = if let Some(ext) = ext {
|
||||||
None => invoc.expansion_kind.dummy(invoc.span()),
|
if let Some(ext) = ext {
|
||||||
};
|
let expansion = self.expand_invoc(invoc, ext);
|
||||||
|
self.collect_invocations(expansion, &[])
|
||||||
|
} else if let InvocationKind::Attr { attr: None, traits, item } = invoc.kind {
|
||||||
|
let item = item
|
||||||
|
.map_attrs(|mut attrs| { attrs.retain(|a| a.name() != "derive"); attrs });
|
||||||
|
let item_with_markers =
|
||||||
|
add_derived_markers(&mut self.cx, &traits, item.clone());
|
||||||
|
let derives = derives.entry(invoc.expansion_data.mark).or_insert_with(Vec::new);
|
||||||
|
|
||||||
let (expansion, new_invocations) = self.collect_invocations(expansion);
|
for &(name, span) in &traits {
|
||||||
|
let mark = Mark::fresh();
|
||||||
|
derives.push(mark);
|
||||||
|
let path = ast::Path::from_ident(span, Ident::with_empty_ctxt(name));
|
||||||
|
let item = match self.cx.resolver
|
||||||
|
.resolve_macro(Mark::root(), &path, false) {
|
||||||
|
Ok(ext) => match *ext {
|
||||||
|
SyntaxExtension::BuiltinDerive(..) => item_with_markers.clone(),
|
||||||
|
_ => item.clone(),
|
||||||
|
},
|
||||||
|
_ => item.clone(),
|
||||||
|
};
|
||||||
|
invocations.push(Invocation {
|
||||||
|
kind: InvocationKind::Derive { name: name, span: span, item: item },
|
||||||
|
expansion_kind: invoc.expansion_kind,
|
||||||
|
expansion_data: ExpansionData {
|
||||||
|
mark: mark,
|
||||||
|
..invoc.expansion_data.clone()
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
let expansion = invoc.expansion_kind
|
||||||
|
.expect_from_annotatables(::std::iter::once(item_with_markers));
|
||||||
|
self.collect_invocations(expansion, derives)
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
self.collect_invocations(invoc.expansion_kind.dummy(invoc.span()), &[])
|
||||||
|
};
|
||||||
|
|
||||||
if expansions.len() < depth {
|
if expansions.len() < depth {
|
||||||
expansions.push(Vec::new());
|
expansions.push(Vec::new());
|
||||||
@@ -299,7 +323,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
|
|||||||
let mut placeholder_expander = PlaceholderExpander::new(self.cx, self.monotonic);
|
let mut placeholder_expander = PlaceholderExpander::new(self.cx, self.monotonic);
|
||||||
while let Some(expansions) = expansions.pop() {
|
while let Some(expansions) = expansions.pop() {
|
||||||
for (mark, expansion) in expansions.into_iter().rev() {
|
for (mark, expansion) in expansions.into_iter().rev() {
|
||||||
placeholder_expander.add(mark.as_placeholder_id(), expansion);
|
let derives = derives.remove(&mark).unwrap_or_else(Vec::new);
|
||||||
|
placeholder_expander.add(mark.as_placeholder_id(), expansion, derives);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -314,7 +339,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collect_invocations(&mut self, expansion: Expansion) -> (Expansion, Vec<Invocation>) {
|
fn collect_invocations(&mut self, expansion: Expansion, derives: &[Mark])
|
||||||
|
-> (Expansion, Vec<Invocation>) {
|
||||||
let result = {
|
let result = {
|
||||||
let mut collector = InvocationCollector {
|
let mut collector = InvocationCollector {
|
||||||
cfg: StripUnconfigured {
|
cfg: StripUnconfigured {
|
||||||
@@ -332,13 +358,69 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
|
|||||||
if self.monotonic {
|
if self.monotonic {
|
||||||
let err_count = self.cx.parse_sess.span_diagnostic.err_count();
|
let err_count = self.cx.parse_sess.span_diagnostic.err_count();
|
||||||
let mark = self.cx.current_expansion.mark;
|
let mark = self.cx.current_expansion.mark;
|
||||||
self.cx.resolver.visit_expansion(mark, &result.0);
|
self.cx.resolver.visit_expansion(mark, &result.0, derives);
|
||||||
self.cx.resolve_err_count += self.cx.parse_sess.span_diagnostic.err_count() - err_count;
|
self.cx.resolve_err_count += self.cx.parse_sess.span_diagnostic.err_count() - err_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn resolve_invoc(&mut self, invoc: &mut Invocation, scope: Mark, force: bool)
|
||||||
|
-> Result<Option<Rc<SyntaxExtension>>, Determinacy> {
|
||||||
|
let (attr, traits, item) = match invoc.kind {
|
||||||
|
InvocationKind::Bang { ref mac, .. } => {
|
||||||
|
return self.cx.resolver.resolve_macro(scope, &mac.node.path, force).map(Some);
|
||||||
|
}
|
||||||
|
InvocationKind::Attr { attr: None, .. } => return Ok(None),
|
||||||
|
InvocationKind::Derive { name, span, .. } => {
|
||||||
|
let path = ast::Path::from_ident(span, Ident::with_empty_ctxt(name));
|
||||||
|
return self.cx.resolver.resolve_derive_macro(scope, &path, force).map(Some);
|
||||||
|
}
|
||||||
|
InvocationKind::Attr { ref mut attr, ref traits, ref mut item } => (attr, traits, item),
|
||||||
|
};
|
||||||
|
|
||||||
|
let (attr_name, path) = {
|
||||||
|
let attr = attr.as_ref().unwrap();
|
||||||
|
(attr.name(), ast::Path::from_ident(attr.span, Ident::with_empty_ctxt(attr.name())))
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut determined = true;
|
||||||
|
match self.cx.resolver.resolve_macro(scope, &path, force) {
|
||||||
|
Ok(ext) => return Ok(Some(ext)),
|
||||||
|
Err(Determinacy::Undetermined) => determined = false,
|
||||||
|
Err(Determinacy::Determined) if force => return Err(Determinacy::Determined),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
for &(name, span) in traits {
|
||||||
|
let path = ast::Path::from_ident(span, Ident::with_empty_ctxt(name));
|
||||||
|
match self.cx.resolver.resolve_macro(scope, &path, force) {
|
||||||
|
Ok(ext) => if let SyntaxExtension::ProcMacroDerive(_, ref inert_attrs) = *ext {
|
||||||
|
if inert_attrs.contains(&attr_name) {
|
||||||
|
// FIXME(jseyfried) Avoid `mem::replace` here.
|
||||||
|
let dummy_item = placeholder(ExpansionKind::Items, ast::DUMMY_NODE_ID)
|
||||||
|
.make_items().pop().unwrap();
|
||||||
|
*item = mem::replace(item, Annotatable::Item(dummy_item))
|
||||||
|
.map_attrs(|mut attrs| {
|
||||||
|
let inert_attr = attr.take().unwrap();
|
||||||
|
attr::mark_known(&inert_attr);
|
||||||
|
if self.cx.ecfg.proc_macro_enabled() {
|
||||||
|
*attr = find_attr_invoc(&mut attrs);
|
||||||
|
}
|
||||||
|
attrs.push(inert_attr);
|
||||||
|
attrs
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return Err(Determinacy::Undetermined);
|
||||||
|
},
|
||||||
|
Err(Determinacy::Undetermined) => determined = false,
|
||||||
|
Err(Determinacy::Determined) => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(if determined { Determinacy::Determined } else { Determinacy::Undetermined })
|
||||||
|
}
|
||||||
|
|
||||||
fn expand_invoc(&mut self, invoc: Invocation, ext: Rc<SyntaxExtension>) -> Expansion {
|
fn expand_invoc(&mut self, invoc: Invocation, ext: Rc<SyntaxExtension>) -> Expansion {
|
||||||
match invoc.kind {
|
match invoc.kind {
|
||||||
InvocationKind::Bang { .. } => self.expand_bang_invoc(invoc, ext),
|
InvocationKind::Bang { .. } => self.expand_bang_invoc(invoc, ext),
|
||||||
@@ -350,7 +432,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
|
|||||||
fn expand_attr_invoc(&mut self, invoc: Invocation, ext: Rc<SyntaxExtension>) -> Expansion {
|
fn expand_attr_invoc(&mut self, invoc: Invocation, ext: Rc<SyntaxExtension>) -> Expansion {
|
||||||
let Invocation { expansion_kind: kind, .. } = invoc;
|
let Invocation { expansion_kind: kind, .. } = invoc;
|
||||||
let (attr, item) = match invoc.kind {
|
let (attr, item) = match invoc.kind {
|
||||||
InvocationKind::Attr { attr, item } => (attr, item),
|
InvocationKind::Attr { attr, item, .. } => (attr.unwrap(), item),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -503,64 +585,60 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
|
|||||||
/// Expand a derive invocation. Returns the result of expansion.
|
/// Expand a derive invocation. Returns the result of expansion.
|
||||||
fn expand_derive_invoc(&mut self, invoc: Invocation, ext: Rc<SyntaxExtension>) -> Expansion {
|
fn expand_derive_invoc(&mut self, invoc: Invocation, ext: Rc<SyntaxExtension>) -> Expansion {
|
||||||
let Invocation { expansion_kind: kind, .. } = invoc;
|
let Invocation { expansion_kind: kind, .. } = invoc;
|
||||||
let (attr, item) = match invoc.kind {
|
let (name, span, item) = match invoc.kind {
|
||||||
InvocationKind::Derive { attr, item } => (attr, item),
|
InvocationKind::Derive { name, span, item } => (name, span, item),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
|
|
||||||
attr::mark_used(&attr);
|
let mitem = ast::MetaItem { name: name, span: span, node: ast::MetaItemKind::Word };
|
||||||
let titem = derive_attr_trait(self.cx, &attr).unwrap();
|
let pretty_name = Symbol::intern(&format!("derive({})", name));
|
||||||
let tname = ast::Ident::with_empty_ctxt(titem.name().unwrap());
|
|
||||||
let name = Symbol::intern(&format!("derive({})", tname));
|
|
||||||
let mitem = &attr.value;
|
|
||||||
|
|
||||||
self.cx.bt_push(ExpnInfo {
|
self.cx.bt_push(ExpnInfo {
|
||||||
call_site: attr.span,
|
call_site: span,
|
||||||
callee: NameAndSpan {
|
callee: NameAndSpan {
|
||||||
format: MacroAttribute(attr.name()),
|
format: MacroAttribute(pretty_name),
|
||||||
span: Some(attr.span),
|
span: Some(span),
|
||||||
allow_internal_unstable: false,
|
allow_internal_unstable: false,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
match *ext {
|
match *ext {
|
||||||
SyntaxExtension::ProcMacroDerive(ref ext) => {
|
SyntaxExtension::ProcMacroDerive(ref ext, _) => {
|
||||||
let span = Span {
|
let span = Span {
|
||||||
expn_id: self.cx.codemap().record_expansion(ExpnInfo {
|
expn_id: self.cx.codemap().record_expansion(ExpnInfo {
|
||||||
call_site: mitem.span,
|
call_site: span,
|
||||||
callee: NameAndSpan {
|
callee: NameAndSpan {
|
||||||
format: MacroAttribute(Symbol::intern(&format!("derive({})", tname))),
|
format: MacroAttribute(pretty_name),
|
||||||
span: None,
|
span: None,
|
||||||
allow_internal_unstable: false,
|
allow_internal_unstable: false,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
..mitem.span
|
..span
|
||||||
};
|
};
|
||||||
return kind.expect_from_annotatables(ext.expand(self.cx, span, &mitem, item));
|
return kind.expect_from_annotatables(ext.expand(self.cx, span, &mitem, item));
|
||||||
}
|
}
|
||||||
SyntaxExtension::BuiltinDerive(func) => {
|
SyntaxExtension::BuiltinDerive(func) => {
|
||||||
let span = Span {
|
let span = Span {
|
||||||
expn_id: self.cx.codemap().record_expansion(ExpnInfo {
|
expn_id: self.cx.codemap().record_expansion(ExpnInfo {
|
||||||
call_site: titem.span,
|
call_site: span,
|
||||||
callee: NameAndSpan {
|
callee: NameAndSpan {
|
||||||
format: MacroAttribute(name),
|
format: MacroAttribute(pretty_name),
|
||||||
span: None,
|
span: None,
|
||||||
allow_internal_unstable: true,
|
allow_internal_unstable: true,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
..titem.span
|
..span
|
||||||
};
|
};
|
||||||
let mut items = Vec::new();
|
let mut items = Vec::new();
|
||||||
func(self.cx, span, &mitem, &item, &mut |a| {
|
func(self.cx, span, &mitem, &item, &mut |a| {
|
||||||
items.push(a)
|
items.push(a)
|
||||||
});
|
});
|
||||||
items.insert(0, item);
|
|
||||||
return kind.expect_from_annotatables(items);
|
return kind.expect_from_annotatables(items);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let msg = &format!("macro `{}` may not be used for derive attributes", name);
|
let msg = &format!("macro `{}` may not be used for derive attributes", name);
|
||||||
self.cx.span_err(attr.span, &msg);
|
self.cx.span_err(span, &msg);
|
||||||
kind.dummy(attr.span)
|
kind.dummy(span)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -672,34 +750,40 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
|
|||||||
self.collect(kind, InvocationKind::Bang { mac: mac, ident: None, span: span })
|
self.collect(kind, InvocationKind::Bang { mac: mac, ident: None, span: span })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collect_attr(&mut self, attr: ast::Attribute, item: Annotatable, kind: ExpansionKind)
|
fn collect_attr(&mut self,
|
||||||
|
attr: Option<ast::Attribute>,
|
||||||
|
traits: Vec<(Symbol, Span)>,
|
||||||
|
item: Annotatable,
|
||||||
|
kind: ExpansionKind)
|
||||||
-> Expansion {
|
-> Expansion {
|
||||||
let invoc_kind = if attr.name() == "derive" {
|
if !traits.is_empty() &&
|
||||||
if kind == ExpansionKind::TraitItems || kind == ExpansionKind::ImplItems {
|
(kind == ExpansionKind::TraitItems || kind == ExpansionKind::ImplItems) {
|
||||||
self.cx.span_err(attr.span, "`derive` can be only be applied to items");
|
self.cx.span_err(traits[0].1, "`derive` can be only be applied to items");
|
||||||
return kind.expect_from_annotatables(::std::iter::once(item));
|
return kind.expect_from_annotatables(::std::iter::once(item));
|
||||||
}
|
}
|
||||||
InvocationKind::Derive { attr: attr, item: item }
|
self.collect(kind, InvocationKind::Attr { attr: attr, traits: traits, item: item })
|
||||||
} else {
|
|
||||||
InvocationKind::Attr { attr: attr, item: item }
|
|
||||||
};
|
|
||||||
|
|
||||||
self.collect(kind, invoc_kind)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If `item` is an attr invocation, remove and return the macro attribute.
|
// If `item` is an attr invocation, remove and return the macro attribute.
|
||||||
fn classify_item<T: HasAttrs>(&mut self, mut item: T) -> (T, Option<ast::Attribute>) {
|
fn classify_item<T>(&mut self, mut item: T) -> (Option<ast::Attribute>, Vec<(Symbol, Span)>, T)
|
||||||
let mut attr = None;
|
where T: HasAttrs,
|
||||||
|
{
|
||||||
|
let (mut attr, mut traits) = (None, Vec::new());
|
||||||
|
|
||||||
item = item.map_attrs(|mut attrs| {
|
item = item.map_attrs(|mut attrs| {
|
||||||
attr = self.cx.resolver.find_attr_invoc(&mut attrs).or_else(|| {
|
if let Some(legacy_attr_invoc) = self.cx.resolver.find_legacy_attr_invoc(&mut attrs) {
|
||||||
find_derive_attr(self.cx, &mut attrs)
|
attr = Some(legacy_attr_invoc);
|
||||||
});
|
return attrs;
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.cx.ecfg.proc_macro_enabled() {
|
||||||
|
attr = find_attr_invoc(&mut attrs);
|
||||||
|
}
|
||||||
|
traits = collect_derives(&mut self.cx, &mut attrs);
|
||||||
attrs
|
attrs
|
||||||
});
|
});
|
||||||
|
|
||||||
(item, attr)
|
(attr, traits, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
|
fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
|
||||||
@@ -717,6 +801,16 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn find_attr_invoc(attrs: &mut Vec<ast::Attribute>) -> Option<ast::Attribute> {
|
||||||
|
for i in 0 .. attrs.len() {
|
||||||
|
if !attr::is_known(&attrs[i]) && !is_builtin_attr(&attrs[i]) {
|
||||||
|
return Some(attrs.remove(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
// These are pretty nasty. Ideally, we would keep the tokens around, linked from
|
// These are pretty nasty. Ideally, we would keep the tokens around, linked from
|
||||||
// the AST. However, we don't so we need to create new ones. Since the item might
|
// the AST. However, we don't so we need to create new ones. Since the item might
|
||||||
// have come from a macro expansion (possibly only in part), we can't use the
|
// have come from a macro expansion (possibly only in part), we can't use the
|
||||||
@@ -844,10 +938,10 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
|
|||||||
fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
|
fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
|
||||||
let item = configure!(self, item);
|
let item = configure!(self, item);
|
||||||
|
|
||||||
let (mut item, attr) = self.classify_item(item);
|
let (attr, traits, mut item) = self.classify_item(item);
|
||||||
if let Some(attr) = attr {
|
if attr.is_some() || !traits.is_empty() {
|
||||||
let item = Annotatable::Item(fully_configure!(self, item, noop_fold_item));
|
let item = Annotatable::Item(fully_configure!(self, item, noop_fold_item));
|
||||||
return self.collect_attr(attr, item, ExpansionKind::Items).make_items();
|
return self.collect_attr(attr, traits, item, ExpansionKind::Items).make_items();
|
||||||
}
|
}
|
||||||
|
|
||||||
match item.node {
|
match item.node {
|
||||||
@@ -928,11 +1022,12 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
|
|||||||
fn fold_trait_item(&mut self, item: ast::TraitItem) -> SmallVector<ast::TraitItem> {
|
fn fold_trait_item(&mut self, item: ast::TraitItem) -> SmallVector<ast::TraitItem> {
|
||||||
let item = configure!(self, item);
|
let item = configure!(self, item);
|
||||||
|
|
||||||
let (item, attr) = self.classify_item(item);
|
let (attr, traits, item) = self.classify_item(item);
|
||||||
if let Some(attr) = attr {
|
if attr.is_some() || !traits.is_empty() {
|
||||||
let item =
|
let item =
|
||||||
Annotatable::TraitItem(P(fully_configure!(self, item, noop_fold_trait_item)));
|
Annotatable::TraitItem(P(fully_configure!(self, item, noop_fold_trait_item)));
|
||||||
return self.collect_attr(attr, item, ExpansionKind::TraitItems).make_trait_items()
|
return self.collect_attr(attr, traits, item, ExpansionKind::TraitItems)
|
||||||
|
.make_trait_items()
|
||||||
}
|
}
|
||||||
|
|
||||||
match item.node {
|
match item.node {
|
||||||
@@ -948,10 +1043,11 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
|
|||||||
fn fold_impl_item(&mut self, item: ast::ImplItem) -> SmallVector<ast::ImplItem> {
|
fn fold_impl_item(&mut self, item: ast::ImplItem) -> SmallVector<ast::ImplItem> {
|
||||||
let item = configure!(self, item);
|
let item = configure!(self, item);
|
||||||
|
|
||||||
let (item, attr) = self.classify_item(item);
|
let (attr, traits, item) = self.classify_item(item);
|
||||||
if let Some(attr) = attr {
|
if attr.is_some() || !traits.is_empty() {
|
||||||
let item = Annotatable::ImplItem(P(fully_configure!(self, item, noop_fold_impl_item)));
|
let item = Annotatable::ImplItem(P(fully_configure!(self, item, noop_fold_impl_item)));
|
||||||
return self.collect_attr(attr, item, ExpansionKind::ImplItems).make_impl_items();
|
return self.collect_attr(attr, traits, item, ExpansionKind::ImplItems)
|
||||||
|
.make_impl_items();
|
||||||
}
|
}
|
||||||
|
|
||||||
match item.node {
|
match item.node {
|
||||||
@@ -1038,6 +1134,7 @@ impl<'feat> ExpansionConfig<'feat> {
|
|||||||
fn enable_trace_macros = trace_macros,
|
fn enable_trace_macros = trace_macros,
|
||||||
fn enable_allow_internal_unstable = allow_internal_unstable,
|
fn enable_allow_internal_unstable = allow_internal_unstable,
|
||||||
fn enable_custom_derive = custom_derive,
|
fn enable_custom_derive = custom_derive,
|
||||||
|
fn proc_macro_enabled = proc_macro,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -84,8 +84,17 @@ impl<'a, 'b> PlaceholderExpander<'a, 'b> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add(&mut self, id: ast::NodeId, expansion: Expansion) {
|
pub fn add(&mut self, id: ast::NodeId, expansion: Expansion, derives: Vec<Mark>) {
|
||||||
let expansion = expansion.fold_with(self);
|
let mut expansion = expansion.fold_with(self);
|
||||||
|
if let Expansion::Items(mut items) = expansion {
|
||||||
|
for derive in derives {
|
||||||
|
match self.remove(derive.as_placeholder_id()) {
|
||||||
|
Expansion::Items(derived_items) => items.extend(derived_items),
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expansion = Expansion::Items(items);
|
||||||
|
}
|
||||||
self.expansions.insert(id, expansion);
|
self.expansions.insert(id, expansion);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -107,12 +107,10 @@ impl MultiItemModifier for ProcMacroDerive {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut res = vec![Annotatable::Item(item)];
|
|
||||||
// Reassign spans of all expanded items to the input `item`
|
// Reassign spans of all expanded items to the input `item`
|
||||||
// for better errors here.
|
// for better errors here.
|
||||||
res.extend(new_items.into_iter().flat_map(|item| {
|
new_items.into_iter().map(|item| {
|
||||||
ChangeSpan { span: span }.fold_item(item)
|
Annotatable::Item(ChangeSpan { span: span }.fold_item(item).expect_one(""))
|
||||||
}).map(Annotatable::Item));
|
}).collect()
|
||||||
res
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,18 +39,18 @@ impl ParseSess {
|
|||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let ecx = &ParseSess;
|
let ecx = &ParseSess;
|
||||||
let x = quote_tokens!(ecx, 3); //~ ERROR macro undefined: 'quote_tokens!'
|
let x = quote_tokens!(ecx, 3); //~ ERROR macro undefined: `quote_tokens`
|
||||||
let x = quote_expr!(ecx, 3); //~ ERROR macro undefined: 'quote_expr!'
|
let x = quote_expr!(ecx, 3); //~ ERROR macro undefined: `quote_expr`
|
||||||
let x = quote_ty!(ecx, 3); //~ ERROR macro undefined: 'quote_ty!'
|
let x = quote_ty!(ecx, 3); //~ ERROR macro undefined: `quote_ty`
|
||||||
let x = quote_method!(ecx, 3); //~ ERROR macro undefined: 'quote_method!'
|
let x = quote_method!(ecx, 3); //~ ERROR macro undefined: `quote_method`
|
||||||
let x = quote_item!(ecx, 3); //~ ERROR macro undefined: 'quote_item!'
|
let x = quote_item!(ecx, 3); //~ ERROR macro undefined: `quote_item`
|
||||||
let x = quote_pat!(ecx, 3); //~ ERROR macro undefined: 'quote_pat!'
|
let x = quote_pat!(ecx, 3); //~ ERROR macro undefined: `quote_pat`
|
||||||
let x = quote_arm!(ecx, 3); //~ ERROR macro undefined: 'quote_arm!'
|
let x = quote_arm!(ecx, 3); //~ ERROR macro undefined: `quote_arm`
|
||||||
let x = quote_stmt!(ecx, 3); //~ ERROR macro undefined: 'quote_stmt!'
|
let x = quote_stmt!(ecx, 3); //~ ERROR macro undefined: `quote_stmt`
|
||||||
let x = quote_matcher!(ecx, 3); //~ ERROR macro undefined: 'quote_matcher!'
|
let x = quote_matcher!(ecx, 3); //~ ERROR macro undefined: `quote_matcher`
|
||||||
let x = quote_attr!(ecx, 3); //~ ERROR macro undefined: 'quote_attr!'
|
let x = quote_attr!(ecx, 3); //~ ERROR macro undefined: `quote_attr`
|
||||||
let x = quote_arg!(ecx, 3); //~ ERROR macro undefined: 'quote_arg!'
|
let x = quote_arg!(ecx, 3); //~ ERROR macro undefined: `quote_arg`
|
||||||
let x = quote_block!(ecx, 3); //~ ERROR macro undefined: 'quote_block!'
|
let x = quote_block!(ecx, 3); //~ ERROR macro undefined: `quote_block`
|
||||||
let x = quote_meta_item!(ecx, 3); //~ ERROR macro undefined: 'quote_meta_item!'
|
let x = quote_meta_item!(ecx, 3); //~ ERROR macro undefined: `quote_meta_item`
|
||||||
let x = quote_path!(ecx, 3); //~ ERROR macro undefined: 'quote_path!'
|
let x = quote_path!(ecx, 3); //~ ERROR macro undefined: `quote_path`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,5 +14,5 @@
|
|||||||
extern crate macro_crate_test;
|
extern crate macro_crate_test;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
assert_eq!(3, unexported_macro!()); //~ ERROR macro undefined: 'unexported_macro!'
|
assert_eq!(3, unexported_macro!()); //~ ERROR macro undefined: `unexported_macro`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,12 +12,12 @@
|
|||||||
// gate
|
// gate
|
||||||
|
|
||||||
__register_diagnostic!(E0001);
|
__register_diagnostic!(E0001);
|
||||||
//~^ ERROR macro undefined: '__register_diagnostic!'
|
//~^ ERROR macro undefined: `__register_diagnostic`
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
__diagnostic_used!(E0001);
|
__diagnostic_used!(E0001);
|
||||||
//~^ ERROR macro undefined: '__diagnostic_used!'
|
//~^ ERROR macro undefined: `__diagnostic_used`
|
||||||
}
|
}
|
||||||
|
|
||||||
__build_diagnostic_array!(DIAGNOSTICS);
|
__build_diagnostic_array!(DIAGNOSTICS);
|
||||||
//~^ ERROR macro undefined: '__build_diagnostic_array!'
|
//~^ ERROR macro undefined: `__build_diagnostic_array`
|
||||||
|
|||||||
@@ -10,9 +10,9 @@
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
print!(test!());
|
print!(test!());
|
||||||
//~^ ERROR: macro undefined: 'test!'
|
//~^ ERROR: macro undefined: `test`
|
||||||
//~^^ ERROR: format argument must be a string literal
|
//~^^ ERROR: format argument must be a string literal
|
||||||
|
|
||||||
concat!(test!());
|
concat!(test!());
|
||||||
//~^ ERROR: macro undefined: 'test!'
|
//~^ ERROR: macro undefined: `test`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,5 +11,5 @@
|
|||||||
fn main() {}
|
fn main() {}
|
||||||
|
|
||||||
impl Type {
|
impl Type {
|
||||||
undef!(); //~ ERROR macro undefined: 'undef!'
|
undef!(); //~ ERROR macro undefined: `undef`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,5 +16,5 @@ fn main() {
|
|||||||
foo!(0); // Check that we report errors at macro definition, not expansion.
|
foo!(0); // Check that we report errors at macro definition, not expansion.
|
||||||
|
|
||||||
let _: cfg!(foo) = (); //~ ERROR non-type macro in type position
|
let _: cfg!(foo) = (); //~ ERROR non-type macro in type position
|
||||||
derive!(); //~ ERROR macro undefined: 'derive!'
|
derive!(); //~ ERROR macro undefined: `derive`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ mod m {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
k!(); //~ ERROR macro undefined: 'k!'
|
k!(); //~ ERROR macro undefined: `k`
|
||||||
//~^ HELP did you mean `kl!`?
|
//~^ HELP did you mean `kl!`?
|
||||||
kl!(); //~ ERROR macro undefined: 'kl!'
|
kl!(); //~ ERROR macro undefined: `kl`
|
||||||
//~^ HELP have you added the `#[macro_use]` on the module/import?
|
//~^ HELP have you added the `#[macro_use]` on the module/import?
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ pub fn main() {
|
|||||||
ref mut Self => (),
|
ref mut Self => (),
|
||||||
//~^ ERROR expected identifier, found keyword `Self`
|
//~^ ERROR expected identifier, found keyword `Self`
|
||||||
Self!() => (),
|
Self!() => (),
|
||||||
//~^ ERROR macro undefined: 'Self!'
|
//~^ ERROR macro undefined: `Self`
|
||||||
Foo { Self } => (),
|
Foo { Self } => (),
|
||||||
//~^ ERROR expected identifier, found keyword `Self`
|
//~^ ERROR expected identifier, found keyword `Self`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,5 @@ use proc_macro::TokenStream;
|
|||||||
pub fn derive(input: TokenStream) -> TokenStream {
|
pub fn derive(input: TokenStream) -> TokenStream {
|
||||||
let input = input.to_string();
|
let input = input.to_string();
|
||||||
assert!(input.contains("struct A;"));
|
assert!(input.contains("struct A;"));
|
||||||
assert!(input.contains("#[derive(Debug, PartialEq, Eq, Copy, Clone)]"));
|
|
||||||
"".parse().unwrap()
|
"".parse().unwrap()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,6 @@ use proc_macro::TokenStream;
|
|||||||
#[proc_macro_derive(AToB)]
|
#[proc_macro_derive(AToB)]
|
||||||
pub fn derive(input: TokenStream) -> TokenStream {
|
pub fn derive(input: TokenStream) -> TokenStream {
|
||||||
let input = input.to_string();
|
let input = input.to_string();
|
||||||
assert_eq!(input, "#[derive(Copy, Clone)]\nstruct A;");
|
assert_eq!(input, "struct A;");
|
||||||
"struct B;".parse().unwrap()
|
"struct B;".parse().unwrap()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,5 @@ pub fn derive(input: TokenStream) -> TokenStream {
|
|||||||
assert!(input.contains("#[B]"));
|
assert!(input.contains("#[B]"));
|
||||||
assert!(input.contains("struct B {"));
|
assert!(input.contains("struct B {"));
|
||||||
assert!(input.contains("#[C]"));
|
assert!(input.contains("#[C]"));
|
||||||
assert!(input.contains("#[derive(Debug, PartialEq, Eq, Copy, Clone)]"));
|
|
||||||
"".parse().unwrap()
|
"".parse().unwrap()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user