@@ -29,6 +29,7 @@ use rustc_hir::{GenericArg, GenericArgs, OpaqueTyOrigin};
2929use rustc_infer:: infer:: { InferCtxt , TyCtxtInferExt } ;
3030use rustc_infer:: traits:: ObligationCause ;
3131use rustc_middle:: middle:: stability:: AllowUnstable ;
32+ use rustc_middle:: mir:: interpret:: { LitToConstError , LitToConstInput } ;
3233use rustc_middle:: ty:: {
3334 self , Const , GenericArgKind , GenericArgsRef , GenericParamDefKind , IsSuggestable , ParamEnv , Ty ,
3435 TyCtxt , TypeVisitableExt ,
@@ -2559,25 +2560,76 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
25592560 // handled specially and will not descend into this routine.
25602561 self . ty_infer ( None , ast_ty. span )
25612562 }
2562- hir:: TyKind :: Pat ( _ty, pat) => match pat. kind {
2563- hir:: PatKind :: Wild => {
2564- let err = tcx. dcx ( ) . emit_err ( WildPatTy { span : pat. span } ) ;
2565- Ty :: new_error ( tcx, err)
2566- }
2567- hir:: PatKind :: Binding ( _, _, _, _) => todo ! ( ) ,
2568- hir:: PatKind :: Struct ( _, _, _) => todo ! ( ) ,
2569- hir:: PatKind :: TupleStruct ( _, _, _) => todo ! ( ) ,
2570- hir:: PatKind :: Or ( _) => todo ! ( ) ,
2571- hir:: PatKind :: Path ( _) => todo ! ( ) ,
2572- hir:: PatKind :: Tuple ( _, _) => todo ! ( ) ,
2573- hir:: PatKind :: Box ( _) => todo ! ( ) ,
2574- hir:: PatKind :: Ref ( _, _) => todo ! ( ) ,
2575- hir:: PatKind :: Lit ( _) => todo ! ( ) ,
2576- hir:: PatKind :: Range ( _, _, _) => Ty :: new_misc_error ( tcx) ,
2577- hir:: PatKind :: Slice ( _, _, _) => todo ! ( ) ,
2578- hir:: PatKind :: Never => todo ! ( ) ,
2579- hir:: PatKind :: Err ( e) => Ty :: new_error ( tcx, e) ,
2580- } ,
2563+ hir:: TyKind :: Pat ( ty, pat) => {
2564+ let ty = self . ast_ty_to_ty ( ty) ;
2565+ let pat_ty = match pat. kind {
2566+ hir:: PatKind :: Wild => {
2567+ let err = tcx. dcx ( ) . emit_err ( WildPatTy { span : pat. span } ) ;
2568+ Ty :: new_error ( tcx, err)
2569+ }
2570+ hir:: PatKind :: Binding ( _, _, _, _) => todo ! ( ) ,
2571+ hir:: PatKind :: Struct ( _, _, _) => todo ! ( ) ,
2572+ hir:: PatKind :: TupleStruct ( _, _, _) => todo ! ( ) ,
2573+ hir:: PatKind :: Or ( _) => todo ! ( ) ,
2574+ hir:: PatKind :: Path ( _) => todo ! ( ) ,
2575+ hir:: PatKind :: Tuple ( _, _) => todo ! ( ) ,
2576+ hir:: PatKind :: Box ( _) => todo ! ( ) ,
2577+ hir:: PatKind :: Ref ( _, _) => todo ! ( ) ,
2578+ hir:: PatKind :: Lit ( _) => todo ! ( ) ,
2579+ hir:: PatKind :: Range ( start, end, include_end) => {
2580+ let expr_to_const = |expr : & ' tcx hir:: Expr < ' tcx > , neg| -> ty:: Const < ' tcx > {
2581+ match & expr. kind {
2582+ hir:: ExprKind :: Lit ( lit) => {
2583+ let lit_input = LitToConstInput { lit : & lit. node , ty, neg } ;
2584+ match tcx. lit_to_const ( lit_input) {
2585+ Ok ( c) => c,
2586+ Err ( LitToConstError :: Reported ( err) ) => {
2587+ ty:: Const :: new_error ( tcx, err, ty)
2588+ }
2589+ Err ( LitToConstError :: TypeError ) => todo ! ( ) ,
2590+ }
2591+ }
2592+ _ => {
2593+ let err = tcx
2594+ . dcx ( )
2595+ . emit_err ( crate :: errors:: NonConstRange { span : expr. span } ) ;
2596+ ty:: Const :: new_error ( tcx, err, ty)
2597+ }
2598+ }
2599+ } ;
2600+ let expr_to_const = |expr, neg| {
2601+ let c = expr_to_const ( expr, neg) ;
2602+ self . record_ty ( expr. hir_id , c. ty ( ) , expr. span ) ;
2603+ c
2604+ } ;
2605+ let expr_to_const = |expr : & ' tcx hir:: Expr < ' tcx > | match & expr. kind {
2606+ hir:: ExprKind :: Unary ( hir:: UnOp :: Neg , expr) => expr_to_const ( expr, true ) ,
2607+ _ => expr_to_const ( expr, false ) ,
2608+ } ;
2609+ let expr_to_const = |expr| {
2610+ let c = expr_to_const ( expr) ;
2611+ self . record_ty ( expr. hir_id , c. ty ( ) , expr. span ) ;
2612+ c
2613+ } ;
2614+
2615+ let start = start. map ( expr_to_const) ;
2616+ let end = end. map ( expr_to_const) ;
2617+
2618+ let include_end = match include_end {
2619+ hir:: RangeEnd :: Included => true ,
2620+ hir:: RangeEnd :: Excluded => false ,
2621+ } ;
2622+
2623+ let pat = tcx. mk_pat ( ty:: PatternKind :: Range { start, end, include_end } ) ;
2624+ Ty :: new_pat ( tcx, ty, pat)
2625+ }
2626+ hir:: PatKind :: Slice ( _, _, _) => todo ! ( ) ,
2627+ hir:: PatKind :: Never => todo ! ( ) ,
2628+ hir:: PatKind :: Err ( e) => Ty :: new_error ( tcx, e) ,
2629+ } ;
2630+ self . record_ty ( pat. hir_id , ty, pat. span ) ;
2631+ pat_ty
2632+ }
25812633 hir:: TyKind :: Err ( guar) => Ty :: new_error ( tcx, * guar) ,
25822634 } ;
25832635
0 commit comments