@@ -27,6 +27,7 @@ use rustc_middle::ty::{
2727 self , AdtDef , CanonicalUserTypeAnnotation , GenericArg , GenericArgsRef , Region , Ty , TyCtxt ,
2828 TypeVisitableExt , UserType ,
2929} ;
30+ use rustc_span:: def_id:: LocalDefId ;
3031use rustc_span:: { ErrorGuaranteed , Span , Symbol } ;
3132use rustc_target:: abi:: { FieldIdx , Integer } ;
3233
@@ -88,15 +89,21 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
8889 fn lower_pattern_range_endpoint (
8990 & mut self ,
9091 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+ > {
9296 match expr {
93- None => Ok ( ( None , None ) ) ,
97+ None => Ok ( ( None , None , None ) ) ,
9498 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+ }
96103 PatKind :: AscribeUserType { ascription, subpattern : box Pat { kind, .. } } => {
97- ( kind, Some ( ascription) )
104+ ( kind, Some ( ascription) , None )
98105 }
99- kind => ( kind, None ) ,
106+ kind => ( kind, None , None ) ,
100107 } ;
101108 let value = if let PatKind :: Constant { value } = kind {
102109 value
@@ -106,7 +113,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
106113 ) ;
107114 return Err ( self . tcx . sess . delay_span_bug ( expr. span , msg) ) ;
108115 } ;
109- Ok ( ( Some ( value) , ascr) )
116+ Ok ( ( Some ( value) , ascr, inline_const ) )
110117 }
111118 }
112119 }
@@ -177,8 +184,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
177184 return Err ( self . tcx . sess . delay_span_bug ( span, msg) ) ;
178185 }
179186
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) ?;
182189
183190 let lo = lo. unwrap_or_else ( || {
184191 // Unwrap is ok because the type is known to be numeric.
@@ -237,6 +244,12 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
237244 } ;
238245 }
239246 }
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+ }
240253 Ok ( kind)
241254 }
242255
@@ -599,11 +612,9 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
599612 // const eval path below.
600613 // FIXME: investigate the performance impact of removing this.
601614 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 } ) ,
607618 _ => None ,
608619 } ,
609620 _ => None ,
@@ -633,13 +644,13 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
633644 if let Ok ( Some ( valtree) ) =
634645 self . tcx . const_eval_resolve_for_typeck ( self . param_env , ct, Some ( span) )
635646 {
636- self . const_to_pat (
647+ let subpattern = self . const_to_pat (
637648 Const :: Ty ( ty:: Const :: new_value ( self . tcx , valtree, ty) ) ,
638649 id,
639650 span,
640651 None ,
641- )
642- . kind
652+ ) ;
653+ PatKind :: InlineConstant { subpattern , def : def_id }
643654 } else {
644655 // If that fails, convert it to an opaque constant pattern.
645656 match tcx. const_eval_resolve ( self . param_env , uneval, Some ( span) ) {
@@ -822,6 +833,9 @@ impl<'tcx> PatternFoldable<'tcx> for PatKind<'tcx> {
822833 PatKind :: Deref { subpattern : subpattern. fold_with ( folder) }
823834 }
824835 PatKind :: Constant { value } => PatKind :: Constant { value } ,
836+ PatKind :: InlineConstant { def, subpattern : ref pattern } => {
837+ PatKind :: InlineConstant { def, subpattern : pattern. fold_with ( folder) }
838+ }
825839 PatKind :: Range ( ref range) => PatKind :: Range ( range. clone ( ) ) ,
826840 PatKind :: Slice { ref prefix, ref slice, ref suffix } => PatKind :: Slice {
827841 prefix : prefix. fold_with ( folder) ,
0 commit comments