use syntax::ast::Name;
use syntax::attr;
-use syntax::parse::token;
use syntax::ast::{self, Block, ForeignItem, ForeignItemKind, Item, ItemKind};
use syntax::ast::{Mutability, StmtKind, TraitItem, TraitItemKind};
})
}
+ fn insert_field_names(&mut self, def_id: DefId, field_names: Vec<Name>) {
+ if !field_names.is_empty() {
+ self.field_names.insert(def_id, field_names);
+ }
+ }
+
/// Constructs the reduced graph for one item.
fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
let parent = self.current_module;
self.define(parent, name, ValueNS, (ctor_def, sp, vis));
}
- // Record the def ID and fields of this struct.
- let field_names = struct_def.fields().iter().enumerate().map(|(index, field)| {
+ // Record field names for error reporting.
+ let field_names = struct_def.fields().iter().filter_map(|field| {
self.resolve_visibility(&field.vis);
field.ident.map(|ident| ident.name)
- .unwrap_or_else(|| token::intern(&index.to_string()))
}).collect();
let item_def_id = self.definitions.local_def_id(item.id);
- self.structs.insert(item_def_id, field_names);
+ self.insert_field_names(item_def_id, field_names);
}
ItemKind::Union(ref vdata, _) => {
let def = Def::Union(self.definitions.local_def_id(item.id));
self.define(parent, name, TypeNS, (def, sp, vis));
- // Record the def ID and fields of this union.
- let field_names = vdata.fields().iter().enumerate().map(|(index, field)| {
+ // Record field names for error reporting.
+ let field_names = vdata.fields().iter().filter_map(|field| {
self.resolve_visibility(&field.vis);
field.ident.map(|ident| ident.name)
- .unwrap_or_else(|| token::intern(&index.to_string()))
}).collect();
let item_def_id = self.definitions.local_def_id(item.id);
- self.structs.insert(item_def_id, field_names);
+ self.insert_field_names(item_def_id, field_names);
}
ItemKind::DefaultImpl(..) | ItemKind::Impl(..) => {}
parent: Module<'b>,
vis: ty::Visibility) {
let name = variant.node.name.name;
- let ctor_kind = CtorKind::from_vdata(&variant.node.data);
- if variant.node.data.is_struct() {
- // Not adding fields for variants as they are not accessed with a self receiver
- let variant_def_id = self.definitions.local_def_id(variant.node.data.id());
- self.structs.insert(variant_def_id, Vec::new());
- }
+ let def_id = self.definitions.local_def_id(variant.node.data.id());
- // All variants are defined in both type and value namespaces as future-proofing.
- let def = Def::Variant(self.definitions.local_def_id(variant.node.data.id()));
- let ctor_def = Def::VariantCtor(self.definitions.local_def_id(variant.node.data.id()),
- ctor_kind);
+ // Define a name in the type namespace.
+ let def = Def::Variant(def_id);
self.define(parent, name, TypeNS, (def, variant.span, vis));
+
+ // 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_vdata(&variant.node.data);
+ let ctor_def = Def::VariantCtor(def_id, ctor_kind);
self.define(parent, name, ValueNS, (ctor_def, variant.span, vis));
}
};
match def {
- Def::Mod(_) | Def::Enum(..) => {
- debug!("(building reduced graph for external crate) building module {} {:?}",
- name, vis);
+ Def::Mod(..) | Def::Enum(..) => {
let module = self.new_module(parent, ModuleKind::Def(def, name), false);
- let _ = self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
+ self.define(parent, name, TypeNS, (module, DUMMY_SP, vis));
}
Def::Variant(..) => {
- debug!("(building reduced graph for external crate) building variant {}", name);
- // All variants are defined in both type and value namespaces as future-proofing.
- let vkind = self.session.cstore.variant_kind(def_id).unwrap();
- let _ = self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
- if vkind == ty::VariantKind::Struct {
- // Not adding fields for variants as they are not accessed with a self receiver
- self.structs.insert(def_id, Vec::new());
- }
+ self.define(parent, name, TypeNS, (def, DUMMY_SP, vis));
}
Def::VariantCtor(..) => {
- let _ = self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
+ self.define(parent, name, ValueNS, (def, DUMMY_SP, vis));
}
Def::Fn(..) |
Def::Static(..) |
Def::Const(..) |
Def::AssociatedConst(..) |
Def::Method(..) => {
- debug!("(building reduced graph for external crate) building value (fn/static) {}",
- name);
- let _ = self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
+ self.define(parent, name, ValueNS, (def, DUMMY_SP, vis));
}
- Def::Trait(_) => {
- debug!("(building reduced graph for external crate) building type {}", name);
-
- // If this is a trait, add all the trait item names to the trait
- // info.
+ Def::Trait(..) => {
+ let module = self.new_module(parent, ModuleKind::Def(def, name), false);
+ self.define(parent, name, TypeNS, (module, DUMMY_SP, vis));
+ // If this is a trait, add all the trait item names to the trait info.
let trait_item_def_ids = self.session.cstore.impl_or_trait_items(def_id);
- for &trait_item_def in &trait_item_def_ids {
- let trait_item_name =
- self.session.cstore.def_key(trait_item_def)
- .disambiguated_data.data.get_opt_name()
- .expect("opt_item_name returned None for trait");
-
- debug!("(building reduced graph for external crate) ... adding trait item \
- '{}'",
- trait_item_name);
-
+ for trait_item_def_id in trait_item_def_ids {
+ let trait_item_name = self.session.cstore.def_key(trait_item_def_id)
+ .disambiguated_data.data.get_opt_name()
+ .expect("opt_item_name returned None for trait");
self.trait_item_map.insert((trait_item_name, def_id), false);
}
-
- let module = self.new_module(parent, ModuleKind::Def(def, name), false);
- let _ = self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
}
Def::TyAlias(..) | Def::AssociatedTy(..) => {
- debug!("(building reduced graph for external crate) building type {}", name);
- let _ = self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
+ self.define(parent, name, TypeNS, (def, DUMMY_SP, vis));
}
Def::Struct(..) => {
- debug!("(building reduced graph for external crate) building type and value for {}",
- name);
- let _ = self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
+ self.define(parent, name, TypeNS, (def, DUMMY_SP, vis));
- // Record the def ID and fields of this struct.
- let fields = self.session.cstore.struct_field_names(def_id);
- self.structs.insert(def_id, fields);
+ // 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(..) => {
- let _ = self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
+ self.define(parent, name, ValueNS, (def, DUMMY_SP, vis));
}
- Def::Union(_) => {
- let _ = self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
+ Def::Union(..) => {
+ self.define(parent, name, TypeNS, (def, DUMMY_SP, vis));
- // Record the def ID and fields of this union.
- let fields = self.session.cstore.struct_field_names(def_id);
- self.structs.insert(def_id, fields);
+ // 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::Local(..) |
Def::PrimTy(..) |
Def::Label(..) |
Def::SelfTy(..) |
Def::Err => {
- bug!("didn't expect `{:?}`", def);
+ bug!("unexpected definition: {:?}", def);
}
}
}