Skip to content

Commit 9506f62

Browse files
authored
Rollup merge of rust-lang#59936 - petrochenkov:confict, r=davidtwco
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
2 parents 271eb8f + dbc7042 commit 9506f62

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/librustc_metadata/decoder.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,18 @@ impl<'a, 'tcx> CrateMetadata {
831831
let ctor_def_id = self.get_ctor_def_id(child_index).unwrap_or(def_id);
832832
let ctor_kind = self.get_ctor_kind(child_index);
833833
let ctor_def = Def::Ctor(ctor_def_id, CtorOf::Variant, ctor_kind);
834-
let vis = self.get_visibility(ctor_def_id.index);
834+
let mut vis = self.get_visibility(ctor_def_id.index);
835+
if ctor_def_id == def_id && vis == ty::Visibility::Public {
836+
// For non-exhaustive variants lower the constructor visibility to
837+
// within the crate. We only need this for fictive constructors,
838+
// for other constructors correct visibilities
839+
// were already encoded in metadata.
840+
let attrs = self.get_item_attrs(def_id.index, sess);
841+
if attr::contains_name(&attrs, "non_exhaustive") {
842+
let crate_def_id = DefId { index: CRATE_DEF_INDEX, ..def_id };
843+
vis = ty::Visibility::Restricted(crate_def_id);
844+
}
845+
}
835846
callback(def::Export { def: ctor_def, ident, vis, span });
836847
}
837848
_ => {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// compile-pass
2+
// aux-build:variants.rs
3+
4+
extern crate variants;
5+
6+
const S: u8 = 0;
7+
8+
// OK, `Struct` in value namespace is crate-private, so it's filtered away
9+
// and there's no conflict with the previously defined `const S`.
10+
use variants::NonExhaustiveVariants::Struct as S;
11+
12+
fn main() {}

0 commit comments

Comments
 (0)