Skip to content

Commit 0833a89

Browse files
committed
resolve: refactor away PRIVATE_VARIANT and ensure that restricted
reexports of private variants are handled correctly.
1 parent a0c3ce3 commit 0833a89

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,9 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
291291
let module = self.new_module(parent_link, Some(def), false, vis);
292292
self.define(parent, name, TypeNS, (module, sp));
293293

294-
let variant_modifiers = match vis {
295-
ty::Visibility::Public => DefModifiers::empty(),
296-
_ => DefModifiers::PRIVATE_VARIANT,
297-
};
298294
for variant in &(*enum_definition).variants {
299295
let item_def_id = self.ast_map.local_def_id(item.id);
300-
self.build_reduced_graph_for_variant(variant, item_def_id,
301-
module, variant_modifiers);
296+
self.build_reduced_graph_for_variant(variant, item_def_id, module);
302297
}
303298
}
304299

@@ -358,8 +353,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
358353
fn build_reduced_graph_for_variant(&mut self,
359354
variant: &Variant,
360355
item_id: DefId,
361-
parent: Module<'b>,
362-
variant_modifiers: DefModifiers) {
356+
parent: Module<'b>) {
363357
let name = variant.node.name;
364358
if variant.node.data.is_struct() {
365359
// Not adding fields for variants as they are not accessed with a self receiver
@@ -369,12 +363,11 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
369363

370364
// Variants are always treated as importable to allow them to be glob used.
371365
// All variants are defined in both type and value namespaces as future-proofing.
372-
let modifiers = DefModifiers::IMPORTABLE | variant_modifiers;
366+
let modifiers = DefModifiers::IMPORTABLE;
373367
let def = Def::Variant(item_id, self.ast_map.local_def_id(variant.node.data.id()));
374-
let vis = ty::Visibility::Public;
375368

376-
self.define(parent, name, ValueNS, (def, variant.span, modifiers, vis));
377-
self.define(parent, name, TypeNS, (def, variant.span, modifiers, vis));
369+
self.define(parent, name, ValueNS, (def, variant.span, modifiers, parent.vis));
370+
self.define(parent, name, TypeNS, (def, variant.span, modifiers, parent.vis));
378371
}
379372

380373
/// Constructs the reduced graph for one foreign item.

src/librustc_resolve/lib.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -919,9 +919,6 @@ bitflags! {
919919
#[derive(Debug)]
920920
flags DefModifiers: u8 {
921921
const IMPORTABLE = 1 << 1,
922-
// Variants are considered `PUBLIC`, but some of them live in private enums.
923-
// We need to track them to prohibit reexports like `pub use PrivEnum::Variant`.
924-
const PRIVATE_VARIANT = 1 << 2,
925922
const GLOB_IMPORTED = 1 << 3,
926923
}
927924
}
@@ -932,8 +929,6 @@ pub struct NameBinding<'a> {
932929
modifiers: DefModifiers,
933930
kind: NameBindingKind<'a>,
934931
span: Option<Span>,
935-
// Enum variants are always considered `PUBLIC`, this is needed for `use Enum::Variant`
936-
// or `use Enum::*` to work on private enums.
937932
vis: ty::Visibility,
938933
}
939934

@@ -982,8 +977,20 @@ impl<'a> NameBinding<'a> {
982977
self.modifiers.contains(modifiers)
983978
}
984979

