File tree 2 files changed +16
-4
lines changed
compiler/rustc_middle/src
2 files changed +16
-4
lines changed Original file line number Diff line number Diff line change @@ -244,6 +244,8 @@ impl<'tcx> Const<'tcx> {
244
244
Const :: Ty ( c) => match c. kind ( ) {
245
245
ty:: ConstKind :: Value ( valtree) if c. ty ( ) . is_primitive ( ) => {
246
246
// A valtree of a type where leaves directly represent the scalar const value.
247
+ // Just checking whether it is a leaf is insufficient as e.g. references are leafs
248
+ // but the leaf value is the value they point to, not the reference itself!
247
249
Some ( valtree. unwrap_leaf ( ) . into ( ) )
248
250
}
249
251
_ => None ,
@@ -255,7 +257,17 @@ impl<'tcx> Const<'tcx> {
255
257
256
258
#[ inline]
257
259
pub fn try_to_scalar_int ( self ) -> Option < ScalarInt > {
258
- self . try_to_scalar ( ) ?. try_to_int ( ) . ok ( )
260
+ // This is equivalent to `self.try_to_scalar()?.try_to_int().ok()`, but measurably faster.
261
+ match self {
262
+ Const :: Val ( ConstValue :: Scalar ( Scalar :: Int ( x) ) , _) => Some ( x) ,
263
+ Const :: Ty ( c) => match c. kind ( ) {
264
+ ty:: ConstKind :: Value ( valtree) if c. ty ( ) . is_primitive ( ) => {
265
+ Some ( valtree. unwrap_leaf ( ) )
266
+ }
267
+ _ => None ,
268
+ } ,
269
+ _ => None ,
270
+ }
259
271
}
260
272
261
273
#[ inline]
Original file line number Diff line number Diff line change @@ -1013,12 +1013,12 @@ impl<'tcx> PatRangeBoundary<'tcx> {
1013
1013
// many ranges such as '\u{037A}'..='\u{037F}', and chars can be compared
1014
1014
// in this way.
1015
1015
( Finite ( a) , Finite ( b) ) if matches ! ( ty. kind( ) , ty:: Uint ( _) | ty:: Char ) => {
1016
- let to_scalar_int = |x| match x {
1016
+ /* let to_scalar_int = |x| match x {
1017
1017
mir::Const::Val(mir::ConstValue::Scalar(Scalar::Int(x)), _) => Some(x),
1018
1018
mir::Const::Ty(x) => Some(x.to_valtree().unwrap_leaf()),
1019
1019
_ => None,
1020
- } ;
1021
- if let ( Some ( a) , Some ( b) ) = ( to_scalar_int ( a ) , to_scalar_int ( b ) ) {
1020
+ };*/
1021
+ if let ( Some ( a) , Some ( b) ) = ( a . try_to_scalar_int ( ) , b . try_to_scalar_int ( ) ) {
1022
1022
let sz = ty. primitive_size ( tcx) ;
1023
1023
let a = a. assert_uint ( sz) ;
1024
1024
let b = b. assert_uint ( sz) ;
You can’t perform that action at this time.
0 commit comments