|
1 | 1 | use rustc_abi::Primitive::{Int, Pointer}; |
2 | 2 | use rustc_abi::{Align, FieldsShape, Size, TagEncoding, 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}; |
@@ -388,13 +389,17 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> { |
388 | 389 | let niche_llty = bx.cx().immediate_backend_type(niche.layout); |
389 | 390 | let niche_value = variant_index.as_u32() - niche_variants.start().as_u32(); |
390 | 391 | 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"); |
397 | 395 | }; |
| 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 | + ); |
398 | 403 | OperandValue::Immediate(niche_llval).store(bx, niche); |
399 | 404 | } |
400 | 405 | } |
|
0 commit comments