@@ -11,15 +11,16 @@ 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 , LetStmt , LocalSource , Node } ;
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:: {
21
+ self , CanonicalUserTypeAnnotation , Ty , TypeVisitableExt , ValTree , ValTreeKind ,
22
+ } ;
23
+ use rustc_middle:: { bug, span_bug} ;
23
24
use rustc_pattern_analysis:: rustc:: { DeconstructedPat , RustcPatCtxt } ;
24
25
use rustc_span:: { BytePos , Pos , Span , Symbol , sym} ;
25
26
use tracing:: { debug, instrument} ;
@@ -2870,7 +2871,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2870
2871
pub ( crate ) fn static_pattern_match (
2871
2872
& self ,
2872
2873
cx : & RustcPatCtxt < ' _ , ' tcx > ,
2873
- value : ExprId ,
2874
+ constant : ConstOperand < ' tcx > ,
2874
2875
arms : & [ ArmId ] ,
2875
2876
built_match_tree : & BuiltMatchTree < ' tcx > ,
2876
2877
) -> Option < BasicBlock > {
@@ -2888,56 +2889,81 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2888
2889
. or_else ( || branch. sub_branches . last ( ) )
2889
2890
. unwrap ( ) ;
2890
2891
2891
- match self . static_pattern_match_help ( value , & pat. pat ) {
2892
+ match self . static_pattern_match_help ( constant , & pat. pat ) {
2892
2893
true => return Some ( sub_branch. success_block ) ,
2893
2894
false => continue ,
2894
2895
}
2895
2896
}
2896
- } else if self . static_pattern_match_help ( value , & pat) {
2897
+ } else if self . static_pattern_match_help ( constant , & pat) {
2897
2898
return Some ( branch. sub_branches [ 0 ] . success_block ) ;
2898
2899
}
2899
2900
}
2900
2901
2901
2902
None
2902
2903
}
2903
2904
2904
- fn static_pattern_match_help ( & self , value : ExprId , pat : & DeconstructedPat < ' _ , ' tcx > ) -> bool {
2905
- use rustc_middle:: thir:: ExprKind ;
2905
+ fn static_pattern_match_help (
2906
+ & self ,
2907
+ constant : ConstOperand < ' tcx > ,
2908
+ pat : & DeconstructedPat < ' _ , ' tcx > ,
2909
+ ) -> bool {
2906
2910
use rustc_pattern_analysis:: constructor:: { IntRange , MaybeInfiniteInt } ;
2907
2911
use rustc_pattern_analysis:: rustc:: Constructor ;
2908
2912
2909
- match pat. ctor ( ) {
2910
- Constructor :: Variant ( variant_index) => match & self . thir [ value] . kind {
2911
- ExprKind :: Adt ( value_adt) => {
2912
- return * variant_index == value_adt. variant_index ;
2913
+ // Based on eval_unevaluated_mir_constant_to_valtree
2914
+ let ( valtree, ty) = ' a: {
2915
+ assert ! ( !constant. const_. ty( ) . has_param( ) ) ;
2916
+ let ( uv, ty) = match constant. const_ {
2917
+ mir:: Const :: Unevaluated ( uv, ty) => ( uv. shrink ( ) , ty) ,
2918
+ mir:: Const :: Ty ( _, c) => match c. kind ( ) {
2919
+ // A constant that came from a const generic but was then used as an argument to
2920
+ // old-style simd_shuffle (passing as argument instead of as a generic param).
2921
+ ty:: ConstKind :: Value ( cv) => break ' a ( cv. valtree , cv. ty ) ,
2922
+ other => span_bug ! ( constant. span, "{other:#?}" ) ,
2923
+ } ,
2924
+ mir:: Const :: Val ( mir:: ConstValue :: Scalar ( mir:: interpret:: Scalar :: Int ( val) ) , ty) => {
2925
+ break ' a ( ValTree :: from_scalar_int ( self . tcx , val) , ty) ;
2913
2926
}
2914
- other => todo ! ( "{other:?}" ) ,
2915
- } ,
2916
- Constructor :: IntRange ( int_range) => match & self . thir [ value] . kind {
2917
- ExprKind :: Literal { lit, neg } => match & lit. node {
2918
- LitKind :: Int ( n, _) => {
2919
- let n = if pat. ty ( ) . is_signed ( ) {
2920
- let size = pat. ty ( ) . primitive_size ( self . tcx ) ;
2921
- MaybeInfiniteInt :: new_finite_int (
2922
- if * neg {
2923
- size. truncate ( ( n. get ( ) as i128 ) . overflowing_neg ( ) . 0 as u128 )
2924
- } else {
2925
- n. get ( )
2926
- } ,
2927
- size. bits ( ) ,
2928
- )
2929
- } else {
2930
- MaybeInfiniteInt :: new_finite_uint ( n. get ( ) )
2931
- } ;
2932
-
2933
- return IntRange :: from_singleton ( n) . is_subrange ( int_range) ;
2934
- }
2927
+ // We should never encounter `Const::Val` unless MIR opts (like const prop) evaluate
2928
+ // a constant and write that value back into `Operand`s. This could happen, but is
2929
+ // unlikely. Also: all users of `simd_shuffle` are on unstable and already need to take
2930
+ // a lot of care around intrinsics. For an issue to happen here, it would require a
2931
+ // macro expanding to a `simd_shuffle` call without wrapping the constant argument in a
2932
+ // `const {}` block, but the user pass through arbitrary expressions.
2933
+ // FIXME(oli-obk): replace the magic const generic argument of `simd_shuffle` with a
2934
+ // real const generic, and get rid of this entire function.
2935
+ other => span_bug ! ( constant. span, "{other:#?}" ) ,
2936
+ } ;
2937
+ (
2938
+ self . tcx
2939
+ . const_eval_resolve_for_typeck ( self . typing_env ( ) , uv, constant. span )
2940
+ . unwrap ( )
2941
+ . unwrap ( ) ,
2942
+ ty,
2943
+ )
2944
+ } ;
2945
+ assert ! ( !ty. has_param( ) ) ;
2935
2946
2936
- other => todo ! ( "{other:?}" ) ,
2937
- } ,
2947
+ match pat. ctor ( ) {
2948
+ Constructor :: Variant ( variant_index) => match * valtree {
2949
+ ValTreeKind :: Branch ( box [ actual_variant_idx] ) => {
2950
+ * variant_index
2951
+ == VariantIdx :: from_u32 ( actual_variant_idx. unwrap_leaf ( ) . to_u32 ( ) )
2952
+ }
2938
2953
other => todo ! ( "{other:?}" ) ,
2939
2954
} ,
2940
- Constructor :: Wildcard => return true ,
2955
+ Constructor :: IntRange ( int_range) => {
2956
+ let size = pat. ty ( ) . primitive_size ( self . tcx ) ;
2957
+ let actual_int = valtree. unwrap_leaf ( ) . to_bits ( size) ;
2958
+ let actual_int = if pat. ty ( ) . is_signed ( ) {
2959
+ MaybeInfiniteInt :: new_finite_int ( actual_int, size. bits ( ) )
2960
+ } else {
2961
+ MaybeInfiniteInt :: new_finite_uint ( actual_int)
2962
+ } ;
2963
+ IntRange :: from_singleton ( actual_int) . is_subrange ( int_range)
2964
+ }
2965
+ Constructor :: Wildcard => true ,
2966
+ // FIXME error out before static_pattern_match gets run and replace this with bug!()
2941
2967
_ => false ,
2942
2968
}
2943
2969
}
0 commit comments