@@ -26,15 +26,20 @@ pub(crate) fn codegen_set_discriminant<'tcx>(
2626 variants : _,
2727 } => {
2828 let ptr = place. place_field ( fx, mir:: Field :: new ( tag_field) ) ;
29- let to = ty:: ScalarInt :: try_from_uint (
30- layout
31- . ty
32- . discriminant_for_variant ( fx. tcx , variant_index)
33- . unwrap ( )
34- . val ,
35- ptr. layout ( ) . size ,
36- )
37- . unwrap ( ) ;
29+ let to = layout
30+ . ty
31+ . discriminant_for_variant ( fx. tcx , variant_index)
32+ . unwrap ( )
33+ . val ;
34+ let to = if ptr. layout ( ) . abi . is_signed ( ) {
35+ ty:: ScalarInt :: try_from_int (
36+ ptr. layout ( ) . size . sign_extend ( to) as i128 ,
37+ ptr. layout ( ) . size ,
38+ )
39+ . unwrap ( )
40+ } else {
41+ ty:: ScalarInt :: try_from_uint ( to, ptr. layout ( ) . size ) . unwrap ( )
42+ } ;
3843 let discr = CValue :: const_val ( fx, ptr. layout ( ) , to) ;
3944 ptr. write_cvalue ( fx, discr) ;
4045 }
@@ -52,8 +57,12 @@ pub(crate) fn codegen_set_discriminant<'tcx>(
5257 if variant_index != dataful_variant {
5358 let niche = place. place_field ( fx, mir:: Field :: new ( tag_field) ) ;
5459 let niche_value = variant_index. as_u32 ( ) - niche_variants. start ( ) . as_u32 ( ) ;
55- let niche_value = u128:: from ( niche_value) . wrapping_add ( niche_start) ;
56- let niche_llval = CValue :: const_val ( fx, niche. layout ( ) , niche_value. into ( ) ) ;
60+ let niche_value = ty:: ScalarInt :: try_from_uint (
61+ u128:: from ( niche_value) . wrapping_add ( niche_start) ,
62+ niche. layout ( ) . size ,
63+ )
64+ . unwrap ( ) ;
65+ let niche_llval = CValue :: const_val ( fx, niche. layout ( ) , niche_value) ;
5766 niche. write_cvalue ( fx, niche_llval) ;
5867 }
5968 }
@@ -81,7 +90,16 @@ pub(crate) fn codegen_get_discriminant<'tcx>(
8190 . ty
8291 . discriminant_for_variant ( fx. tcx , * index)
8392 . map_or ( u128:: from ( index. as_u32 ( ) ) , |discr| discr. val ) ;
84- return CValue :: const_val ( fx, dest_layout, discr_val. into ( ) ) ;
93+ let discr_val = if dest_layout. abi . is_signed ( ) {
94+ ty:: ScalarInt :: try_from_int (
95+ dest_layout. size . sign_extend ( discr_val) as i128 ,
96+ dest_layout. size ,
97+ )
98+ . unwrap ( )
99+ } else {
100+ ty:: ScalarInt :: try_from_uint ( discr_val, dest_layout. size ) . unwrap ( )
101+ } ;
102+ return CValue :: const_val ( fx, dest_layout, discr_val) ;
85103 }
86104 Variants :: Multiple {
87105 tag,
0 commit comments