提交 1bbf7a43 编写于 作者: J Jeffrey Seyfried

Add field `expansion: Mark` to `NameBinding`.

上级 83aac43f
......@@ -45,15 +45,25 @@
use syntax_pos::{Span, DUMMY_SP};
impl<'a> ToNameBinding<'a> for (Module<'a>, Span, ty::Visibility) {
impl<'a> ToNameBinding<'a> for (Module<'a>, ty::Visibility, Span, Mark) {
fn to_name_binding(self) -> NameBinding<'a> {
NameBinding { kind: NameBindingKind::Module(self.0), span: self.1, vis: self.2 }
NameBinding {
kind: NameBindingKind::Module(self.0),
vis: self.1,
span: self.2,
expansion: self.3,
}
}
}
impl<'a> ToNameBinding<'a> for (Def, Span, ty::Visibility) {
impl<'a> ToNameBinding<'a> for (Def, ty::Visibility, Span, Mark) {
fn to_name_binding(self) -> NameBinding<'a> {
NameBinding { kind: NameBindingKind::Def(self.0), span: self.1, vis: self.2 }
NameBinding {
kind: NameBindingKind::Def(self.0),
vis: self.1,
span: self.2,
expansion: self.3,
}
}
}
......@@ -148,8 +158,9 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
}
let subclass = ImportDirectiveSubclass::single(binding.name, source.name);
let span = view_path.span;
self.add_import_directive(module_path, subclass, span, item.id, vis);
self.add_import_directive(
module_path, subclass, view_path.span, item.id, vis, expansion,
);
}
ViewPathList(_, ref source_items) => {
// Make sure there's at most one `mod` import in the list.
......@@ -196,8 +207,10 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
}
};
let subclass = ImportDirectiveSubclass::single(rename, name);
let (span, id) = (source_item.span, source_item.node.id);
self.add_import_directive(module_path, subclass, span, id, vis);
let id = source_item.node.id;
self.add_import_directive(
module_path, subclass, source_item.span, id, vis, expansion,
);
}
}
ViewPathGlob(_) => {
......@@ -205,8 +218,9 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
is_prelude: is_prelude,
max_vis: Cell::new(ty::Visibility::PrivateExternal),
};
let span = view_path.span;
self.add_import_directive(module_path, subclass, span, item.id, vis);
self.add_import_directive(
module_path, subclass, view_path.span, item.id, vis, expansion,
);
}
}
}
......@@ -217,7 +231,7 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
// 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);
let binding = (module, sp, ty::Visibility::Public).to_name_binding();
let binding = (module, ty::Visibility::Public, sp, expansion).to_name_binding();
let binding = self.arenas.alloc_name_binding(binding);
let directive = self.arenas.alloc_import_directive(ImportDirective {
id: item.id,
......@@ -227,6 +241,7 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
span: item.span,
module_path: Vec::new(),
vis: Cell::new(vis),
expansion: expansion,
});
let imported_binding = self.import(binding, directive);
self.define(parent, name, TypeNS, imported_binding);
......@@ -245,7 +260,7 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
normal_ancestor_id: Some(item.id),
..ModuleS::new(Some(parent), ModuleKind::Def(def, name))
});
self.define(parent, name, TypeNS, (module, sp, vis));
self.define(parent, name, TypeNS, (module, vis, sp, expansion));
self.module_map.insert(item.id, module);
// Descend into the module.
......@@ -258,30 +273,30 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
ItemKind::Static(_, m, _) => {
let mutbl = m == Mutability::Mutable;
let def = Def::Static(self.definitions.local_def_id(item.id), mutbl);
self.define(parent, name, ValueNS, (def, sp, vis));
self.define(parent, name, ValueNS, (def, vis, sp, expansion));
}
ItemKind::Const(..) => {
let def = Def::Const(self.definitions.local_def_id(item.id));
self.define(parent, name, ValueNS, (def, sp, vis));
self.define(parent, name, ValueNS, (def, vis, sp, expansion));
}
ItemKind::Fn(..) => {
let def = Def::Fn(self.definitions.local_def_id(item.id));
self.define(parent, name, ValueNS, (def, sp, vis));
self.define(parent, name, ValueNS, (def, vis, sp, expansion));
}
// These items live in the type namespace.
ItemKind::Ty(..) => {
let def = Def::TyAlias(self.definitions.local_def_id(item.id));
self.define(parent, name, TypeNS, (def, sp, vis));
self.define(parent, name, TypeNS, (def, vis, sp, expansion));
}
ItemKind::Enum(ref enum_definition, _) => {
let def = Def::Enum(self.definitions.local_def_id(item.id));
let module = self.new_module(parent, ModuleKind::Def(def, name), true);
self.define(parent, name, TypeNS, (module, sp, vis));
self.define(parent, name, TypeNS, (module, vis, sp, expansion));
for variant in &(*enum_definition).variants {
self.build_reduced_graph_for_variant(variant, module, vis);
self.build_reduced_graph_for_variant(variant, module, vis, expansion);
}
}
......@@ -289,14 +304,14 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
ItemKind::Struct(ref struct_def, _) => {
// Define a name in the type namespace.
let def = Def::Struct(self.definitions.local_def_id(item.id));
self.define(parent, name, TypeNS, (def, sp, vis));
self.define(parent, name, TypeNS, (def, vis, sp, expansion));
// If this is a tuple or unit struct, define a name
// in the value namespace as well.
if !struct_def.is_struct() {
let ctor_def = Def::StructCtor(self.definitions.local_def_id(struct_def.id()),
CtorKind::from_ast(struct_def));
self.define(parent, name, ValueNS, (ctor_def, sp, vis));
self.define(parent, name, ValueNS, (ctor_def, vis, sp, expansion));
}
// Record field names for error reporting.
......@@ -310,7 +325,7 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
ItemKind::Union(ref vdata, _) => {
let def = Def::Union(self.definitions.local_def_id(item.id));
self.define(parent, name, TypeNS, (def, sp, vis));
self.define(parent, name, TypeNS, (def, vis, sp, expansion));
// Record field names for error reporting.
let field_names = vdata.fields().iter().filter_map(|field| {
......@@ -329,7 +344,7 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
// Add all the items within to a new module.
let module =
self.new_module(parent, ModuleKind::Def(Def::Trait(def_id), name), true);
self.define(parent, name, TypeNS, (module, sp, vis));
self.define(parent, name, TypeNS, (module, vis, sp, expansion));
self.current_module = module;
}
ItemKind::Mac(_) => panic!("unexpanded macro in resolve!"),
......@@ -338,27 +353,28 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
// Constructs the reduced graph for one variant. Variants exist in the
// type and value namespaces.
fn build_reduced_graph_for_variant(&mut self,
variant: &Variant,
parent: Module<'b>,
vis: ty::Visibility) {
fn build_reduced_graph_for_variant(
&mut self, variant: &Variant, parent: Module<'b>, vis: ty::Visibility, expansion: Mark,
) {
let name = variant.node.name.name;
let def_id = self.definitions.local_def_id(variant.node.data.id());
// Define a name in the type namespace.
let def = Def::Variant(def_id);
self.define(parent, name, TypeNS, (def, variant.span, vis));
self.define(parent, name, TypeNS, (def, vis, variant.span, expansion));
// Define a constructor name in the value namespace.
// Braced variants, unlike structs, generate unusable names in
// value namespace, they are reserved for possible future use.
let ctor_kind = CtorKind::from_ast(&variant.node.data);
let ctor_def = Def::VariantCtor(def_id, ctor_kind);
self.define(parent, name, ValueNS, (ctor_def, variant.span, vis));
self.define(parent, name, ValueNS, (ctor_def, vis, variant.span, expansion));
}
/// Constructs the reduced graph for one foreign item.
fn build_reduced_graph_for_foreign_item(&mut self, foreign_item: &ForeignItem) {
fn build_reduced_graph_for_foreign_item(
&mut self, foreign_item: &ForeignItem, expansion: Mark,
) {
let parent = self.current_module;
let name = foreign_item.ident.name;
......@@ -371,7 +387,7 @@ fn build_reduced_graph_for_foreign_item(&mut self, foreign_item: &ForeignItem) {
}
};
let vis = self.resolve_visibility(&foreign_item.vis);
self.define(parent, name, ValueNS, (def, foreign_item.span, vis));
self.define(parent, name, ValueNS, (def, vis, foreign_item.span, expansion));
}
fn build_reduced_graph_for_block(&mut self, block: &Block) {
......@@ -404,24 +420,24 @@ fn build_reduced_graph_for_external_crate_def(&mut self, parent: Module<'b>,
match def {
Def::Mod(..) | Def::Enum(..) => {
let module = self.new_module(parent, ModuleKind::Def(def, name), false);
self.define(parent, name, TypeNS, (module, DUMMY_SP, vis));
self.define(parent, name, TypeNS, (module, vis, DUMMY_SP, Mark::root()));
}
Def::Variant(..) => {
self.define(parent, name, TypeNS, (def, DUMMY_SP, vis));
self.define(parent, name, TypeNS, (def, vis, DUMMY_SP, Mark::root()));
}
Def::VariantCtor(..) => {
self.define(parent, name, ValueNS, (def, DUMMY_SP, vis));
self.define(parent, name, ValueNS, (def, vis, DUMMY_SP, Mark::root()));
}
Def::Fn(..) |
Def::Static(..) |
Def::Const(..) |
Def::AssociatedConst(..) |
Def::Method(..) => {
self.define(parent, name, ValueNS, (def, DUMMY_SP, vis));
self.define(parent, name, ValueNS, (def, vis, DUMMY_SP, Mark::root()));
}
Def::Trait(..) => {
let module = self.new_module(parent, ModuleKind::Def(def, name), false);
self.define(parent, name, TypeNS, (module, DUMMY_SP, vis));
self.define(parent, name, TypeNS, (module, vis, DUMMY_SP, Mark::root()));
// If this is a trait, add all the trait item names to the trait info.
let trait_item_def_ids = self.session.cstore.associated_item_def_ids(def_id);
......@@ -433,27 +449,27 @@ fn build_reduced_graph_for_external_crate_def(&mut self, parent: Module<'b>,
}
}
Def::TyAlias(..) | Def::AssociatedTy(..) => {
self.define(parent, name, TypeNS, (def, DUMMY_SP, vis));
self.define(parent, name, TypeNS, (def, vis, DUMMY_SP, Mark::root()));
}
Def::Struct(..) => {
self.define(parent, name, TypeNS, (def, DUMMY_SP, vis));
self.define(parent, name, TypeNS, (def, vis, DUMMY_SP, Mark::root()));
// 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::StructCtor(..) => {
self.define(parent, name, ValueNS, (def, DUMMY_SP, vis));
self.define(parent, name, ValueNS, (def, vis, DUMMY_SP, Mark::root()));
}
Def::Union(..) => {
self.define(parent, name, TypeNS, (def, DUMMY_SP, vis));
self.define(parent, name, TypeNS, (def, vis, DUMMY_SP, Mark::root()));
// 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, name, MacroNS, (def, DUMMY_SP, vis));
self.define(parent, name, MacroNS, (def, vis, DUMMY_SP, Mark::root()));
}
Def::Local(..) |
Def::PrimTy(..) |
......@@ -695,7 +711,7 @@ fn visit_stmt(&mut self, stmt: &ast::Stmt) {
}
fn visit_foreign_item(&mut self, foreign_item: &ForeignItem) {
self.resolver.build_reduced_graph_for_foreign_item(foreign_item);
self.resolver.build_reduced_graph_for_foreign_item(foreign_item, self.expansion);
visit::walk_foreign_item(self, foreign_item);
}
......@@ -732,7 +748,7 @@ fn visit_trait_item(&mut self, item: &TraitItem) {
self.resolver.trait_item_map.insert((item.ident.name, def_id), is_static_method);
let vis = ty::Visibility::Public;
self.resolver.define(parent, item.ident.name, ns, (def, item.span, vis));
self.resolver.define(parent, item.ident.name, ns, (def, vis, item.span, self.expansion));
self.resolver.current_module = parent.parent.unwrap(); // nearest normal ancestor
visit::walk_trait_item(self, item);
......
......@@ -881,6 +881,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
#[derive(Clone, Debug)]
pub struct NameBinding<'a> {
kind: NameBindingKind<'a>,
expansion: Mark,
span: Span,
vis: ty::Visibility,
}
......@@ -1304,6 +1305,7 @@ pub fn new(session: &'a Session,
arenas: arenas,
dummy_binding: arenas.alloc_name_binding(NameBinding {
kind: NameBindingKind::Def(Def::Err),
expansion: Mark::root(),
span: DUMMY_SP,
vis: ty::Visibility::Public,
}),
......
......@@ -184,6 +184,7 @@ fn add_ext(&mut self, ident: ast::Ident, ext: Rc<SyntaxExtension>) {
kind: NameBindingKind::Def(Def::Macro(def_id)),
span: DUMMY_SP,
vis: ty::Visibility::PrivateExternal,
expansion: Mark::root(),
});
self.builtin_macros.insert(ident.name, binding);
}
......
......@@ -26,6 +26,7 @@
use syntax::ast::{Ident, NodeId, Name};
use syntax::ext::base::Determinacy::{self, Determined, Undetermined};
use syntax::ext::hygiene::Mark;
use syntax::util::lev_distance::find_best_match_for_name;
use syntax_pos::Span;
......@@ -70,6 +71,7 @@ pub struct ImportDirective<'a> {
pub subclass: ImportDirectiveSubclass<'a>,
pub span: Span,
pub vis: Cell<ty::Visibility>,
pub expansion: Mark,
}
impl<'a> ImportDirective<'a> {
......@@ -257,7 +259,8 @@ pub fn add_import_directive(&mut self,
subclass: ImportDirectiveSubclass<'a>,
span: Span,
id: NodeId,
vis: ty::Visibility) {
vis: ty::Visibility,
expansion: Mark) {
let current_module = self.current_module;
let directive = self.arenas.alloc_import_directive(ImportDirective {
parent: current_module,
......@@ -267,6 +270,7 @@ pub fn add_import_directive(&mut self,
span: span,
id: id,
vis: Cell::new(vis),
expansion: expansion,
});
self.indeterminate_imports.push(directive);
......@@ -310,6 +314,7 @@ pub fn import(&mut self, binding: &'a NameBinding<'a>, directive: &'a ImportDire
},
span: directive.span,
vis: vis,
expansion: directive.expansion,
}
}
......@@ -336,6 +341,7 @@ pub fn try_define<T>(&mut self, module: Module<'a>, name: Name, ns: Namespace, b
binding.vis
},
span: old_binding.span,
expansion: Mark::root(),
}));
} else if !old_binding.vis.is_at_least(binding.vis, this) {
// We are glob-importing the same item but with greater visibility.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册