diff options
| author | Arthur Carcano <arthur.carcano@ocamlpro.com> | 2024-01-03 17:39:16 +0100 |
|---|---|---|
| committer | Arthur Carcano <arthur.carcano@ocamlpro.com> | 2024-03-12 10:59:40 +0100 |
| commit | a0fe4138ed83b9c3f6e1c85ef6e6a0846b1e542d (patch) | |
| tree | 8584562ee54726efcb82f8e10e6eef549ae1e25f | |
| parent | b0170b693ef91460c97ff9ea5e360888466611dd (diff) | |
| download | rust-a0fe4138ed83b9c3f6e1c85ef6e6a0846b1e542d.tar.gz rust-a0fe4138ed83b9c3f6e1c85ef6e6a0846b1e542d.zip | |
Replace visibility test with reachability test in dead code detection
Fixes https://github.com/rust-lang/rust/issues/119545
| -rw-r--r-- | compiler/rustc_passes/src/dead.rs | 5 | ||||
| -rw-r--r-- | tests/ui/lint/dead-code/pub-field-in-priv-mod.rs | 11 | ||||
| -rw-r--r-- | tests/ui/lint/dead-code/pub-field-in-priv-mod.stderr | 16 |
3 files changed, 30 insertions, 2 deletions
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 + |
