Skip to content

Commit 8612455

Browse files
committed
Fix cross-crate enum namespacing
1 parent 8c1d2ab commit 8612455

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/librustc/metadata/csearch.rs

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use metadata::common::*;
1616
use metadata::cstore;
1717
use metadata::decoder;
18+
use middle::def;
1819
use middle::lang_items;
1920
use middle::resolve;
2021
use middle::ty;
@@ -115,6 +116,12 @@ pub fn maybe_get_item_ast<'tcx>(tcx: &ty::ctxt<'tcx>, def: ast::DefId,
115116
decoder::maybe_get_item_ast(&*cdata, tcx, def.node, decode_inlined_item)
116117
}
117118

119+
pub fn get_enum_variant_defs(cstore: &cstore::CStore, enum_id: ast::DefId)
120+
-> Vec<(def::Def, ast::Ident, ast::Visibility)> {
121+
let cdata = cstore.get_crate_data(enum_id.krate);
122+
decoder::get_enum_variant_defs(&*cstore.intr, &*cdata, enum_id.node)
123+
}
124+
118125
pub fn get_enum_variants(tcx: &ty::ctxt, def: ast::DefId)
119126
-> Vec<Rc<ty::VariantInfo>> {
120127
let cstore = &tcx.sess.cstore;

src/librustc/metadata/decoder.rs

+18
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,24 @@ pub fn maybe_get_item_ast<'tcx>(cdata: Cmd, tcx: &ty::ctxt<'tcx>, id: ast::NodeI
662662
}
663663
}
664664

665+
pub fn get_enum_variant_defs(intr: &IdentInterner,
666+
cdata: Cmd,
667+
id: ast::NodeId)
668+
-> Vec<(def::Def, ast::Ident, ast::Visibility)> {
669+
let data = cdata.data();
670+
let items = reader::get_doc(rbml::Doc::new(data), tag_items);
671+
let item = find_item(id, items);
672+
enum_variant_ids(item, cdata).iter().map(|did| {
673+
let item = find_item(did.node, items);
674+
let name = item_name(intr, item);
675+
let visibility = item_visibility(item);
676+
match item_to_def_like(item, *did, cdata.cnum) {
677+
DlDef(def @ def::DefVariant(..)) => (def, name, visibility),
678+
_ => unreachable!()
679+
}
680+
}).collect()
681+
}
682+
665683
pub fn get_enum_variants(intr: Rc<IdentInterner>, cdata: Cmd, id: ast::NodeId,
666684
tcx: &ty::ctxt) -> Vec<Rc<ty::VariantInfo>> {
667685
let data = cdata.data();

src/librustc/middle/resolve.rs

+31
Original file line numberDiff line numberDiff line change
@@ -1806,6 +1806,7 @@ impl<'a> Resolver<'a> {
18061806
}
18071807

18081808
let kind = match def {
1809+
DefTy(_, true) => EnumModuleKind,
18091810
DefStruct(..) | DefTy(..) => ImplModuleKind,
18101811
_ => NormalModuleKind
18111812
};
@@ -1840,6 +1841,7 @@ impl<'a> Resolver<'a> {
18401841

18411842
match def {
18421843
DefMod(_) | DefForeignMod(_) => {}
1844+
// Still here for staging
18431845
DefVariant(enum_did, variant_id, is_struct) => {
18441846
debug!("(building reduced graph for external crate) building \
18451847
variant {}",
@@ -1905,6 +1907,35 @@ impl<'a> Resolver<'a> {
19051907
is_public,
19061908
DUMMY_SP)
19071909
}
1910+
DefTy(def_id, true) => { // enums
1911+
debug!("(building reduced graph for external crate) building enum {}", final_ident);
1912+
child_name_bindings.define_type(def, DUMMY_SP, is_public);
1913+
let enum_module = ModuleReducedGraphParent(child_name_bindings.get_module());
1914+
1915+
let variants = csearch::get_enum_variant_defs(&self.session.cstore, def_id);
1916+
for &(v_def, name, vis) in variants.iter() {
1917+
let (variant_id, is_struct) = match v_def {
1918+
DefVariant(_, variant_id, is_struct) => (variant_id, is_struct),
1919+
_ => unreachable!()
1920+
};
1921+
let child = self.add_child(name, enum_module.clone(),
1922+
OverwriteDuplicates,
1923+
DUMMY_SP);
1924+
1925+
// If this variant is public, then it was publicly reexported,
1926+
// otherwise we need to inherit the visibility of the enum
1927+
// definition.
1928+
let variant_exported = vis == ast::Public || is_exported;
1929+
if is_struct {
1930+
child.define_type(v_def, DUMMY_SP, variant_exported);
1931+
// Not adding fields for variants as they are not accessed with a self receiver
1932+
self.structs.insert(variant_id, Vec::new());
1933+
} else {
1934+
child.define_value(v_def, DUMMY_SP, variant_exported);
1935+
}
1936+
}
1937+
1938+
}
19081939
DefTy(..) | DefAssociatedTy(..) => {
19091940
debug!("(building reduced graph for external \
19101941
crate) building type {}", final_ident);

0 commit comments

Comments
 (0)