diff --git a/src/librustc_metadata/decoder.rs b/src/librustc_metadata/decoder.rs index f456a5c1619..e2464c517ae 100644 --- a/src/librustc_metadata/decoder.rs +++ b/src/librustc_metadata/decoder.rs @@ -831,7 +831,16 @@ impl<'a, 'tcx> CrateMetadata { let ctor_def_id = self.get_ctor_def_id(child_index).unwrap_or(def_id); let ctor_kind = self.get_ctor_kind(child_index); let ctor_def = Def::Ctor(ctor_def_id, CtorOf::Variant, ctor_kind); - let vis = self.get_visibility(ctor_def_id.index); + let mut vis = self.get_visibility(ctor_def_id.index); + // If the variant is marked as non_exhaustive then lower the visibility + // to within the crate. + let has_non_exhaustive = || { attr::contains_name( + &self.get_item_attrs(def_id.index, sess), "non_exhaustive" + )}; + if vis == ty::Visibility::Public && has_non_exhaustive() { + let crate_def_id = DefId { index: CRATE_DEF_INDEX, ..def_id }; + vis = ty::Visibility::Restricted(crate_def_id); + } callback(def::Export { def: ctor_def, ident, vis, span }); } _ => {} diff --git a/src/test/ui/rfc-2008-non-exhaustive/variants_fictive_visibility.rs b/src/test/ui/rfc-2008-non-exhaustive/variants_fictive_visibility.rs new file mode 100644 index 00000000000..9057592259b --- /dev/null +++ b/src/test/ui/rfc-2008-non-exhaustive/variants_fictive_visibility.rs @@ -0,0 +1,9 @@ +// compile-pass +// aux-build:variants.rs + +extern crate variants; + +const S: u8 = 0; +use variants::NonExhaustiveVariants::Struct as S; + +fn main() {}