Skip to content

Commit

Permalink
Elimite $crate before invokng custom derives.
Browse files Browse the repository at this point in the history
  • Loading branch information
jseyfried committed Nov 10, 2016
1 parent b46ce08 commit 1119567
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
29 changes: 28 additions & 1 deletion src/librustc_resolve/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use {Module, Resolver};
use {Module, ModuleKind, Resolver};
use build_reduced_graph::BuildReducedGraphVisitor;
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefIndex};
use rustc::hir::map::{self, DefCollector};
Expand All @@ -21,7 +21,9 @@ use syntax::ext::base::{NormalTT, SyntaxExtension};
use syntax::ext::expand::Expansion;
use syntax::ext::hygiene::Mark;
use syntax::ext::tt::macro_rules;
use syntax::fold::Folder;
use syntax::parse::token::intern;
use syntax::ptr::P;
use syntax::util::lev_distance::find_best_match_for_name;
use syntax_pos::Span;

Expand Down Expand Up @@ -97,6 +99,31 @@ impl<'a> base::Resolver for Resolver<'a> {
mark
}

fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item> {
struct EliminateCrateVar<'b, 'a: 'b>(&'b mut Resolver<'a>);

impl<'a, 'b> Folder for EliminateCrateVar<'a, 'b> {
fn fold_path(&mut self, mut path: ast::Path) -> ast::Path {
let ident = path.segments[0].identifier;
if &ident.name.as_str() == "$crate" {
path.global = true;
let module = self.0.resolve_crate_var(ident.ctxt);
if module.is_local() {
path.segments.remove(0);
} else {
path.segments[0].identifier = match module.kind {
ModuleKind::Def(_, name) => ast::Ident::with_empty_ctxt(name),
_ => unreachable!(),
};
}
}
path
}
}

EliminateCrateVar(self).fold_item(item).expect_one("")
}

fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion) {
let invocation = self.invocations[&mark];
self.collect_def_ids(invocation, expansion);
Expand Down
2 changes: 2 additions & 0 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ pub type NamedSyntaxExtension = (Name, SyntaxExtension);
pub trait Resolver {
fn next_node_id(&mut self) -> ast::NodeId;
fn get_module_scope(&mut self, id: ast::NodeId) -> Mark;
fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item>;

fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion);
fn add_macro(&mut self, scope: Mark, def: ast::MacroDef, export: bool);
Expand All @@ -539,6 +540,7 @@ pub struct DummyResolver;
impl Resolver for DummyResolver {
fn next_node_id(&mut self) -> ast::NodeId { ast::DUMMY_NODE_ID }
fn get_module_scope(&mut self, _id: ast::NodeId) -> Mark { Mark::root() }
fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item> { item }

fn visit_expansion(&mut self, _invoc: Mark, _expansion: &Expansion) {}
fn add_macro(&mut self, _scope: Mark, _def: ast::MacroDef, _export: bool) {}
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax_ext/deriving/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl MultiItemModifier for CustomDerive {
// Mark attributes as known, and used.
MarkAttrs(&self.attrs).visit_item(&item);

let input = __internal::new_token_stream(item.clone());
let input = __internal::new_token_stream(ecx.resolver.eliminate_crate_var(item.clone()));
let res = __internal::set_parse_sess(&ecx.parse_sess, || {
let inner = self.inner;
panic::catch_unwind(panic::AssertUnwindSafe(|| inner(input)))
Expand Down

0 comments on commit 1119567

Please sign in to comment.