@@ -19,6 +19,7 @@ pub(crate) mod closure;
19
19
mod coerce;
20
20
pub ( crate ) mod diagnostics;
21
21
mod expr;
22
+ mod fallback;
22
23
mod mutability;
23
24
mod pat;
24
25
mod path;
@@ -53,16 +54,16 @@ use indexmap::IndexSet;
53
54
use intern:: sym;
54
55
use la_arena:: { ArenaMap , Entry } ;
55
56
use rustc_hash:: { FxHashMap , FxHashSet } ;
57
+ use rustc_type_ir:: inherent:: Ty as _;
56
58
use stdx:: { always, never} ;
57
59
use triomphe:: Arc ;
58
60
59
- use crate :: db:: InternedClosureId ;
60
61
use crate :: {
61
62
AliasEq , AliasTy , Binders , ClosureId , Const , DomainGoal , GenericArg , ImplTraitId , ImplTraitIdx ,
62
63
IncorrectGenericsLenKind , Interner , Lifetime , OpaqueTyId , ParamLoweringMode ,
63
64
PathLoweringDiagnostic , ProjectionTy , Substitution , TargetFeatures , TraitEnvironment , Ty ,
64
65
TyBuilder , TyExt ,
65
- db:: HirDatabase ,
66
+ db:: { HirDatabase , InternedClosureId } ,
66
67
fold_tys,
67
68
generics:: Generics ,
68
69
infer:: {
@@ -75,6 +76,7 @@ use crate::{
75
76
mir:: MirSpan ,
76
77
next_solver:: {
77
78
self , DbInterner ,
79
+ infer:: { DefineOpaqueTypes , traits:: ObligationCause } ,
78
80
mapping:: { ChalkToNextSolver , NextSolverToChalk } ,
79
81
} ,
80
82
static_lifetime, to_assoc_type_id,
@@ -138,6 +140,20 @@ pub(crate) fn infer_query(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<Infer
138
140
139
141
ctx. infer_mut_body ( ) ;
140
142
143
+ ctx. type_inference_fallback ( ) ;
144
+
145
+ // Comment from rustc:
146
+ // Even though coercion casts provide type hints, we check casts after fallback for
147
+ // backwards compatibility. This makes fallback a stronger type hint than a cast coercion.
148
+ let cast_checks = std:: mem:: take ( & mut ctx. deferred_cast_checks ) ;
149
+ for mut cast in cast_checks. into_iter ( ) {
150
+ if let Err ( diag) = cast. check ( & mut ctx) {
151
+ ctx. diagnostics . push ( diag) ;
152
+ }
153
+ }
154
+
155
+ ctx. table . select_obligations_where_possible ( ) ;
156
+
141
157
ctx. infer_closures ( ) ;
142
158
143
159
Arc :: new ( ctx. resolve_all ( ) )
@@ -165,7 +181,6 @@ pub(crate) fn normalize(db: &dyn HirDatabase, trait_env: Arc<TraitEnvironment<'_
165
181
166
182
let ty_with_vars = table. normalize_associated_types_in ( ty) ;
167
183
table. select_obligations_where_possible ( ) ;
168
- table. propagate_diverging_flag ( ) ;
169
184
table. resolve_completely ( ty_with_vars)
170
185
}
171
186
@@ -686,6 +701,25 @@ impl Index<BindingId> for InferenceResult {
686
701
}
687
702
}
688
703
704
+ #[ derive( Debug , Clone ) ]
705
+ struct InternedStandardTypesNextSolver < ' db > {
706
+ unit : crate :: next_solver:: Ty < ' db > ,
707
+ never : crate :: next_solver:: Ty < ' db > ,
708
+ i32 : crate :: next_solver:: Ty < ' db > ,
709
+ f64 : crate :: next_solver:: Ty < ' db > ,
710
+ }
711
+
712
+ impl < ' db > InternedStandardTypesNextSolver < ' db > {
713
+ fn new ( interner : DbInterner < ' db > ) -> Self {
714
+ Self {
715
+ unit : crate :: next_solver:: Ty :: new_unit ( interner) ,
716
+ never : crate :: next_solver:: Ty :: new ( interner, crate :: next_solver:: TyKind :: Never ) ,
717
+ i32 : crate :: next_solver:: Ty :: new_int ( interner, rustc_type_ir:: IntTy :: I32 ) ,
718
+ f64 : crate :: next_solver:: Ty :: new_float ( interner, rustc_type_ir:: FloatTy :: F64 ) ,
719
+ }
720
+ }
721
+ }
722
+
689
723
/// The inference context contains all information needed during type inference.
690
724
#[ derive( Clone , Debug ) ]
691
725
pub ( crate ) struct InferenceContext < ' db > {
@@ -718,6 +752,7 @@ pub(crate) struct InferenceContext<'db> {
718
752
resume_yield_tys : Option < ( Ty , Ty ) > ,
719
753
diverges : Diverges ,
720
754
breakables : Vec < BreakableContext < ' db > > ,
755
+ types : InternedStandardTypesNextSolver < ' db > ,
721
756
722
757
/// Whether we are inside the pattern of a destructuring assignment.
723
758
inside_assignment : bool ,
@@ -798,11 +833,13 @@ impl<'db> InferenceContext<'db> {
798
833
resolver : Resolver < ' db > ,
799
834
) -> Self {
800
835
let trait_env = db. trait_environment_for_body ( owner) ;
836
+ let table = unify:: InferenceTable :: new ( db, trait_env) ;
801
837
InferenceContext {
838
+ types : InternedStandardTypesNextSolver :: new ( table. interner ) ,
802
839
target_features : OnceCell :: new ( ) ,
803
840
generics : OnceCell :: new ( ) ,
804
841
result : InferenceResult :: default ( ) ,
805
- table : unify :: InferenceTable :: new ( db , trait_env ) ,
842
+ table,
806
843
tuple_field_accesses_rev : Default :: default ( ) ,
807
844
return_ty : TyKind :: Error . intern ( Interner ) , // set in collect_* calls
808
845
resume_yield_tys : None ,
@@ -865,24 +902,33 @@ impl<'db> InferenceContext<'db> {
865
902
self . result . has_errors = true ;
866
903
}
867
904
868
- // FIXME: This function should be private in module. It is currently only used in the consteval, since we need
869
- // `InferenceResult` in the middle of inference. See the fixme comment in `consteval::eval_to_const`. If you
870
- // used this function for another workaround, mention it here. If you really need this function and believe that
871
- // there is no problem in it being `pub(crate)`, remove this comment.
872
- pub ( crate ) fn resolve_all ( mut self ) -> InferenceResult {
873
- self . table . select_obligations_where_possible ( ) ;
874
- self . table . fallback_if_possible ( ) ;
905
+ /// Clones `self` and calls `resolve_all()` on it.
906
+ // FIXME: Remove this.
907
+ pub ( crate ) fn fixme_resolve_all_clone ( & self ) -> InferenceResult {
908
+ let mut ctx = self . clone ( ) ;
909
+
910
+ ctx. type_inference_fallback ( ) ;
875
911
876
912
// Comment from rustc:
877
913
// Even though coercion casts provide type hints, we check casts after fallback for
878
914
// backwards compatibility. This makes fallback a stronger type hint than a cast coercion.
879
- let cast_checks = std:: mem:: take ( & mut self . deferred_cast_checks ) ;
915
+ let cast_checks = std:: mem:: take ( & mut ctx . deferred_cast_checks ) ;
880
916
for mut cast in cast_checks. into_iter ( ) {
881
- if let Err ( diag) = cast. check ( & mut self ) {
882
- self . diagnostics . push ( diag) ;
917
+ if let Err ( diag) = cast. check ( & mut ctx ) {
918
+ ctx . diagnostics . push ( diag) ;
883
919
}
884
920
}
885
921
922
+ ctx. table . select_obligations_where_possible ( ) ;
923
+
924
+ ctx. resolve_all ( )
925
+ }
926
+
927
+ // FIXME: This function should be private in module. It is currently only used in the consteval, since we need
928
+ // `InferenceResult` in the middle of inference. See the fixme comment in `consteval::eval_to_const`. If you
929
+ // used this function for another workaround, mention it here. If you really need this function and believe that
930
+ // there is no problem in it being `pub(crate)`, remove this comment.
931
+ pub ( crate ) fn resolve_all ( self ) -> InferenceResult {
886
932
let InferenceContext {
887
933
mut table, mut result, tuple_field_accesses_rev, diagnostics, ..
888
934
} = self ;
@@ -914,11 +960,6 @@ impl<'db> InferenceContext<'db> {
914
960
diagnostics : _,
915
961
} = & mut result;
916
962
917
- // FIXME resolve obligations as well (use Guidance if necessary)
918
- table. select_obligations_where_possible ( ) ;
919
-
920
- // make sure diverging type variables are marked as such
921
- table. propagate_diverging_flag ( ) ;
922
963
for ty in type_of_expr. values_mut ( ) {
923
964
* ty = table. resolve_completely ( ty. clone ( ) ) ;
924
965
* has_errors = * has_errors || ty. contains_unknown ( ) ;
@@ -1673,6 +1714,22 @@ impl<'db> InferenceContext<'db> {
1673
1714
self . resolve_associated_type_with_params ( inner_ty, assoc_ty, & [ ] )
1674
1715
}
1675
1716
1717
+ fn demand_eqtype (
1718
+ & mut self ,
1719
+ expected : crate :: next_solver:: Ty < ' db > ,
1720
+ actual : crate :: next_solver:: Ty < ' db > ,
1721
+ ) {
1722
+ let result = self
1723
+ . table
1724
+ . infer_ctxt
1725
+ . at ( & ObligationCause :: new ( ) , self . table . trait_env . env )
1726
+ . eq ( DefineOpaqueTypes :: Yes , expected, actual)
1727
+ . map ( |infer_ok| self . table . register_infer_ok ( infer_ok) ) ;
1728
+ if let Err ( _err) = result {
1729
+ // FIXME: Emit diagnostic.
1730
+ }
1731
+ }
1732
+
1676
1733
fn resolve_associated_type_with_params (
1677
1734
& mut self ,
1678
1735
inner_ty : Ty ,
0 commit comments