985-
fn is_public(&self) -> bool {
986-
self.vis == ty::Visibility::Public
980+
fn is_pseudo_public(&self) -> bool {
981+
self.pseudo_vis() == ty::Visibility::Public
982+
}
983+
984+
// We sometimes need to treat variants as `pub` for backwards compatibility
985+
fn pseudo_vis(&self) -> ty::Visibility {
986+
if self.is_variant() { ty::Visibility::Public } else { self.vis }
987+
}
988+
989+
fn is_variant(&self) -> bool {
990+
match self.kind {
991+
NameBindingKind::Def(Def::Variant(..)) => true,
992+
_ => false,
993+
}
987994
}
988995

989996
fn is_extern_crate(&self) -> bool {
@@ -3301,7 +3308,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
33013308
// only if both the module is public and the entity is
33023309
// declared as public (due to pruning, we don't explore
33033310
// outside crate private modules => no need to check this)
3304-
if !in_module_is_extern || name_binding.is_public() {
3311+
if !in_module_is_extern || name_binding.vis == ty::Visibility::Public {
33053312
lookup_results.push(path);
33063313
}
33073314
}
@@ -3326,7 +3333,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
33263333
_ => bug!(),
33273334
};
33283335

3329-
if !in_module_is_extern || name_binding.is_public() {
3336+
if !in_module_is_extern || name_binding.vis == ty::Visibility::Public {
33303337
// add the module to the lookup
33313338
let is_extern = in_module_is_extern || name_binding.is_extern_crate();
33323339
worklist.push((module, path_segments, is_extern));

src/librustc_resolve/resolve_imports.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl<'a> NameResolution<'a> {
184184
// the name, and (3) no public glob has defined the name, the resolution depends
185185
// on whether more globs can define the name.
186186
if !allow_private_imports && directive.vis != ty::Visibility::Public &&
187-
!self.binding.map(NameBinding::is_public).unwrap_or(false) {
187+
!self.binding.map(NameBinding::is_pseudo_public).unwrap_or(false) {
188188
return None;
189189
}
190190

@@ -242,7 +242,8 @@ impl<'a> ::ModuleS<'a> {
242242
if let Some(result) = resolution.try_result(ns, allow_private_imports) {
243243
// If the resolution doesn't depend on glob definability, check privacy and return.
244244
return result.and_then(|binding| {
245-
let allowed = allow_private_imports || !binding.is_import() || binding.is_public();
245+
let allowed = allow_private_imports || !binding.is_import() ||
246+
binding.is_pseudo_public();
246247
if allowed { Success(binding) } else { Failed(None) }
247248
});
248249
}
@@ -336,7 +337,7 @@ impl<'a> ::ModuleS<'a> {
336337
}
337338

338339
fn define_in_glob_importers(&self, name: Name, ns: Namespace, binding: &'a NameBinding<'a>) {
339-
if !binding.defined_with(DefModifiers::IMPORTABLE) || !binding.is_public() { return }
340+
if !binding.defined_with(DefModifiers::IMPORTABLE) || !binding.is_pseudo_public() { return }
340341
for &(importer, directive) in self.glob_importers.borrow_mut().iter() {
341342
let _ = importer.try_define_child(name, ns, directive.import(binding, None));
342343
}
@@ -569,7 +570,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
569570

570571
let ast_map = self.resolver.ast_map;
571572
match (&value_result, &type_result) {
572-
(&Success(binding), _) if !binding.vis.is_at_least(directive.vis, ast_map) &&
573+
(&Success(binding), _) if !binding.pseudo_vis().is_at_least(directive.vis, ast_map) &&
573574
self.resolver.is_accessible(binding.vis) => {
574575
let msg = format!("`{}` is private, and cannot be reexported", source);
575576
let note_msg = format!("consider marking `{}` as `pub` in the imported module",
@@ -579,7 +580,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
579580
.emit();
580581
}
581582

582-
(_, &Success(binding)) if !binding.vis.is_at_least(directive.vis, ast_map) &&
583+
(_, &Success(binding)) if !binding.pseudo_vis().is_at_least(directive.vis, ast_map) &&
583584
self.resolver.is_accessible(binding.vis) => {
584585
if binding.is_extern_crate() {
585586
let msg = format!("extern crate `{}` is private, and cannot be reexported \
@@ -661,7 +662,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
661662
resolution.borrow().binding().map(|binding| (*name, binding))
662663
}).collect::<Vec<_>>();
663664
for ((name, ns), binding) in bindings {
664-
if binding.defined_with(DefModifiers::IMPORTABLE) && binding.is_public() {
665+
if binding.defined_with(DefModifiers::IMPORTABLE) && binding.is_pseudo_public() {
665666
let _ = module_.try_define_child(name, ns, directive.import(binding, None));
666667
}
667668
}
@@ -697,15 +698,16 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
697698
None => continue,
698699
};
699700

700-
if binding.is_public() && (binding.is_import() || binding.is_extern_crate()) {
701+
if binding.vis == ty::Visibility::Public &&
702+
(binding.is_import() || binding.is_extern_crate()) {
701703
if let Some(def) = binding.def() {
702704
reexports.push(Export { name: name, def_id: def.def_id() });
703705
}
704706
}
705707

706708
if let NameBindingKind::Import { binding: orig_binding, id, .. } = binding.kind {
707-
if ns == TypeNS && binding.is_public() &&
708-
orig_binding.defined_with(DefModifiers::PRIVATE_VARIANT) {
709+
if ns == TypeNS && orig_binding.is_variant() &&
710+
!orig_binding.vis.is_at_least(binding.vis, &self.resolver.ast_map) {
709711
let msg = format!("variant `{}` is private, and cannot be reexported \
710712
(error E0364), consider declaring its enum as `pub`",
711713
name);

0 commit comments

Comments
 (0)