From dbc7042bfe690d55cbcd40e75851efd8af9bb34e Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 14 Apr 2019 12:37:22 +0300 Subject: [PATCH] Address review comments --- src/librustc_metadata/decoder.rs | 18 ++++++++++-------- .../variants_fictive_visibility.rs | 3 +++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/librustc_metadata/decoder.rs b/src/librustc_metadata/decoder.rs index e2464c517ae72..076e6590226e5 100644 --- a/src/librustc_metadata/decoder.rs +++ b/src/librustc_metadata/decoder.rs @@ -832,14 +832,16 @@ impl<'a, 'tcx> CrateMetadata { let ctor_kind = self.get_ctor_kind(child_index); let ctor_def = Def::Ctor(ctor_def_id, CtorOf::Variant, ctor_kind); 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); + if ctor_def_id == def_id && vis == ty::Visibility::Public { + // For non-exhaustive variants lower the constructor visibility to + // within the crate. We only need this for fictive constructors, + // for other constructors correct visibilities + // were already encoded in metadata. + let attrs = self.get_item_attrs(def_id.index, sess); + if attr::contains_name(&attrs, "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 index 9057592259ba6..62f6e4463f936 100644 --- a/src/test/ui/rfc-2008-non-exhaustive/variants_fictive_visibility.rs +++ b/src/test/ui/rfc-2008-non-exhaustive/variants_fictive_visibility.rs @@ -4,6 +4,9 @@ extern crate variants; const S: u8 = 0; + +// OK, `Struct` in value namespace is crate-private, so it's filtered away +// and there's no conflict with the previously defined `const S`. use variants::NonExhaustiveVariants::Struct as S; fn main() {}