Skip to content

Commit 036b79a

Browse files
committed
use stores of the correct size to set discriminants
1 parent 2aa26d8 commit 036b79a

File tree

1 file changed

+12
-5
lines changed
  • compiler/rustc_codegen_ssa/src/mir

1 file changed

+12
-5
lines changed

compiler/rustc_codegen_ssa/src/mir/place.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_abi::Primitive::{Int, Pointer};
22
use rustc_abi::{Align, FieldsShape, Size, TagEncoding, 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};
@@ -389,12 +390,18 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
389390
let niche_value = variant_index.as_u32() - niche_variants.start().as_u32();
390391
let niche_value = (niche_value as u128).wrapping_add(niche_start);
391392
// FIXME(eddyb): check the actual primitive type here.
392-
let niche_llval = if niche_value == 0 {
393-
// HACK(eddyb): using `c_null` as it works on all types.
394-
bx.cx().const_null(niche_llty)
395-
} else {
396-
bx.cx().const_uint_big(niche_llty, niche_value)
393+
394+
let rustc_abi::Abi::Scalar(scalar) = niche.layout.abi else {
395+
bug!("expected a scalar placeref for the niche");
397396
};
397+
398+
let max = niche.layout.size.unsigned_int_max();
399+
let wrapped = niche_value & max;
400+
let niche_llval = bx.cx().scalar_to_backend(
401+
Scalar::from_uint(wrapped, niche.layout.size),
402+
scalar,
403+
niche_llty,
404+
);
398405
OperandValue::Immediate(niche_llval).store(bx, niche);
399406
}
400407
}

0 commit comments

Comments
 (0)