You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 2ce8ec0
Browse filesBrowse the repository at this point in the historyBrowse files
Rollup merge of #166113 - tehforsch:fix-ignore-variant-stack-self-type, r=mu001999
Fix dead-code marking dead enum variants as live
This fixes a bug with the dead-code lint. Constructing an enum variant inside a match arm that matches on the same enum variant should not count to it being marked as live. This behavior was already implemented, but it only worked when the pattern matches on the enum type directly, not via the `Self` type alias. As an example, see:
```rust
enum Enum {
Variant1, // This is marked as dead
Variant2, // This isnt
}
impl Clone for Enum {
fn clone(&self) -> Self {
match self {
Enum::Variant1 => Enum::Variant1,
Self::Variant2 => Self::Variant2,
}
}
}
fn main() {
// keep enum itself live
let _: Enum = todo!();
}
```
This PR fixes this by resolving the `QPath` of the pattern properly.
As a result, the derived `Clone` impl for an enum like the above now doesn't result in the variants being kept alive at all. Previously, we'd get a note saying that a variant is constructed in the `Clone` impl but that this was being intentionally ignored (due to `#[rustc_trivial_field_reads]`), but now this note does not appear anymore.
Additionally, this PR also adds a test which actually uses the `ignore_variant_stack` (i.e. the mechanism responsible for preventing enum variants from being marked live in arms where we match on them). The original PR (#25961) which added the `ignore_variant_stack` also added a test that uses `#[derive(Clone)]` to test the ignored variant stack.
However, after `rustc_trivial_field_reads` was added (in #85200), this test didn't actually run into the dead variant stack anymore, since the derived `Clone` impl was ignored for dead-code analysis. So on current `main` it was possible to remove the `ignore_variant_stack` entirely without a single test failing.
r? @mu001999
0 commit comments