Skip to content

fix: const eval cast of single-variant enum - #23185

Open
hlsxx wants to merge 2 commits into
rust-lang:masterfrom
hlsxx:enum-dicr-evaluation
Open

hlsxx wants to merge 2 commits into
rust-lang:masterfrom
hlsxx:enum-dicr-evaluation

Conversation

@hlsxx

@hlsxx hlsxx commented Aug 18, 2026

Copy link
Copy Markdown

Single-variant enum incorrectly evaluated to 0.

Previously enum-to-int casts were lowered as a plain Rvalue::Cast, causing single-variant enums to evaluate to 0 instead of their corresponding discriminant value.

Fixes enum discriminant handling in MIR lowering by extracting the discriminant before performing the integer cast. Prevents recursion in MIR evaluation by reading discriminant bytes directly when the owner is a variant of the same enum in Rvalue::Discriminant branch.

Fixes #23148

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 18, 2026

@ChayimFriedman2 ChayimFriedman2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't the correct way to fix that. It should be fixed during MIR lowering, see the corresponding code in rustc: https://github.com/rust-lang/rust/blob/e71c0f1e3395b10a8c331317be1a5c107bdf7b2e/compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs#L125-L163.

View changes since this review

@hlsxx
hlsxx force-pushed the enum-dicr-evaluation branch from 6e61250 to 5627c1c Compare August 28, 2026 16:44
@rustbot

This comment has been minimized.

@hlsxx
hlsxx force-pushed the enum-dicr-evaluation branch 3 times, most recently from b36d82e to b32e18a Compare August 29, 2026 10:37
@hlsxx
hlsxx requested a review from ChayimFriedman2 August 29, 2026 10:54
Comment thread crates/hir-ty/src/mir/eval.rs Outdated
@hlsxx
hlsxx force-pushed the enum-dicr-evaluation branch 2 times, most recently from 7314abf to 199e5d4 Compare September 14, 2026 19:48
@rustbot

This comment has been minimized.

When an enum variant discriminant
initializer references another variant via a cast, e.g. `B = Foo::A as isize + 1`
the previous implementation would lower `Foo::A` as a place and then extract its
discriminant via `Rvalue::Discriminant`.
This caused infinite recursion during consteval because evaluating the discriminant of one variant requires evaluating another.

# Conflicts:
#	crates/hir-ty/src/mir/lower.rs
@hlsxx
hlsxx force-pushed the enum-dicr-evaluation branch from 199e5d4 to 185846c Compare September 15, 2026 17:09
@rustbot

rustbot commented Sep 15, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FP expected [i32; 0], found [i32; 4] wrong array size infer

3 participants