Replace visibility test with reachability test in dead code detection

Fixes https://github.com/rust-lang/rust/issues/119545
This commit is contained in:
Arthur Carcano 2024-01-03 17:39:16 +01:00
parent b0170b693e
commit a0fe4138ed
3 changed files with 30 additions and 2 deletions

View File

@ -529,15 +529,16 @@ fn visit_variant_data(&mut self, def: &'tcx hir::VariantData<'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);

View File

@ -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
}
}

View File

@ -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