Skip to content

Commit

Permalink
Replace visibility test with reachability test in dead code detection
Browse files Browse the repository at this point in the history
Fixes #119545
  • Loading branch information
krtab committed Jan 3, 2024
1 parent e51e98d commit f1ff461
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/lint/dead-code/pub-field-in-priv-mod.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
16 changes: 16 additions & 0 deletions tests/ui/lint/dead-code/pub-field-in-priv-mod.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f1ff461

Please sign in to comment.