From f1ff461d118fb8de54ef2a6a9937c0e9e6ce25d8 Mon Sep 17 00:00:00 2001 From: Arthur Carcano Date: Wed, 3 Jan 2024 17:39:16 +0100 Subject: [PATCH] Replace visibility test with reachability test in dead code detection Fixes https://github.com/rust-lang/rust/issues/119545 --- compiler/rustc_passes/src/dead.rs | 3 ++- tests/ui/lint/dead-code/pub-field-in-priv-mod.rs | 11 +++++++++++ .../lint/dead-code/pub-field-in-priv-mod.stderr | 16 ++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/ui/lint/dead-code/pub-field-in-priv-mod.rs create mode 100644 tests/ui/lint/dead-code/pub-field-in-priv-mod.stderr diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index 22aac1e775e6f..c335b2741bee2 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -434,12 +434,13 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> { let tcx = self.tcx; let has_repr_c = self.repr_has_repr_c; 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 has_repr_c || (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 } 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 0000000000000..e49a164e9401b --- /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 0000000000000..11dd387315fbc --- /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 +