-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)Area: Constant evaluation, covers all const contexts (static, const fn, ...)A-miriArea: The miri toolArea: The miri toolC-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
When loading discriminants of an enum that uses niche optimizations, Miri/CTFE has to do some arithmetic:
rust/src/librustc_mir/interpret/operand.rs
Lines 645 to 646 in bdd4bda
let adjusted_discr = raw_discr.wrapping_sub(niche_start) | |
.wrapping_add(variants_start); |
Currently, this happens on type u128
. That's probably wrong, it should happen on the type of the discriminant. That will result in different overflow behavior.
It's just addition and subtraction, so signed vs unsigned does not matter and we could just mask off the "too high" bits after each operation, as in:
rust/src/librustc_target/abi/mod.rs
Lines 664 to 669 in bdd4bda
let mask = !0u128 >> (128 - bits); | |
let start = *self.valid_range.start(); | |
let end = *self.valid_range.end(); | |
assert_eq!(start, start & mask); | |
assert_eq!(end, end & mask); | |
start..(end.wrapping_add(1) & mask) |
However, it might be more elegant to use our binary_*op
methods in operator.rs
.
Metadata
Metadata
Assignees
Labels
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)Area: Constant evaluation, covers all const contexts (static, const fn, ...)A-miriArea: The miri toolArea: The miri toolC-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.