Skip to content

Commit b8d4b75

Browse files
committed
use stores of the correct size to set discriminants
1 parent 97b84e4 commit b8d4b75

File tree

1 file changed

+16
-7
lines changed
  • compiler/rustc_codegen_ssa/src/mir

1 file changed

+16
-7
lines changed

compiler/rustc_codegen_ssa/src/mir/place.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_abi::Primitive::{Int, Pointer};
2-
use rustc_abi::{Align, FieldsShape, Size, TagEncoding, VariantIdx, Variants};
2+
use rustc_abi::{Align, BackendRepr, FieldsShape, Size, TagEncoding, VariantIdx, Variants};
3+
use rustc_middle::mir::interpret::Scalar;
34
use rustc_middle::mir::tcx::PlaceTy;
45
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf, TyAndLayout};
56
use rustc_middle::ty::{self, Ty};
@@ -387,13 +388,21 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
387388
let niche_llty = bx.cx().immediate_backend_type(niche.layout);
388389
let niche_value = variant_index.as_u32() - niche_variants.start().as_u32();
389390
let niche_value = (niche_value as u128).wrapping_add(niche_start);
390-
// FIXME(eddyb): check the actual primitive type here.
391-
let niche_llval = if niche_value == 0 {
392-
// HACK(eddyb): using `c_null` as it works on all types.
393-
bx.cx().const_null(niche_llty)
394-
} else {
395-
bx.cx().const_uint_big(niche_llty, niche_value)
391+
392+
let BackendRepr::Scalar(scalar) = niche.layout.backend_repr else {
393+
bug!("expected a scalar placeref for the niche");
396394
};
395+
let max = niche.layout.size.unsigned_int_max();
396+
// Niche ranges that wrap around may be represented as out-of-range values
397+
// e.g. niche at 0u8 may end up being u8::MAX + 1.
398+
// This was previously handled by backends by masking them back into range
399+
// but that didn't work for pointer-typed scalars. So we do the masking ourselves.
400+
let wrapped = niche_value & max;
401+
let niche_llval = bx.cx().scalar_to_backend(
402+
Scalar::from_uint(wrapped, niche.layout.size),
403+
scalar,
404+
niche_llty,
405+
);
397406
OperandValue::Immediate(niche_llval).store(bx, niche);
398407
}
399408
}

0 commit comments

Comments
 (0)