diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index cdfde2b9405..15b4df45b01 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -529,15 +529,16 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> { let tcx = self.tcx; let unconditionally_treat_fields_as_live = self.repr_unconditionally_treats_fields_as_live; let has_repr_simd = self.repr_has_repr_simd; + let effective_visibilities = &tcx.effective_visibilities(()); let live_fields = def.fields().iter().filter_map(|f| { let def_id = f.def_id; if unconditionally_treat_fields_as_live || (f.is_positional() && has_repr_simd) { return Some(def_id); } - if !tcx.visibility(f.hir_id.owner.def_id).is_public() { + if !effective_visibilities.is_reachable(f.hir_id.owner.def_id) { return None; } - if tcx.visibility(def_id).is_public() { Some(def_id) } else { None } + if effective_visibilities.is_reachable(def_id) { Some(def_id) } else { None } }); self.live_symbols.extend(live_fields); diff --git a/tests/ui/lint/dead-code/pub-field-in-priv-mod.rs b/tests/ui/lint/dead-code/pub-field-in-priv-mod.rs new file mode 100644 index 00000000000..e49a164e940 --- /dev/null +++ b/tests/ui/lint/dead-code/pub-field-in-priv-mod.rs @@ -0,0 +1,11 @@ +#![deny(dead_code)] + +fn main() { + let _ = foo::S{f: false}; +} + +mod foo { + pub struct S { + pub f: bool, //~ ERROR field `f` is never read + } +} diff --git a/tests/ui/lint/dead-code/pub-field-in-priv-mod.stderr b/tests/ui/lint/dead-code/pub-field-in-priv-mod.stderr new file mode 100644 index 00000000000..11dd387315f --- /dev/null +++ b/tests/ui/lint/dead-code/pub-field-in-priv-mod.stderr @@ -0,0 +1,16 @@ +error: field `f` is never read + --> $DIR/pub-field-in-priv-mod.rs:9:13 + | +LL | pub struct S { + | - field in this struct +LL | pub f: bool, + | ^ + | +note: the lint level is defined here + --> $DIR/pub-field-in-priv-mod.rs:1:9 + | +LL | #![deny(dead_code)] + | ^^^^^^^^^ + +error: aborting due to 1 previous error +