@@ -413,39 +413,70 @@ impl<'a> InferenceContext<'a> {
413
413
}
414
414
}
415
415
416
- fn err_ty ( & self ) -> Ty {
417
- self . result . standard_types . unknown . clone ( )
418
- }
416
+ fn resolve_all ( self ) -> InferenceResult {
417
+ let InferenceContext { mut table, mut result, .. } = self ;
419
418
420
- fn resolve_all ( mut self ) -> InferenceResult {
421
419
// FIXME resolve obligations as well (use Guidance if necessary)
422
- self . table . resolve_obligations_as_possible ( ) ;
420
+ table. resolve_obligations_as_possible ( ) ;
423
421
424
422
// make sure diverging type variables are marked as such
425
- self . table . propagate_diverging_flag ( ) ;
426
- let mut result = std:: mem:: take ( & mut self . result ) ;
423
+ table. propagate_diverging_flag ( ) ;
427
424
for ty in result. type_of_expr . values_mut ( ) {
428
- * ty = self . table . resolve_completely ( ty. clone ( ) ) ;
425
+ * ty = table. resolve_completely ( ty. clone ( ) ) ;
429
426
}
430
427
for ty in result. type_of_pat . values_mut ( ) {
431
- * ty = self . table . resolve_completely ( ty. clone ( ) ) ;
428
+ * ty = table. resolve_completely ( ty. clone ( ) ) ;
432
429
}
433
430
for mismatch in result. type_mismatches . values_mut ( ) {
434
- mismatch. expected = self . table . resolve_completely ( mismatch. expected . clone ( ) ) ;
435
- mismatch. actual = self . table . resolve_completely ( mismatch. actual . clone ( ) ) ;
431
+ mismatch. expected = table. resolve_completely ( mismatch. expected . clone ( ) ) ;
432
+ mismatch. actual = table. resolve_completely ( mismatch. actual . clone ( ) ) ;
436
433
}
437
434
for ( _, subst) in result. method_resolutions . values_mut ( ) {
438
- * subst = self . table . resolve_completely ( subst. clone ( ) ) ;
435
+ * subst = table. resolve_completely ( subst. clone ( ) ) ;
439
436
}
440
437
for adjustment in result. expr_adjustments . values_mut ( ) . flatten ( ) {
441
- adjustment. target = self . table . resolve_completely ( adjustment. target . clone ( ) ) ;
438
+ adjustment. target = table. resolve_completely ( adjustment. target . clone ( ) ) ;
442
439
}
443
440
for adjustment in result. pat_adjustments . values_mut ( ) . flatten ( ) {
444
- adjustment. target = self . table . resolve_completely ( adjustment. target . clone ( ) ) ;
441
+ adjustment. target = table. resolve_completely ( adjustment. target . clone ( ) ) ;
445
442
}
446
443
result
447
444
}
448
445
446
+ fn collect_const ( & mut self , data : & ConstData ) {
447
+ self . return_ty = self . make_ty ( & data. type_ref ) ;
448
+ }
449
+
450
+ fn collect_static ( & mut self , data : & StaticData ) {
451
+ self . return_ty = self . make_ty ( & data. type_ref ) ;
452
+ }
453
+
454
+ fn collect_fn ( & mut self , data : & FunctionData ) {
455
+ let body = Arc :: clone ( & self . body ) ; // avoid borrow checker problem
456
+ let ctx = crate :: lower:: TyLoweringContext :: new ( self . db , & self . resolver )
457
+ . with_impl_trait_mode ( ImplTraitLoweringMode :: Param ) ;
458
+ let param_tys =
459
+ data. params . iter ( ) . map ( |( _, type_ref) | ctx. lower_ty ( type_ref) ) . collect :: < Vec < _ > > ( ) ;
460
+ for ( ty, pat) in param_tys. into_iter ( ) . zip ( body. params . iter ( ) ) {
461
+ let ty = self . insert_type_vars ( ty) ;
462
+ let ty = self . normalize_associated_types_in ( ty) ;
463
+
464
+ self . infer_pat ( * pat, & ty, BindingMode :: default ( ) ) ;
465
+ }
466
+ let error_ty = & TypeRef :: Error ;
467
+ let return_ty = if data. has_async_kw ( ) {
468
+ data. async_ret_type . as_deref ( ) . unwrap_or ( error_ty)
469
+ } else {
470
+ & * data. ret_type
471
+ } ;
472
+ let return_ty = self . make_ty_with_mode ( return_ty, ImplTraitLoweringMode :: Disallowed ) ; // FIXME implement RPIT
473
+ self . return_ty = return_ty;
474
+ }
475
+
476
+ fn infer_body ( & mut self ) {
477
+ self . infer_expr_coerce ( self . body . body_expr , & Expectation :: has_type ( self . return_ty . clone ( ) ) ) ;
478
+ }
479
+
449
480
fn write_expr_ty ( & mut self , expr : ExprId , ty : Ty ) {
450
481
self . result . type_of_expr . insert ( expr, ty) ;
451
482
}
@@ -491,6 +522,10 @@ impl<'a> InferenceContext<'a> {
491
522
self . make_ty_with_mode ( type_ref, ImplTraitLoweringMode :: Disallowed )
492
523
}
493
524
525
+ fn err_ty ( & self ) -> Ty {
526
+ self . result . standard_types . unknown . clone ( )
527
+ }
528
+
494
529
/// Replaces ConstScalar::Unknown by a new type var, so we can maybe still infer it.
495
530
fn insert_const_vars_shallow ( & mut self , c : Const ) -> Const {
496
531
let data = c. data ( Interner ) ;
@@ -544,6 +579,16 @@ impl<'a> InferenceContext<'a> {
544
579
self . table . unify ( ty1, ty2)
545
580
}
546
581
582
+ /// Recurses through the given type, normalizing associated types mentioned
583
+ /// in it by replacing them by type variables and registering obligations to
584
+ /// resolve later. This should be done once for every type we get from some
585
+ /// type annotation (e.g. from a let type annotation, field type or function
586
+ /// call). `make_ty` handles this already, but e.g. for field types we need
587
+ /// to do it as well.
588
+ fn normalize_associated_types_in ( & mut self , ty : Ty ) -> Ty {
589
+ self . table . normalize_associated_types_in ( ty)
590
+ }
591
+
547
592
fn resolve_ty_shallow ( & mut self , ty : & Ty ) -> Ty {
548
593
self . resolve_obligations_as_possible ( ) ;
549
594
self . table . resolve_ty_shallow ( ty)
@@ -586,16 +631,6 @@ impl<'a> InferenceContext<'a> {
586
631
}
587
632
}
588
633
589
- /// Recurses through the given type, normalizing associated types mentioned
590
- /// in it by replacing them by type variables and registering obligations to
591
- /// resolve later. This should be done once for every type we get from some
592
- /// type annotation (e.g. from a let type annotation, field type or function
593
- /// call). `make_ty` handles this already, but e.g. for field types we need
594
- /// to do it as well.
595
- fn normalize_associated_types_in ( & mut self , ty : Ty ) -> Ty {
596
- self . table . normalize_associated_types_in ( ty)
597
- }
598
-
599
634
fn resolve_variant ( & mut self , path : Option < & Path > , value_ns : bool ) -> ( Ty , Option < VariantId > ) {
600
635
let path = match path {
601
636
Some ( path) => path,
@@ -727,40 +762,6 @@ impl<'a> InferenceContext<'a> {
727
762
}
728
763
}
729
764
730
- fn collect_const ( & mut self , data : & ConstData ) {
731
- self . return_ty = self . make_ty ( & data. type_ref ) ;
732
- }
733
-
734
- fn collect_static ( & mut self , data : & StaticData ) {
735
- self . return_ty = self . make_ty ( & data. type_ref ) ;
736
- }
737
-
738
- fn collect_fn ( & mut self , data : & FunctionData ) {
739
- let body = Arc :: clone ( & self . body ) ; // avoid borrow checker problem
740
- let ctx = crate :: lower:: TyLoweringContext :: new ( self . db , & self . resolver )
741
- . with_impl_trait_mode ( ImplTraitLoweringMode :: Param ) ;
742
- let param_tys =
743
- data. params . iter ( ) . map ( |( _, type_ref) | ctx. lower_ty ( type_ref) ) . collect :: < Vec < _ > > ( ) ;
744
- for ( ty, pat) in param_tys. into_iter ( ) . zip ( body. params . iter ( ) ) {
745
- let ty = self . insert_type_vars ( ty) ;
746
- let ty = self . normalize_associated_types_in ( ty) ;
747
-
748
- self . infer_pat ( * pat, & ty, BindingMode :: default ( ) ) ;
749
- }
750
- let error_ty = & TypeRef :: Error ;
751
- let return_ty = if data. has_async_kw ( ) {
752
- data. async_ret_type . as_deref ( ) . unwrap_or ( error_ty)
753
- } else {
754
- & * data. ret_type
755
- } ;
756
- let return_ty = self . make_ty_with_mode ( return_ty, ImplTraitLoweringMode :: Disallowed ) ; // FIXME implement RPIT
757
- self . return_ty = return_ty;
758
- }
759
-
760
- fn infer_body ( & mut self ) {
761
- self . infer_expr_coerce ( self . body . body_expr , & Expectation :: has_type ( self . return_ty . clone ( ) ) ) ;
762
- }
763
-
764
765
fn resolve_lang_item ( & self , name : Name ) -> Option < LangItemTarget > {
765
766
let krate = self . resolver . krate ( ) ;
766
767
self . db . lang_item ( krate, name. to_smol_str ( ) )
0 commit comments