Skip to content

Commit 1b80892

Browse files
committed
use stores of the correct size to set discriminants
1 parent 0892dbd commit 1b80892

File tree

1 file changed

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

1 file changed

+12
-7
lines changed

compiler/rustc_codegen_ssa/src/mir/place.rs

+12-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,17 @@ 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+
let wrapped = niche_value & max;
397+
let niche_llval = bx.cx().scalar_to_backend(
398+
Scalar::from_uint(wrapped, niche.layout.size),
399+
scalar,
400+
niche_llty,
401+
);
397402
OperandValue::Immediate(niche_llval).store(bx, niche);
398403
}
399404
}

0 commit comments

Comments
 (0)