Skip to content

Commit 2afa993

Browse files
Use bespoke macro instead of ? inside const fns
1 parent c981d67 commit 2afa993

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/libcore/num/mod.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ use crate::intrinsics;
1010
use crate::mem;
1111
use crate::str::FromStr;
1212

13+
// Used because the `?` operator is not allowed in a const context.
14+
macro_rules! try_opt {
15+
($e:expr) => {
16+
match $e {
17+
Some(x) => x,
18+
None => return None,
19+
}
20+
};
21+
}
22+
1323
macro_rules! impl_nonzero_fmt {
1424
( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
1525
$(
@@ -1000,17 +1010,17 @@ $EndFeature, "
10001010

10011011
while exp > 1 {
10021012
if (exp & 1) == 1 {
1003-
acc = acc.checked_mul(base)?;
1013+
acc = try_opt!(acc.checked_mul(base));
10041014
}
10051015
exp /= 2;
1006-
base = base.checked_mul(base)?;
1016+
base = try_opt!(base.checked_mul(base));
10071017
}
10081018

10091019
// Deal with the final bit of the exponent separately, since
10101020
// squaring the base afterwards is not necessary and may cause a
10111021
// needless overflow.
10121022
if exp == 1 {
1013-
acc = acc.checked_mul(base)?;
1023+
acc = try_opt!(acc.checked_mul(base));
10141024
}
10151025

10161026
Some(acc)
@@ -3126,17 +3136,17 @@ assert_eq!(", stringify!($SelfT), "::max_value().checked_pow(2), None);", $EndFe
31263136

31273137
while exp > 1 {
31283138
if (exp & 1) == 1 {
3129-
acc = acc.checked_mul(base)?;
3139+
acc = try_opt!(acc.checked_mul(base));
31303140
}
31313141
exp /= 2;
3132-
base = base.checked_mul(base)?;
3142+
base = try_opt!(base.checked_mul(base));
31333143
}
31343144

31353145
// Deal with the final bit of the exponent separately, since
31363146
// squaring the base afterwards is not necessary and may cause a
31373147
// needless overflow.
31383148
if exp == 1 {
3139-
acc = acc.checked_mul(base)?;
3149+
acc = try_opt!(acc.checked_mul(base));
31403150
}
31413151

31423152
Some(acc)

0 commit comments

Comments
 (0)