Repository navigation
Fix dead-code marking dead enum variants as live - #166113
Merged
rust-bors[bot] merged 2 commits intoNov 19, 2026
Merged
Conversation
mu001999
reviewed
Nov 18, 2026
| PatKind::TupleStruct(qpath, ..) | PatKind::Struct(qpath, ..) => (qpath, p.hir_id), | ||
| _ => return true, | ||
| }; | ||
| if let Res::Def(_, id) = resolve_qpath(qpath, hir_id) { |
Member
There was a problem hiding this comment.
Why is the condition DefKind::Variant | DefKind::Ctor(CtorOf::Variant, ..) removed? IIUC, this change would include structs and their ctors, which is unnecessary.
Contributor
Author
There was a problem hiding this comment.
Dumb oversight on my part. Fixed.
Comment on lines
124
to
128
Member
There was a problem hiding this comment.
I think we can just pass a general f instead of resolve_qpath, then we can write the de-dup and insert logic in f directly, like:
let len = self.ignore_variant_stack.len();
arm.pat.necessary_variants(|qpath, hir_id| {
if let Res::Def(.., id) = self.typeck_results().qpath_res(qpath, hir_id)
&& duplicates.insert(id)
{
self.ignore_variant_stack.push(id);
}
});
let result = intravisit::walk_arm(self, arm);
self.ignore_variant_stack.truncate(len);
So that we can avoid the temporary vec. What do you think about this?
Contributor
Author
There was a problem hiding this comment.
Yes, that seems a lot cleaner, done.
Collaborator
|
Reminder, once the PR becomes ready for a review, use |
tehforsch
force-pushed
the
fix-ignore-variant-stack-self-type
branch
from
November 18, 2026 12:34
612c76e to
d88bc8a
Compare
Contributor
Author
|
@rustbot ready |
Contributor
rust-bors Bot
pushed a commit
that referenced
this pull request
Nov 18, 2026
…uwer Rollup of 11 pull requests Successful merges: - #165913 (MirPatch: reduce new statement insertion complexity) - #166081 (test `feature(asm_const_ptr)` with a vtable) - #166094 (Chain the spans of qualified paths instead of having a first-to-last scheme ) - #166113 (Fix dead-code marking dead enum variants as live) - #166118 (rustc_target: catch up with LLVM 24 movement on ppc64le bsd data layout) - #166155 (windows seek_write: document interaction with append mode) - #165935 (Duplicate attribute diagnostic cleanups) - #165948 (lint: Avoid unused_parens warning for ref patterns binding an identifier) - #166018 (`rustc_mir_build`: de-duplicate `local_temp_lifetime()`) - #166111 (Add `cd` alias to `std::env::set_current_dir`) - #166157 (Don't recover invalid literals in attribute parsing)
rust-bors Bot
pushed a commit
that referenced
this pull request
Nov 19, 2026
…uwer Rollup of 11 pull requests Successful merges: - #165913 (MirPatch: reduce new statement insertion complexity) - #166081 (test `feature(asm_const_ptr)` with a vtable) - #166094 (Chain the spans of qualified paths instead of having a first-to-last scheme ) - #166113 (Fix dead-code marking dead enum variants as live) - #166118 (rustc_target: catch up with LLVM 24 movement on ppc64le bsd data layout) - #166155 (windows seek_write: document interaction with append mode) - #165935 (Duplicate attribute diagnostic cleanups) - #165948 (lint: Avoid unused_parens warning for ref patterns binding an identifier) - #166018 (`rustc_mir_build`: de-duplicate `local_temp_lifetime()`) - #166111 (Add `cd` alias to `std::env::set_current_dir`) - #166157 (Don't recover invalid literals in attribute parsing)
rust-bors Bot
pushed a commit
that referenced
this pull request
Nov 19, 2026
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Selftype alias. As an example, see:This PR fixes this by resolving the
QPathof the pattern properly.As a result, the derived
Cloneimpl 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 theCloneimpl 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 theignore_variant_stackalso added a test that uses#[derive(Clone)]to test the ignored variant stack.However, after
rustc_trivial_field_readswas added (in #85200), this test didn't actually run into the dead variant stack anymore, since the derivedCloneimpl was ignored for dead-code analysis. So on currentmainit was possible to remove theignore_variant_stackentirely without a single test failing.r? @mu001999