|
1 | 1 | 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; |
3 | 4 | use rustc_middle::mir::tcx::PlaceTy;
|
4 | 5 | use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf, TyAndLayout};
|
5 | 6 | use rustc_middle::ty::{self, Ty};
|
@@ -385,15 +386,22 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
|
385 | 386 | if variant_index != untagged_variant {
|
386 | 387 | let niche = self.project_field(bx, tag_field);
|
387 | 388 | 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. |
388 | 396 | let niche_value = variant_index.as_u32() - niche_variants.start().as_u32();
|
389 | 397 | 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 | + ); |
397 | 405 | OperandValue::Immediate(niche_llval).store(bx, niche);
|
398 | 406 | }
|
399 | 407 | }
|
|
0 commit comments