Skip to content

Commit 26d7b5d

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

File tree

1 file changed

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

1 file changed

+16
-8
lines changed

compiler/rustc_codegen_ssa/src/mir/place.rs

+16-8
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};
@@ -385,15 +386,22 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
385386
if variant_index != untagged_variant {
386387
let niche = self.project_field(bx, tag_field);
387388
let niche_llty = bx.cx().immediate_backend_type(niche.layout);
389+
let BackendRepr::Scalar(scalar) = niche.layout.backend_repr else {
390+
bug!("expected a scalar placeref for the niche");
391+
};
392+
// We are supposed to compute `niche_value.wrapping_add(niche_start)` wrapping
393+
// around the `niche`'s type.
394+
// The easiest way to do that is to do wrapping arithmetic on `u128` and then
395+
// masking off any extra bits that occur because we did the arithmetic with too many bits.
388396
let niche_value = variant_index.as_u32() - niche_variants.start().as_u32();
389397
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)
396-
};
398+
let niche_value = niche_value & niche.layout.size.unsigned_int_max();
399+
400+
let niche_llval = bx.cx().scalar_to_backend(
401+
Scalar::from_uint(niche_value, niche.layout.size),
402+
scalar,
403+
niche_llty,
404+
);
397405
OperandValue::Immediate(niche_llval).store(bx, niche);
398406
}
399407
}

0 commit comments

Comments
 (0)