@@ -6,6 +6,7 @@ use chalk_ir::Mutability;
66use hir_def:: {
77 expr:: { BindingAnnotation , Expr , Literal , Pat , PatId , RecordFieldPat } ,
88 path:: Path ,
9+ type_ref:: ConstScalar ,
910} ;
1011use hir_expand:: name:: Name ;
1112
@@ -14,7 +15,8 @@ use crate::{
1415 Adjust , Adjustment , AutoBorrow , BindingMode , Expectation , InferenceContext , TypeMismatch ,
1516 } ,
1617 lower:: lower_to_chalk_mutability,
17- static_lifetime, Interner , Substitution , Ty , TyBuilder , TyExt , TyKind ,
18+ static_lifetime, ConcreteConst , ConstValue , Interner , Substitution , Ty , TyBuilder , TyExt ,
19+ TyKind ,
1820} ;
1921
2022impl < ' a > InferenceContext < ' a > {
@@ -232,16 +234,28 @@ impl<'a> InferenceContext<'a> {
232234 self . infer_pat ( pat_id, & elem_ty, default_bm) ;
233235 }
234236
235- let pat_ty = match expected. kind ( Interner ) {
236- TyKind :: Array ( _, const_) => TyKind :: Array ( elem_ty, const_. clone ( ) ) ,
237- _ => TyKind :: Slice ( elem_ty) ,
238- }
239- . intern ( Interner ) ;
240237 if let & Some ( slice_pat_id) = slice {
241- self . infer_pat ( slice_pat_id, & pat_ty, default_bm) ;
238+ let rest_pat_ty = match expected. kind ( Interner ) {
239+ TyKind :: Array ( _, length) => {
240+ let length = match length. data ( Interner ) . value {
241+ ConstValue :: Concrete ( ConcreteConst {
242+ interned : ConstScalar :: Usize ( length) ,
243+ } ) => length. checked_sub ( ( prefix. len ( ) + suffix. len ( ) ) as u64 ) ,
244+ _ => None ,
245+ } ;
246+ TyKind :: Array ( elem_ty. clone ( ) , crate :: consteval:: usize_const ( length) )
247+ }
248+ _ => TyKind :: Slice ( elem_ty. clone ( ) ) ,
249+ }
250+ . intern ( Interner ) ;
251+ self . infer_pat ( slice_pat_id, & rest_pat_ty, default_bm) ;
242252 }
243253
244- pat_ty
254+ match expected. kind ( Interner ) {
255+ TyKind :: Array ( _, const_) => TyKind :: Array ( elem_ty, const_. clone ( ) ) ,
256+ _ => TyKind :: Slice ( elem_ty) ,
257+ }
258+ . intern ( Interner )
245259 }
246260 Pat :: Wild => expected. clone ( ) ,
247261 Pat :: Range { start, end } => {
0 commit comments