@@ -11,15 +11,14 @@ use std::mem;
11
11
use std:: sync:: Arc ;
12
12
13
13
use rustc_abi:: VariantIdx ;
14
- use rustc_ast:: LitKind ;
15
14
use rustc_data_structures:: fx:: FxIndexMap ;
16
15
use rustc_data_structures:: stack:: ensure_sufficient_stack;
17
16
use rustc_hir:: { BindingMode , ByRef } ;
18
- use rustc_middle:: bug;
19
17
use rustc_middle:: middle:: region;
20
18
use rustc_middle:: mir:: { self , * } ;
21
19
use rustc_middle:: thir:: { self , * } ;
22
- use rustc_middle:: ty:: { self , CanonicalUserTypeAnnotation , Ty } ;
20
+ use rustc_middle:: ty:: { self , CanonicalUserTypeAnnotation , Ty , TypeVisitableExt , ValTreeKind } ;
21
+ use rustc_middle:: { bug, span_bug} ;
23
22
use rustc_pattern_analysis:: rustc:: { DeconstructedPat , RustcPatCtxt } ;
24
23
use rustc_span:: { BytePos , Pos , Span , Symbol } ;
25
24
use tracing:: { debug, instrument} ;
@@ -2836,7 +2835,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2836
2835
pub ( crate ) fn static_pattern_match (
2837
2836
& self ,
2838
2837
cx : & RustcPatCtxt < ' _ , ' tcx > ,
2839
- value : ExprId ,
2838
+ constant : ConstOperand < ' tcx > ,
2840
2839
arms : & [ ArmId ] ,
2841
2840
built_match_tree : & BuiltMatchTree < ' tcx > ,
2842
2841
) -> Option < BasicBlock > {
@@ -2854,57 +2853,78 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2854
2853
. or_else ( || branch. sub_branches . last ( ) )
2855
2854
. unwrap ( ) ;
2856
2855
2857
- match self . static_pattern_match_help ( value , & pat. pat ) {
2856
+ match self . static_pattern_match_help ( constant , & pat. pat ) {
2858
2857
true => return Some ( sub_branch. success_block ) ,
2859
2858
false => continue ,
2860
2859
}
2861
2860
}
2862
- } else if self . static_pattern_match_help ( value , & pat) {
2861
+ } else if self . static_pattern_match_help ( constant , & pat) {
2863
2862
return Some ( branch. sub_branches [ 0 ] . success_block ) ;
2864
2863
}
2865
2864
}
2866
2865
2867
2866
None
2868
2867
}
2869
2868
2870
- fn static_pattern_match_help ( & self , value : ExprId , pat : & DeconstructedPat < ' _ , ' tcx > ) -> bool {
2871
- use rustc_middle:: thir:: ExprKind ;
2869
+ fn static_pattern_match_help (
2870
+ & self ,
2871
+ constant : ConstOperand < ' tcx > ,
2872
+ pat : & DeconstructedPat < ' _ , ' tcx > ,
2873
+ ) -> bool {
2872
2874
use rustc_pattern_analysis:: constructor:: { IntRange , MaybeInfiniteInt } ;
2873
2875
use rustc_pattern_analysis:: rustc:: Constructor ;
2874
2876
2877
+ // Based on eval_unevaluated_mir_constant_to_valtree
2878
+ let ( valtree, ty) = ' a: {
2879
+ assert ! ( !constant. const_. ty( ) . has_param( ) ) ;
2880
+ let ( uv, ty) = match constant. const_ {
2881
+ mir:: Const :: Unevaluated ( uv, ty) => ( uv. shrink ( ) , ty) ,
2882
+ mir:: Const :: Ty ( _, c) => match c. kind ( ) {
2883
+ // A constant that came from a const generic but was then used as an argument to
2884
+ // old-style simd_shuffle (passing as argument instead of as a generic param).
2885
+ ty:: ConstKind :: Value ( cv) => break ' a ( cv. valtree , cv. ty ) ,
2886
+ other => span_bug ! ( constant. span, "{other:#?}" ) ,
2887
+ } ,
2888
+ // We should never encounter `Const::Val` unless MIR opts (like const prop) evaluate
2889
+ // a constant and write that value back into `Operand`s. This could happen, but is
2890
+ // unlikely. Also: all users of `simd_shuffle` are on unstable and already need to take
2891
+ // a lot of care around intrinsics. For an issue to happen here, it would require a
2892
+ // macro expanding to a `simd_shuffle` call without wrapping the constant argument in a
2893
+ // `const {}` block, but the user pass through arbitrary expressions.
2894
+ // FIXME(oli-obk): replace the magic const generic argument of `simd_shuffle` with a
2895
+ // real const generic, and get rid of this entire function.
2896
+ other => span_bug ! ( constant. span, "{other:#?}" ) ,
2897
+ } ;
2898
+ (
2899
+ self . tcx
2900
+ . const_eval_resolve_for_typeck ( self . typing_env ( ) , uv, constant. span )
2901
+ . unwrap ( )
2902
+ . unwrap ( ) ,
2903
+ ty,
2904
+ )
2905
+ } ;
2906
+ assert ! ( !ty. has_param( ) ) ;
2907
+
2875
2908
match pat. ctor ( ) {
2876
- Constructor :: Variant ( variant_index) => match & self . thir [ value] . kind {
2877
- ExprKind :: Adt ( value_adt) => {
2878
- return * variant_index == value_adt. variant_index ;
2909
+ Constructor :: Variant ( variant_index) => match * valtree {
2910
+ ValTreeKind :: Branch ( box [ actual_variant_idx] ) => {
2911
+ * variant_index
2912
+ == VariantIdx :: from_u32 ( actual_variant_idx. unwrap_leaf ( ) . to_u32 ( ) )
2879
2913
}
2880
2914
other => todo ! ( "{other:?}" ) ,
2881
2915
} ,
2882
- Constructor :: IntRange ( int_range) => match & self . thir [ value] . kind {
2883
- ExprKind :: Literal { lit, neg } => match & lit. node {
2884
- LitKind :: Int ( n, _) => {
2885
- let n = if pat. ty ( ) . is_signed ( ) {
2886
- let bits = pat. ty ( ) . primitive_size ( self . tcx ) . bits ( ) ;
2887
- MaybeInfiniteInt :: new_finite_int (
2888
- if * neg {
2889
- ( n. get ( ) as i128 ) . overflowing_neg ( ) . 0 as u128
2890
- & ( ( 1u128 << bits) - 1 )
2891
- } else {
2892
- n. get ( )
2893
- } ,
2894
- bits,
2895
- )
2896
- } else {
2897
- MaybeInfiniteInt :: new_finite_uint ( n. get ( ) )
2898
- } ;
2899
-
2900
- return IntRange :: from_singleton ( n) . is_subrange ( int_range) ;
2901
- }
2902
-
2903
- other => todo ! ( "{other:?}" ) ,
2904
- } ,
2905
- other => todo ! ( "{other:?}" ) ,
2906
- } ,
2907
- Constructor :: Wildcard => return true ,
2916
+ Constructor :: IntRange ( int_range) => {
2917
+ let size = pat. ty ( ) . primitive_size ( self . tcx ) ;
2918
+ let actual_int = valtree. unwrap_leaf ( ) . to_bits ( size) ;
2919
+ let actual_int = if pat. ty ( ) . is_signed ( ) {
2920
+ MaybeInfiniteInt :: new_finite_int ( actual_int, size. bits ( ) )
2921
+ } else {
2922
+ MaybeInfiniteInt :: new_finite_uint ( actual_int)
2923
+ } ;
2924
+ IntRange :: from_singleton ( actual_int) . is_subrange ( int_range)
2925
+ }
2926
+ Constructor :: Wildcard => true ,
2927
+ // FIXME error out before static_pattern_match gets run and replace this with bug!()
2908
2928
_ => false ,
2909
2929
}
2910
2930
}
0 commit comments