Extend dead code analysis of impls and impl items to non-ADTs#142968
Extend dead code analysis of impls and impl items to non-ADTs#142968mu001999 wants to merge 1 commit intorust-lang:masterfrom
Conversation
compiler/rustc_passes/src/dead.rs
Outdated
| || assoc_item.trait_item_def_id.is_none() | ||
| } | ||
| _ => true, | ||
| }) |
There was a problem hiding this comment.
How is this change related to supporting non-ADTs?
Isn't this reverting a part of changes from #141407?
The logic is again spread randomly between the various analysis stages, what is the principle behind all this, what is filtered away or analyzed at what stage?
There was a problem hiding this comment.
Removed this and edited the reachable analysis in the new change
There was a problem hiding this comment.
The logic here is removing impl ReachableTrait for ReachableTy<PrivateTy> from the seed worklist. Because the reachable analysis considers impl ReachableTrait for ReachableTy<UnreachableTy> reachable due to the trait and the "shallow" type are both reachable, but this will also mark impl ReachableTrait for ReachableTy<PrivateTy> and impl ReachableTrait for [PrivateTy]reachable. The latter is not expected.
There was a problem hiding this comment.
impl ReachableTrait for ReachableTy<PrivateTy> and impl ReachableTrait for [PrivateTy] may indeed be reachable due to type inference, even it's not intuitive.
(But they will produce an error on use.)
I need to check though, whether we mark private items as reachable in other, non-impl cases.
If we do, then private impls can probably be treated as non-reachable as well.
This is a change that may have language implications.
28d6722 to
9242dd0
Compare
9242dd0 to
7e29893
Compare
|
@rustbot ready |
|
The cases reported by dead code lint in this PR are most similar to this case. struct Priv;
pub fn leak_priv() -> Priv { Priv }
On the other hand marking it reachable is exactly what allows the So what we really need is two different "effective visibility" tables, one with the rules making |
|
(I'll think about this more tomorrow.) |
|
I'm thinking about whether it's correct to extend to non-ADT. Considering the following: #![deny(dead_code)]
struct T;
fn foo(_: &[T]) {}
struct U;
pub trait Bar {
fn bar(&self);
}
impl Bar for [U] {
fn bar(&self) {}
}
fn main() {
foo(&[]);
[].bar();
}This PR will mark |
|
@mu001999 |
|
Ok, this extension has enough problems, let's not do this. |
Then we can lint more unused structs and traits like the following:
Extracted from #128637.
r? @petrochenkov