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