@@ -27,6 +27,7 @@ use rustc_middle::ty::{
27
27
self , AdtDef , CanonicalUserTypeAnnotation , GenericArg , GenericArgsRef , Region , Ty , TyCtxt ,
28
28
TypeVisitableExt , UserType ,
29
29
} ;
30
+ use rustc_span:: def_id:: LocalDefId ;
30
31
use rustc_span:: { ErrorGuaranteed , Span , Symbol } ;
31
32
use rustc_target:: abi:: { FieldIdx , Integer } ;
32
33
@@ -88,15 +89,21 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
88
89
fn lower_pattern_range_endpoint (
89
90
& mut self ,
90
91
expr : Option < & ' tcx hir:: Expr < ' tcx > > ,
91
- ) -> Result < ( Option < mir:: Const < ' tcx > > , Option < Ascription < ' tcx > > ) , ErrorGuaranteed > {
92
+ ) -> Result <
93
+ ( Option < mir:: Const < ' tcx > > , Option < Ascription < ' tcx > > , Option < LocalDefId > ) ,
94
+ ErrorGuaranteed ,
95
+ > {
92
96
match expr {
93
- None => Ok ( ( None , None ) ) ,
97
+ None => Ok ( ( None , None , None ) ) ,
94
98
Some ( expr) => {
95
- let ( kind, ascr) = match self . lower_lit ( expr) {
99
+ let ( kind, ascr, inline_const) = match self . lower_lit ( expr) {
100
+ PatKind :: InlineConstant { subpattern, def } => {
101
+ ( subpattern. kind , None , Some ( def) )
102
+ }
96
103
PatKind :: AscribeUserType { ascription, subpattern : box Pat { kind, .. } } => {
97
- ( kind, Some ( ascription) )
104
+ ( kind, Some ( ascription) , None )
98
105
}
99
- kind => ( kind, None ) ,
106
+ kind => ( kind, None , None ) ,
100
107
} ;
101
108
let value = if let PatKind :: Constant { value } = kind {
102
109
value
@@ -106,7 +113,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
106
113
) ;
107
114
return Err ( self . tcx . sess . delay_span_bug ( expr. span , msg) ) ;
108
115
} ;
109
- Ok ( ( Some ( value) , ascr) )
116
+ Ok ( ( Some ( value) , ascr, inline_const ) )
110
117
}
111
118
}
112
119
}
@@ -177,8 +184,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
177
184
return Err ( self . tcx . sess . delay_span_bug ( span, msg) ) ;
178
185
}
179
186
180
- let ( lo, lo_ascr) = self . lower_pattern_range_endpoint ( lo_expr) ?;
181
- let ( hi, hi_ascr) = self . lower_pattern_range_endpoint ( hi_expr) ?;
187
+ let ( lo, lo_ascr, lo_inline ) = self . lower_pattern_range_endpoint ( lo_expr) ?;
188
+ let ( hi, hi_ascr, hi_inline ) = self . lower_pattern_range_endpoint ( hi_expr) ?;
182
189
183
190
let lo = lo. unwrap_or_else ( || {
184
191
// Unwrap is ok because the type is known to be numeric.
@@ -237,6 +244,12 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
237
244
} ;
238
245
}
239
246
}
247
+ for inline_const in [ lo_inline, hi_inline] {
248
+ if let Some ( def) = inline_const {
249
+ kind =
250
+ PatKind :: InlineConstant { def, subpattern : Box :: new ( Pat { span, ty, kind } ) } ;
251
+ }
252
+ }
240
253
Ok ( kind)
241
254
}
242
255
@@ -599,11 +612,9 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
599
612
// const eval path below.
600
613
// FIXME: investigate the performance impact of removing this.
601
614
let lit_input = match expr. kind {
602
- hir:: ExprKind :: Lit ( ref lit) => Some ( LitToConstInput { lit : & lit. node , ty, neg : false } ) ,
603
- hir:: ExprKind :: Unary ( hir:: UnOp :: Neg , ref expr) => match expr. kind {
604
- hir:: ExprKind :: Lit ( ref lit) => {
605
- Some ( LitToConstInput { lit : & lit. node , ty, neg : true } )
606
- }
615
+ hir:: ExprKind :: Lit ( lit) => Some ( LitToConstInput { lit : & lit. node , ty, neg : false } ) ,
616
+ hir:: ExprKind :: Unary ( hir:: UnOp :: Neg , expr) => match expr. kind {
617
+ hir:: ExprKind :: Lit ( lit) => Some ( LitToConstInput { lit : & lit. node , ty, neg : true } ) ,
607
618
_ => None ,
608
619
} ,
609
620
_ => None ,
@@ -633,13 +644,13 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
633
644
if let Ok ( Some ( valtree) ) =
634
645
self . tcx . const_eval_resolve_for_typeck ( self . param_env , ct, Some ( span) )
635
646
{
636
- self . const_to_pat (
647
+ let subpattern = self . const_to_pat (
637
648
Const :: Ty ( ty:: Const :: new_value ( self . tcx , valtree, ty) ) ,
638
649
id,
639
650
span,
640
651
None ,
641
- )
642
- . kind
652
+ ) ;
653
+ PatKind :: InlineConstant { subpattern , def : def_id }
643
654
} else {
644
655
// If that fails, convert it to an opaque constant pattern.
645
656
match tcx. const_eval_resolve ( self . param_env , uneval, Some ( span) ) {
@@ -822,6 +833,9 @@ impl<'tcx> PatternFoldable<'tcx> for PatKind<'tcx> {
822
833
PatKind :: Deref { subpattern : subpattern. fold_with ( folder) }
823
834
}
824
835
PatKind :: Constant { value } => PatKind :: Constant { value } ,
836
+ PatKind :: InlineConstant { def, subpattern : ref pattern } => {
837
+ PatKind :: InlineConstant { def, subpattern : pattern. fold_with ( folder) }
838
+ }
825
839
PatKind :: Range ( ref range) => PatKind :: Range ( range. clone ( ) ) ,
826
840
PatKind :: Slice { ref prefix, ref slice, ref suffix } => PatKind :: Slice {
827
841
prefix : prefix. fold_with ( folder) ,
0 commit comments