Skip to content

Commit

Permalink
Hygienize librustc_resolve.
Browse files Browse the repository at this point in the history
  • Loading branch information
jseyfried committed May 25, 2017
1 parent 1cded84 commit 1f175fa
Show file tree
Hide file tree
Showing 18 changed files with 491 additions and 189 deletions.
2 changes: 1 addition & 1 deletion src/librustc/hir/lowering.rs
Expand Up @@ -393,7 +393,7 @@ impl<'a> LoweringContext<'a> {
}

fn allow_internal_unstable(&self, reason: &'static str, mut span: Span) -> Span {
let mark = Mark::fresh();
let mark = Mark::fresh(Mark::root());
mark.set_expn_info(codemap::ExpnInfo {
call_site: span,
callee: codemap::NameAndSpan {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/cstore_impl.rs
Expand Up @@ -388,7 +388,7 @@ impl CrateStore for cstore::CStore {
attrs: attrs.iter().cloned().collect(),
node: ast::ItemKind::MacroDef(ast::MacroDef {
tokens: body.into(),
legacy: true,
legacy: def.legacy,
}),
vis: ast::Visibility::Inherited,
})
Expand Down
1 change: 1 addition & 0 deletions src/librustc_metadata/encoder.rs
Expand Up @@ -1104,6 +1104,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
Entry {
kind: EntryKind::MacroDef(self.lazy(&MacroDef {
body: pprust::tts_to_string(&macro_def.body.trees().collect::<Vec<_>>()),
legacy: macro_def.legacy,
})),
visibility: self.lazy(&ty::Visibility::Public),
span: self.lazy(&macro_def.span),
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_metadata/schema.rs
Expand Up @@ -433,9 +433,10 @@ impl_stable_hash_for!(struct ModData { reexports });
#[derive(RustcEncodable, RustcDecodable)]
pub struct MacroDef {
pub body: String,
pub legacy: bool,
}

impl_stable_hash_for!(struct MacroDef { body });
impl_stable_hash_for!(struct MacroDef { body, legacy });

#[derive(RustcEncodable, RustcDecodable)]
pub struct FnData {
Expand Down
69 changes: 43 additions & 26 deletions src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -23,7 +23,7 @@ use {resolve_error, resolve_struct_error, ResolutionError};

use rustc::middle::cstore::LoadedMacro;
use rustc::hir::def::*;
use rustc::hir::def_id::{CrateNum, BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, DefId};
use rustc::hir::def_id::{BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, LOCAL_CRATE, DefId};
use rustc::ty;

use std::cell::Cell;
Expand Down Expand Up @@ -150,7 +150,7 @@ impl<'a> Resolver<'a> {
view_path.span,
ResolutionError::SelfImportsOnlyAllowedWithin);
} else if source_name == "$crate" && full_path.segments.len() == 1 {
let crate_root = self.resolve_crate_var(source.ctxt, item.span);
let crate_root = self.resolve_crate_root(source.ctxt);
let crate_name = match crate_root.kind {
ModuleKind::Def(_, name) => name,
ModuleKind::Block(..) => unreachable!(),
Expand Down Expand Up @@ -247,7 +247,8 @@ impl<'a> Resolver<'a> {

// n.b. we don't need to look at the path option here, because cstore already did
let crate_id = self.session.cstore.extern_mod_stmt_cnum(item.id).unwrap();
let module = self.get_extern_crate_root(crate_id, item.span);
let module =
self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX });
self.populate_module_if_necessary(module);
let used = self.process_legacy_macro_imports(item, module, expansion);
let binding =
Expand Down Expand Up @@ -279,7 +280,7 @@ impl<'a> Resolver<'a> {
no_implicit_prelude: parent.no_implicit_prelude || {
attr::contains_name(&item.attrs, "no_implicit_prelude")
},
..ModuleData::new(Some(parent), module_kind, def_id, item.span)
..ModuleData::new(Some(parent), module_kind, def_id, expansion, item.span)
});
self.define(parent, ident, TypeNS, (module, vis, sp, expansion));
self.module_map.insert(def_id, module);
Expand Down Expand Up @@ -317,6 +318,7 @@ impl<'a> Resolver<'a> {
let module = self.new_module(parent,
module_kind,
parent.normal_ancestor_id,
expansion,
item.span);
self.define(parent, ident, TypeNS, (module, vis, sp, expansion));

Expand Down Expand Up @@ -376,6 +378,7 @@ impl<'a> Resolver<'a> {
let module = self.new_module(parent,
module_kind,
parent.normal_ancestor_id,
expansion,
item.span);
self.define(parent, ident, TypeNS, (module, vis, sp, expansion));
self.current_module = module;
Expand Down Expand Up @@ -421,12 +424,13 @@ impl<'a> Resolver<'a> {
self.define(parent, item.ident, ValueNS, (def, vis, item.span, expansion));
}

fn build_reduced_graph_for_block(&mut self, block: &Block) {
fn build_reduced_graph_for_block(&mut self, block: &Block, expansion: Mark) {
let parent = self.current_module;
if self.block_needs_anonymous_module(block) {
let module = self.new_module(parent,
ModuleKind::Block(block.id),
parent.normal_ancestor_id,
expansion,
block.span);
self.block_map.insert(block.id, module);
self.current_module = module; // Descend into the block.
Expand All @@ -440,23 +444,24 @@ impl<'a> Resolver<'a> {
let def_id = def.def_id();
let vis = self.session.cstore.visibility(def_id);
let span = child.span;

let expansion = Mark::root(); // FIXME(jseyfried) intercrate hygiene
match def {
Def::Mod(..) | Def::Enum(..) => {
let module = self.new_module(parent,
ModuleKind::Def(def, ident.name),
def_id,
expansion,
span);
self.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, Mark::root()));
self.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, expansion));
}
Def::Variant(..) | Def::TyAlias(..) => {
self.define(parent, ident, TypeNS, (def, vis, DUMMY_SP, Mark::root()));
self.define(parent, ident, TypeNS, (def, vis, DUMMY_SP, expansion));
}
Def::Fn(..) | Def::Static(..) | Def::Const(..) | Def::VariantCtor(..) => {
self.define(parent, ident, ValueNS, (def, vis, DUMMY_SP, Mark::root()));
self.define(parent, ident, ValueNS, (def, vis, DUMMY_SP, expansion));
}
Def::StructCtor(..) => {
self.define(parent, ident, ValueNS, (def, vis, DUMMY_SP, Mark::root()));
self.define(parent, ident, ValueNS, (def, vis, DUMMY_SP, expansion));

if let Some(struct_def_id) =
self.session.cstore.def_key(def_id).parent
Expand All @@ -469,14 +474,15 @@ impl<'a> Resolver<'a> {
let module = self.new_module(parent,
module_kind,
parent.normal_ancestor_id,
expansion,
span);
self.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, Mark::root()));
self.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, expansion));

for child in self.session.cstore.item_children(def_id) {
let ns = if let Def::AssociatedTy(..) = child.def { TypeNS } else { ValueNS };
let ident = Ident::with_empty_ctxt(child.name);
self.define(module, ident, ns, (child.def, ty::Visibility::Public,
DUMMY_SP, Mark::root()));
DUMMY_SP, expansion));

if self.session.cstore.associated_item_cloned(child.def.def_id())
.method_has_self_argument {
Expand All @@ -486,31 +492,42 @@ impl<'a> Resolver<'a> {
module.populated.set(true);
}
Def::Struct(..) | Def::Union(..) => {
self.define(parent, ident, TypeNS, (def, vis, DUMMY_SP, Mark::root()));
self.define(parent, ident, TypeNS, (def, vis, DUMMY_SP, expansion));

// Record field names for error reporting.
let field_names = self.session.cstore.struct_field_names(def_id);
self.insert_field_names(def_id, field_names);
}
Def::Macro(..) => {
self.define(parent, ident, MacroNS, (def, vis, DUMMY_SP, Mark::root()));
self.define(parent, ident, MacroNS, (def, vis, DUMMY_SP, expansion));
}
_ => bug!("unexpected definition: {:?}", def)
}
}

fn get_extern_crate_root(&mut self, cnum: CrateNum, span: Span) -> Module<'a> {
let def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX };
let name = self.session.cstore.crate_name(cnum);
let macros_only = self.session.cstore.dep_kind(cnum).macros_only();
let module_kind = ModuleKind::Def(Def::Mod(def_id), name);
let arenas = self.arenas;
*self.extern_crate_roots.entry((cnum, macros_only)).or_insert_with(|| {
arenas.alloc_module(ModuleData::new(None, module_kind, def_id, span))
})
pub fn get_module(&mut self, def_id: DefId) -> Module<'a> {
if def_id.krate == LOCAL_CRATE {
return self.module_map[&def_id]
}

let macros_only = self.session.cstore.dep_kind(def_id.krate).macros_only();
if let Some(&module) = self.extern_module_map.get(&(def_id, macros_only)) {
return module;
}

let (name, parent) = if def_id.index == CRATE_DEF_INDEX {
(self.session.cstore.crate_name(def_id.krate), None)
} else {
let def_key = self.session.cstore.def_key(def_id);
(def_key.disambiguated_data.data.get_opt_name().unwrap(),
Some(self.get_module(DefId { index: def_key.parent.unwrap(), ..def_id })))
};

let kind = ModuleKind::Def(Def::Mod(def_id), name);
self.arenas.alloc_module(ModuleData::new(parent, kind, def_id, Mark::root(), DUMMY_SP))
}

pub fn macro_def_scope(&mut self, expansion: Mark, span: Span) -> Module<'a> {
pub fn macro_def_scope(&mut self, expansion: Mark) -> Module<'a> {
let def_id = self.macro_defs[&expansion];
if let Some(id) = self.definitions.as_local_node_id(def_id) {
self.local_macro_def_scopes[&id]
Expand All @@ -519,7 +536,7 @@ impl<'a> Resolver<'a> {
self.graph_root
} else {
let module_def_id = ty::DefIdTree::parent(&*self, def_id).unwrap();
self.get_extern_crate_root(module_def_id.krate, span)
self.get_module(module_def_id)
}
}

Expand Down Expand Up @@ -766,7 +783,7 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> {

fn visit_block(&mut self, block: &'a Block) {
let (parent, legacy_scope) = (self.resolver.current_module, self.legacy_scope);
self.resolver.build_reduced_graph_for_block(block);
self.resolver.build_reduced_graph_for_block(block, self.expansion);
visit::walk_block(self, block);
self.resolver.current_module = parent;
self.legacy_scope = legacy_scope;
Expand Down

0 comments on commit 1f175fa

Please sign in to comment.