@@ -4,10 +4,10 @@ use rustc_ast::ptr::P;
4
4
use rustc_ast:: * ;
5
5
use rustc_data_structures:: stack:: ensure_sufficient_stack;
6
6
use rustc_hir as hir;
7
- use rustc_hir:: def:: Res ;
7
+ use rustc_hir:: def:: { DefKind , Res } ;
8
8
use rustc_middle:: span_bug;
9
9
use rustc_span:: source_map:: { Spanned , respan} ;
10
- use rustc_span:: { Ident , Span } ;
10
+ use rustc_span:: { Ident , Span , kw } ;
11
11
12
12
use super :: errors:: {
13
13
ArbitraryExpressionInPattern , ExtraDoubleDot , MisplacedDoubleDot , SubTupleBinding ,
@@ -429,4 +429,80 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
429
429
} ;
430
430
self . arena . alloc ( hir:: PatExpr { hir_id : self . lower_node_id ( expr. id ) , span, kind } )
431
431
}
432
+
433
+ pub ( crate ) fn lower_ty_pat ( & mut self , pattern : & Pat ) -> & ' hir hir:: TyPat < ' hir > {
434
+ self . arena . alloc ( self . lower_ty_pat_mut ( pattern) )
435
+ }
436
+
437
+ fn lower_ty_pat_mut ( & mut self , mut pattern : & Pat ) -> hir:: TyPat < ' hir > {
438
+ // loop here to avoid recursion
439
+ let pat_hir_id = self . lower_node_id ( pattern. id ) ;
440
+ let node = loop {
441
+ match & pattern. kind {
442
+ PatKind :: Range ( e1, e2, Spanned { node : end, .. } ) => {
443
+ let mut lower_expr = |e : & Expr | -> & _ {
444
+ let kind = if let ExprKind :: Path ( qself, path) = & e. kind {
445
+ hir:: ConstArgKind :: Path ( self . lower_qpath (
446
+ e. id ,
447
+ qself,
448
+ path,
449
+ ParamMode :: Optional ,
450
+ AllowReturnTypeNotation :: No ,
451
+ ImplTraitContext :: Disallowed ( ImplTraitPosition :: Path ) ,
452
+ None ,
453
+ ) )
454
+ } else {
455
+ let node_id = self . next_node_id ( ) ;
456
+ let def_id = self . create_def (
457
+ self . current_hir_id_owner . def_id ,
458
+ node_id,
459
+ kw:: Empty ,
460
+ DefKind :: AnonConst ,
461
+ e. span ,
462
+ ) ;
463
+ let hir_id = self . lower_node_id ( node_id) ;
464
+ let ac = self . arena . alloc ( hir:: AnonConst {
465
+ def_id,
466
+ hir_id,
467
+ body : self . lower_const_body ( pattern. span , Some ( e) ) ,
468
+ span : self . lower_span ( pattern. span ) ,
469
+ } ) ;
470
+ hir:: ConstArgKind :: Anon ( ac)
471
+ } ;
472
+ self . arena . alloc ( hir:: ConstArg { hir_id : self . next_id ( ) , kind } )
473
+ } ;
474
+ break hir:: TyPatKind :: Range (
475
+ e1. as_deref ( ) . map ( |e| lower_expr ( e) ) ,
476
+ e2. as_deref ( ) . map ( |e| lower_expr ( e) ) ,
477
+ self . lower_range_end ( end, e2. is_some ( ) ) ,
478
+ ) ;
479
+ }
480
+ // return inner to be processed in next loop
481
+ PatKind :: Paren ( inner) => pattern = inner,
482
+ PatKind :: MacCall ( _) => panic ! ( "{:?} shouldn't exist here" , pattern. span) ,
483
+ PatKind :: Err ( guar) => break hir:: TyPatKind :: Err ( * guar) ,
484
+ PatKind :: Deref ( ..)
485
+ | PatKind :: Box ( ..)
486
+ | PatKind :: Or ( ..)
487
+ | PatKind :: Struct ( ..)
488
+ | PatKind :: TupleStruct ( ..)
489
+ | PatKind :: Tuple ( ..)
490
+ | PatKind :: Ref ( ..)
491
+ | PatKind :: Expr ( ..)
492
+ | PatKind :: Guard ( ..)
493
+ | PatKind :: Slice ( _)
494
+ | PatKind :: Ident ( ..)
495
+ | PatKind :: Path ( ..)
496
+ | PatKind :: Wild
497
+ | PatKind :: Never
498
+ | PatKind :: Rest => {
499
+ break hir:: TyPatKind :: Err (
500
+ self . dcx ( ) . span_err ( pattern. span , "pattern not supported in pattern types" ) ,
501
+ ) ;
502
+ }
503
+ }
504
+ } ;
505
+
506
+ hir:: TyPat { hir_id : pat_hir_id, kind : node, span : self . lower_span ( pattern. span ) }
507
+ }
432
508
}
0 commit comments