@@ -13,7 +13,9 @@ use hir_expand::name::Name;
1313
1414use super :: { BindingMode , Expectation , InferenceContext } ;
1515use crate :: {
16- lower:: lower_to_chalk_mutability, utils:: variant_data, Interner , Substitution , Ty , TyKind ,
16+ lower:: lower_to_chalk_mutability,
17+ utils:: { generics, variant_data} ,
18+ Interner , Substitution , Ty , TyKind ,
1719} ;
1820
1921impl < ' a > InferenceContext < ' a > {
@@ -233,13 +235,31 @@ impl<'a> InferenceContext<'a> {
233235 Pat :: Lit ( expr) => self . infer_expr ( * expr, & Expectation :: has_type ( expected. clone ( ) ) ) ,
234236 Pat :: Box { inner } => match self . resolve_boxed_box ( ) {
235237 Some ( box_adt) => {
236- let inner_expected = match expected. as_adt ( ) {
237- Some ( ( adt, substs) ) if adt == box_adt => substs. as_single ( ) . clone ( ) ,
238- _ => self . result . standard_types . unknown . clone ( ) ,
238+ let ( inner_ty, alloc_ty) = match expected. as_adt ( ) {
239+ Some ( ( adt, subst) ) if adt == box_adt => {
240+ ( subst[ 0 ] . clone ( ) , subst. get ( 1 ) . cloned ( ) )
241+ }
242+ _ => ( self . result . standard_types . unknown . clone ( ) , None ) ,
239243 } ;
240244
241- let inner_ty = self . infer_pat ( * inner, & inner_expected, default_bm) ;
242- Ty :: adt_ty ( box_adt, Substitution :: single ( inner_ty) )
245+ let inner_ty = self . infer_pat ( * inner, & inner_ty, default_bm) ;
246+ let mut sb = Substitution :: build_for_generics ( & generics (
247+ self . db . upcast ( ) ,
248+ box_adt. into ( ) ,
249+ ) ) ;
250+ sb = sb. push ( inner_ty) ;
251+ if sb. remaining ( ) == 1 {
252+ sb = sb. push ( match alloc_ty {
253+ Some ( alloc_ty) if !alloc_ty. is_unknown ( ) => alloc_ty,
254+ _ => match self . db . generic_defaults ( box_adt. into ( ) ) . get ( 1 ) {
255+ Some ( alloc_ty) if !alloc_ty. value . is_unknown ( ) => {
256+ alloc_ty. value . clone ( )
257+ }
258+ _ => self . table . new_type_var ( ) ,
259+ } ,
260+ } ) ;
261+ }
262+ Ty :: adt_ty ( box_adt, sb. build ( ) )
243263 }
244264 None => self . err_ty ( ) ,
245265 } ,
0 commit comments