Skip to content

Commit e7b2ca9

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

File tree

1 file changed

+11
-6
lines changed
  • compiler/rustc_codegen_ssa/src/mir

1 file changed

+11
-6
lines changed

compiler/rustc_codegen_ssa/src/mir/place.rs

+11-6
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};
@@ -388,13 +389,17 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
388389
let niche_llty = bx.cx().immediate_backend_type(niche.layout);
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);
391-
// 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)
392+
393+
let rustc_abi::Abi::Scalar(scalar) = niche.layout.abi else {
394+
bug!("expected a scalar placeref for the niche");
397395
};
396+
let max = niche.layout.size.unsigned_int_max();
397+
let wrapped = niche_value & max;
398+
let niche_llval = bx.cx().scalar_to_backend(
399+
Scalar::from_uint(wrapped, niche.layout.size),
400+
scalar,
401+
niche_llty,
402+
);
398403
OperandValue::Immediate(niche_llval).store(bx, niche);
399404
}
400405
}

0 commit comments

Comments
 (0)