@@ -26,15 +26,20 @@ pub(crate) fn codegen_set_discriminant<'tcx>(
26
26
variants : _,
27
27
} => {
28
28
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
+ } ;
38
43
let discr = CValue :: const_val ( fx, ptr. layout ( ) , to) ;
39
44
ptr. write_cvalue ( fx, discr) ;
40
45
}
@@ -52,8 +57,12 @@ pub(crate) fn codegen_set_discriminant<'tcx>(
52
57
if variant_index != dataful_variant {
53
58
let niche = place. place_field ( fx, mir:: Field :: new ( tag_field) ) ;
54
59
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) ;
57
66
niche. write_cvalue ( fx, niche_llval) ;
58
67
}
59
68
}
@@ -81,7 +90,16 @@ pub(crate) fn codegen_get_discriminant<'tcx>(
81
90
. ty
82
91
. discriminant_for_variant ( fx. tcx , * index)
83
92
. 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) ;
85
103
}
86
104
Variants :: Multiple {
87
105
tag,
0 commit comments