Skip to content

Commit

Permalink
Rollup merge of rust-lang#59936 - petrochenkov:confict, r=davidtwco
Browse files Browse the repository at this point in the history
Fix cross-crate visibility of fictive variant constructors

After merging rust-lang#59376 I realized that the code in the decoder wasn't entirely correct - we "decoded" fictive variant constructors with their variant's visibility, which could be public, rather than demoted to `pub(crate)`.

Fictive constructors are not directly usable in expression/patterns, but the effect still can be observed with imports.

r? @davidtwco
  • Loading branch information
Centril authored Apr 14, 2019
2 parents 72d28ff + dbc7042 commit 3174c5c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,18 @@ 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 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 });
}
_ => {}
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/rfc-2008-non-exhaustive/variants_fictive_visibility.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// compile-pass
// aux-build:variants.rs

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() {}

0 comments on commit 3174c5c

Please sign in to comment.