@@ -412,11 +412,18 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
412
412
ty
413
413
}
414
414
415
+ /// Recurses through the given type, normalizing associated types mentioned
416
+ /// in it by replacing them by type variables and registering obligations to
417
+ /// resolve later. This should be done once for every type we get from some
418
+ /// type annotation (e.g. from a let type annotation, field type or function
419
+ /// call). `make_ty` handles this already, but e.g. for field types we need
420
+ /// to do it as well.
415
421
fn normalize_associated_types_in ( & mut self , ty : Ty ) -> Ty {
422
+ let ty = self . resolve_ty_as_possible ( & mut vec ! [ ] , ty) ;
416
423
ty. fold ( & mut |ty| match ty {
417
424
Ty :: Projection ( proj_ty) => self . normalize_projection_ty ( proj_ty) ,
418
425
Ty :: UnselectedProjection ( proj_ty) => {
419
- // FIXME
426
+ // FIXME use Chalk's unselected projection support
420
427
Ty :: UnselectedProjection ( proj_ty)
421
428
}
422
429
_ => ty,
@@ -569,6 +576,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
569
576
let substs = Ty :: substs_from_path ( self . db , & self . resolver , path, typable) ;
570
577
let ty = ty. subst ( & substs) ;
571
578
let ty = self . insert_type_vars ( ty) ;
579
+ let ty = self . normalize_associated_types_in ( ty) ;
572
580
Some ( ty)
573
581
}
574
582
Resolution :: LocalBinding ( pat) => {
@@ -690,6 +698,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
690
698
. and_then ( |d| d. field ( self . db , & Name :: tuple_field_name ( i) ) )
691
699
. map_or ( Ty :: Unknown , |field| field. ty ( self . db ) )
692
700
. subst ( & substs) ;
701
+ let expected_ty = self . normalize_associated_types_in ( expected_ty) ;
693
702
self . infer_pat ( subpat, & expected_ty, default_bm) ;
694
703
}
695
704
@@ -717,6 +726,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
717
726
let matching_field = def. and_then ( |it| it. field ( self . db , & subpat. name ) ) ;
718
727
let expected_ty =
719
728
matching_field. map_or ( Ty :: Unknown , |field| field. ty ( self . db ) ) . subst ( & substs) ;
729
+ let expected_ty = self . normalize_associated_types_in ( expected_ty) ;
720
730
self . infer_pat ( subpat. pat , & expected_ty, default_bm) ;
721
731
}
722
732
@@ -947,9 +957,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
947
957
self . unify ( & expected_receiver_ty, & actual_receiver_ty) ;
948
958
949
959
let param_iter = param_tys. into_iter ( ) . chain ( repeat ( Ty :: Unknown ) ) ;
950
- for ( arg, param) in args. iter ( ) . zip ( param_iter) {
951
- self . infer_expr ( * arg, & Expectation :: has_type ( param) ) ;
960
+ for ( arg, param_ty) in args. iter ( ) . zip ( param_iter) {
961
+ let param_ty = self . normalize_associated_types_in ( param_ty) ;
962
+ self . infer_expr ( * arg, & Expectation :: has_type ( param_ty) ) ;
952
963
}
964
+ let ret_ty = self . normalize_associated_types_in ( ret_ty) ;
953
965
ret_ty
954
966
}
955
967
@@ -1040,9 +1052,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1040
1052
} ;
1041
1053
self . register_obligations_for_call ( & callee_ty) ;
1042
1054
let param_iter = param_tys. into_iter ( ) . chain ( repeat ( Ty :: Unknown ) ) ;
1043
- for ( arg, param) in args. iter ( ) . zip ( param_iter) {
1044
- self . infer_expr ( * arg, & Expectation :: has_type ( param) ) ;
1055
+ for ( arg, param_ty) in args. iter ( ) . zip ( param_iter) {
1056
+ let param_ty = self . normalize_associated_types_in ( param_ty) ;
1057
+ self . infer_expr ( * arg, & Expectation :: has_type ( param_ty) ) ;
1045
1058
}
1059
+ let ret_ty = self . normalize_associated_types_in ( ret_ty) ;
1046
1060
ret_ty
1047
1061
}
1048
1062
Expr :: MethodCall { receiver, args, method_name, generic_args } => self
@@ -1140,7 +1154,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1140
1154
_ => None ,
1141
1155
} )
1142
1156
. unwrap_or ( Ty :: Unknown ) ;
1143
- self . insert_type_vars ( ty)
1157
+ let ty = self . insert_type_vars ( ty) ;
1158
+ self . normalize_associated_types_in ( ty)
1144
1159
}
1145
1160
Expr :: Await { expr } => {
1146
1161
let inner_ty = self . infer_expr ( * expr, & Expectation :: none ( ) ) ;
0 commit comments