@@ -105,16 +105,18 @@ macro_rules! simple_op {
105
105
if let Some ( const_lhs) = self . try_get_const_value( lhs)
106
106
&& let Some ( const_rhs) = self . try_get_const_value( rhs)
107
107
{
108
- #[ allow( unreachable_patterns) ]
109
- match ( const_lhs, const_rhs) {
108
+ let result = ( || Some ( match ( const_lhs, const_rhs) {
110
109
$(
111
- ( ConstValue :: Unsigned ( $int_lhs) , ConstValue :: Unsigned ( $int_rhs) ) => return self . const_uint_big ( result_type , $fold_int) ,
112
- ( ConstValue :: Signed ( $int_lhs) , ConstValue :: Signed ( $int_rhs) ) => return self . const_uint_big ( result_type , $fold_int as u128 ) ,
110
+ ( ConstValue :: Unsigned ( $int_lhs) , ConstValue :: Unsigned ( $int_rhs) ) => $fold_int,
111
+ ( ConstValue :: Signed ( $int_lhs) , ConstValue :: Signed ( $int_rhs) ) => $fold_int as u128 ,
113
112
) ?
114
- $( ( ConstValue :: Unsigned ( $uint_lhs) , ConstValue :: Unsigned ( $uint_rhs) ) => return self . const_uint_big( result_type, $fold_uint) , ) ?
115
- $( ( ConstValue :: Signed ( $sint_lhs) , ConstValue :: Signed ( $sint_rhs) ) => return self . const_uint_big( result_type, $fold_sint as u128 ) , ) ?
116
- $( ( ConstValue :: Bool ( $bool_lhs) , ConstValue :: Bool ( $bool_rhs) ) => return self . const_uint_big( result_type, ( $fold_bool) . into( ) ) , ) ?
117
- _ => ( ) ,
113
+ $( ( ConstValue :: Unsigned ( $uint_lhs) , ConstValue :: Unsigned ( $uint_rhs) ) => $fold_uint, ) ?
114
+ $( ( ConstValue :: Signed ( $sint_lhs) , ConstValue :: Signed ( $sint_rhs) ) => $fold_sint as u128 , ) ?
115
+ $( ( ConstValue :: Bool ( $bool_lhs) , ConstValue :: Bool ( $bool_rhs) ) => ( $fold_bool) . into( ) , ) ?
116
+ _ => return None ,
117
+ } ) ) ( ) ;
118
+ if let Some ( result) = result {
119
+ return self . const_uint_big( result_type, result) ;
118
120
}
119
121
}
120
122
) ?
@@ -172,23 +174,25 @@ macro_rules! simple_shift_op {
172
174
if let Some ( const_lhs) = self . try_get_const_value( lhs)
173
175
&& let Some ( const_rhs) = self . try_get_const_value( rhs)
174
176
{
175
- #[ allow( unreachable_patterns) ]
176
- match ( const_lhs, const_rhs) {
177
+ let result = ( || Some ( match ( const_lhs, const_rhs) {
177
178
$(
178
- ( ConstValue :: Unsigned ( $int_lhs) , ConstValue :: Unsigned ( $int_rhs) ) => return self . const_uint_big ( result_type , $fold_int) ,
179
- ( ConstValue :: Unsigned ( $int_lhs) , ConstValue :: Signed ( $int_rhs) ) => return self . const_uint_big ( result_type , $fold_int) ,
180
- ( ConstValue :: Signed ( $int_lhs) , ConstValue :: Unsigned ( $int_rhs) ) => return self . const_uint_big ( result_type , $fold_int as u128 ) ,
181
- ( ConstValue :: Signed ( $int_lhs) , ConstValue :: Signed ( $int_rhs) ) => return self . const_uint_big ( result_type , $fold_int as u128 ) ,
179
+ ( ConstValue :: Unsigned ( $int_lhs) , ConstValue :: Unsigned ( $int_rhs) ) => $fold_int,
180
+ ( ConstValue :: Unsigned ( $int_lhs) , ConstValue :: Signed ( $int_rhs) ) => $fold_int,
181
+ ( ConstValue :: Signed ( $int_lhs) , ConstValue :: Unsigned ( $int_rhs) ) => $fold_int as u128 ,
182
+ ( ConstValue :: Signed ( $int_lhs) , ConstValue :: Signed ( $int_rhs) ) => $fold_int as u128 ,
182
183
) ?
183
184
$(
184
- ( ConstValue :: Unsigned ( $uint_lhs) , ConstValue :: Unsigned ( $uint_rhs) ) => return self . const_uint_big ( result_type , $fold_uint) ,
185
- ( ConstValue :: Unsigned ( $uint_lhs) , ConstValue :: Signed ( $uint_rhs) ) => return self . const_uint_big ( result_type , $fold_uint) ,
185
+ ( ConstValue :: Unsigned ( $uint_lhs) , ConstValue :: Unsigned ( $uint_rhs) ) => $fold_uint,
186
+ ( ConstValue :: Unsigned ( $uint_lhs) , ConstValue :: Signed ( $uint_rhs) ) => $fold_uint,
186
187
) ?
187
188
$(
188
- ( ConstValue :: Signed ( $sint_lhs) , ConstValue :: Unsigned ( $sint_rhs) ) => return self . const_uint_big ( result_type , $fold_sint as u128 ) ,
189
- ( ConstValue :: Signed ( $sint_lhs) , ConstValue :: Signed ( $sint_rhs) ) => return self . const_uint_big ( result_type , $fold_sint as u128 ) ,
189
+ ( ConstValue :: Signed ( $sint_lhs) , ConstValue :: Unsigned ( $sint_rhs) ) => $fold_sint as u128 ,
190
+ ( ConstValue :: Signed ( $sint_lhs) , ConstValue :: Signed ( $sint_rhs) ) => $fold_sint as u128 ,
190
191
) ?
191
- _ => ( ) ,
192
+ _ => return None ,
193
+ } ) ) ( ) ;
194
+ if let Some ( result) = result {
195
+ return self . const_uint_big( result_type, result) ;
192
196
}
193
197
}
194
198
) ?
@@ -238,15 +242,18 @@ macro_rules! simple_uni_op {
238
242
$(
239
243
#[ allow( unreachable_patterns, clippy:: collapsible_match) ]
240
244
if let Some ( const_val) = self . try_get_const_value( val) {
241
- match const_val {
245
+ let result = ( || Some ( match ( const_val) {
242
246
$(
243
- ConstValue :: Unsigned ( $int_val) => return self . const_uint_big ( result_type , $fold_int) ,
244
- ConstValue :: Signed ( $int_val) => return self . const_uint_big ( result_type , $fold_int as u128 ) ,
247
+ ConstValue :: Unsigned ( $int_val) => $fold_int,
248
+ ConstValue :: Signed ( $int_val) => $fold_int as u128 ,
245
249
) ?
246
- $( ConstValue :: Unsigned ( $uint_val) => return self . const_uint_big( result_type, $fold_uint) , ) ?
247
- $( ConstValue :: Signed ( $sint_val) => return self . const_uint_big( result_type, $fold_sint as u128 ) , ) ?
248
- $( ConstValue :: Bool ( $bool_val) => return self . const_uint_big( result_type, ( $fold_bool) . into( ) ) , ) ?
249
- _ => ( ) ,
250
+ $( ConstValue :: Unsigned ( $uint_val) => $fold_uint, ) ?
251
+ $( ConstValue :: Signed ( $sint_val) => $fold_sint as u128 , ) ?
252
+ $( ConstValue :: Bool ( $bool_val) => ( $fold_bool) . into( ) , ) ?
253
+ _ => return None ,
254
+ } ) ) ( ) ;
255
+ if let Some ( result) = result {
256
+ return self . const_uint_big( result_type, result) ;
250
257
}
251
258
}
252
259
) ?
@@ -1538,7 +1545,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
1538
1545
add,
1539
1546
int: i_add,
1540
1547
fold_const {
1541
- int( a, b) => a. wrapping_add ( b) ;
1548
+ int( a, b) => a. checked_add ( b) ? ;
1542
1549
}
1543
1550
}
1544
1551
// FIXME(eddyb) try to annotate the SPIR-V for `fast` and `algebraic`.
@@ -1549,7 +1556,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
1549
1556
sub,
1550
1557
int: i_sub,
1551
1558
fold_const {
1552
- int( a, b) => a. wrapping_sub ( b) ;
1559
+ int( a, b) => a. checked_sub ( b) ? ;
1553
1560
}
1554
1561
}
1555
1562
simple_op ! { fsub, float: f_sub}
@@ -1559,7 +1566,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
1559
1566
mul,
1560
1567
int: i_mul,
1561
1568
fold_const {
1562
- int( a, b) => a. wrapping_mul ( b) ;
1569
+ int( a, b) => a. checked_mul ( b) ? ;
1563
1570
}
1564
1571
}
1565
1572
simple_op ! { fmul, float: f_mul}
@@ -1569,7 +1576,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
1569
1576
udiv,
1570
1577
uint: u_div,
1571
1578
fold_const {
1572
- uint( a, b) => a. wrapping_div ( b) ;
1579
+ uint( a, b) => a. checked_div ( b) ? ;
1573
1580
}
1574
1581
}
1575
1582
// Note: exactudiv is UB when there's a remainder, so it's valid to implement as a normal div.
@@ -1578,22 +1585,22 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
1578
1585
exactudiv,
1579
1586
uint: u_div,
1580
1587
fold_const {
1581
- uint( a, b) => a. wrapping_div ( b) ;
1588
+ uint( a, b) => a. checked_div ( b) ? ;
1582
1589
}
1583
1590
}
1584
1591
simple_op ! {
1585
1592
sdiv,
1586
1593
sint: s_div,
1587
1594
fold_const {
1588
- sint( a, b) => a. wrapping_div ( b) ;
1595
+ sint( a, b) => a. checked_div ( b) ? ;
1589
1596
}
1590
1597
}
1591
1598
// Same note and TODO as exactudiv
1592
1599
simple_op ! {
1593
1600
exactsdiv,
1594
1601
sint: s_div,
1595
1602
fold_const {
1596
- sint( a, b) => a. wrapping_div ( b) ;
1603
+ sint( a, b) => a. checked_div ( b) ? ;
1597
1604
}
1598
1605
}
1599
1606
simple_op ! { fdiv, float: f_div}
@@ -1603,14 +1610,14 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
1603
1610
urem,
1604
1611
uint: u_mod,
1605
1612
fold_const {
1606
- uint( a, b) => a. wrapping_rem ( b) ;
1613
+ uint( a, b) => a. checked_rem ( b) ? ;
1607
1614
}
1608
1615
}
1609
1616
simple_op ! {
1610
1617
srem,
1611
1618
sint: s_rem,
1612
1619
fold_const {
1613
- sint( a, b) => a. wrapping_rem ( b) ;
1620
+ sint( a, b) => a. checked_rem ( b) ? ;
1614
1621
}
1615
1622
}
1616
1623
simple_op ! { frem, float: f_rem}
@@ -1620,28 +1627,28 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
1620
1627
shl,
1621
1628
int: shift_left_logical,
1622
1629
fold_const {
1623
- int( a, b) => a. wrapping_shl ( b as u32 ) ;
1630
+ int( a, b) => a. checked_shl ( b as u32 ) ? ;
1624
1631
}
1625
1632
}
1626
1633
simple_shift_op ! {
1627
1634
lshr,
1628
1635
uint: shift_right_logical,
1629
1636
fold_const {
1630
- uint( a, b) => a. wrapping_shr ( b as u32 ) ;
1637
+ uint( a, b) => a. checked_shr ( b as u32 ) ? ;
1631
1638
}
1632
1639
}
1633
1640
simple_shift_op ! {
1634
1641
ashr,
1635
1642
sint: shift_right_arithmetic,
1636
1643
fold_const {
1637
- sint( a, b) => a. wrapping_shr ( b as u32 ) ;
1644
+ sint( a, b) => a. checked_shr ( b as u32 ) ? ;
1638
1645
}
1639
1646
}
1640
1647
simple_uni_op ! {
1641
1648
neg,
1642
1649
sint: s_negate,
1643
1650
fold_const {
1644
- sint( a) => a. wrapping_neg ( ) ;
1651
+ sint( a) => a. checked_neg ( ) ? ;
1645
1652
}
1646
1653
}
1647
1654
simple_uni_op ! { fneg, float: f_negate}
0 commit comments