@@ -19,6 +19,7 @@ pub(crate) mod closure;
1919mod coerce;
2020pub ( crate ) mod diagnostics;
2121mod expr;
22+ mod fallback;
2223mod mutability;
2324mod pat;
2425mod path;
@@ -53,16 +54,16 @@ use indexmap::IndexSet;
5354use intern:: sym;
5455use la_arena:: { ArenaMap , Entry } ;
5556use rustc_hash:: { FxHashMap , FxHashSet } ;
57+ use rustc_type_ir:: inherent:: Ty as _;
5658use stdx:: { always, never} ;
5759use triomphe:: Arc ;
5860
59- use crate :: db:: InternedClosureId ;
6061use crate :: {
6162 AliasEq , AliasTy , Binders , ClosureId , Const , DomainGoal , GenericArg , ImplTraitId , ImplTraitIdx ,
6263 IncorrectGenericsLenKind , Interner , Lifetime , OpaqueTyId , ParamLoweringMode ,
6364 PathLoweringDiagnostic , ProjectionTy , Substitution , TargetFeatures , TraitEnvironment , Ty ,
6465 TyBuilder , TyExt ,
65- db:: HirDatabase ,
66+ db:: { HirDatabase , InternedClosureId } ,
6667 fold_tys,
6768 generics:: Generics ,
6869 infer:: {
@@ -75,6 +76,7 @@ use crate::{
7576 mir:: MirSpan ,
7677 next_solver:: {
7778 self , DbInterner ,
79+ infer:: { DefineOpaqueTypes , traits:: ObligationCause } ,
7880 mapping:: { ChalkToNextSolver , NextSolverToChalk } ,
7981 } ,
8082 static_lifetime, to_assoc_type_id,
@@ -138,6 +140,20 @@ pub(crate) fn infer_query(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<Infer
138140
139141 ctx. infer_mut_body ( ) ;
140142
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+
141157 ctx. infer_closures ( ) ;
142158
143159 Arc :: new ( ctx. resolve_all ( ) )
@@ -165,7 +181,6 @@ pub(crate) fn normalize(db: &dyn HirDatabase, trait_env: Arc<TraitEnvironment<'_
165181
166182 let ty_with_vars = table. normalize_associated_types_in ( ty) ;
167183 table. select_obligations_where_possible ( ) ;
168- table. propagate_diverging_flag ( ) ;
169184 table. resolve_completely ( ty_with_vars)
170185}
171186
@@ -686,6 +701,25 @@ impl Index<BindingId> for InferenceResult {
686701 }
687702}
688703
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+
689723/// The inference context contains all information needed during type inference.
690724#[ derive( Clone , Debug ) ]
691725pub ( crate ) struct InferenceContext < ' db > {
@@ -718,6 +752,7 @@ pub(crate) struct InferenceContext<'db> {
718752 resume_yield_tys : Option < ( Ty , Ty ) > ,
719753 diverges : Diverges ,
720754 breakables : Vec < BreakableContext < ' db > > ,
755+ types : InternedStandardTypesNextSolver < ' db > ,
721756
722757 /// Whether we are inside the pattern of a destructuring assignment.
723758 inside_assignment : bool ,
@@ -798,11 +833,13 @@ impl<'db> InferenceContext<'db> {
798833 resolver : Resolver < ' db > ,
799834 ) -> Self {
800835 let trait_env = db. trait_environment_for_body ( owner) ;
836+ let table = unify:: InferenceTable :: new ( db, trait_env) ;
801837 InferenceContext {
838+ types : InternedStandardTypesNextSolver :: new ( table. interner ) ,
802839 target_features : OnceCell :: new ( ) ,
803840 generics : OnceCell :: new ( ) ,
804841 result : InferenceResult :: default ( ) ,
805- table : unify :: InferenceTable :: new ( db , trait_env ) ,
842+ table,
806843 tuple_field_accesses_rev : Default :: default ( ) ,
807844 return_ty : TyKind :: Error . intern ( Interner ) , // set in collect_* calls
808845 resume_yield_tys : None ,
@@ -865,24 +902,33 @@ impl<'db> InferenceContext<'db> {
865902 self . result . has_errors = true ;
866903 }
867904
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 ( ) ;
875911
876912 // Comment from rustc:
877913 // Even though coercion casts provide type hints, we check casts after fallback for
878914 // 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 ) ;
880916 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) ;
883919 }
884920 }
885921
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 {
886932 let InferenceContext {
887933 mut table, mut result, tuple_field_accesses_rev, diagnostics, ..
888934 } = self ;
@@ -914,11 +960,6 @@ impl<'db> InferenceContext<'db> {
914960 diagnostics : _,
915961 } = & mut result;
916962
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 ( ) ;
922963 for ty in type_of_expr. values_mut ( ) {
923964 * ty = table. resolve_completely ( ty. clone ( ) ) ;
924965 * has_errors = * has_errors || ty. contains_unknown ( ) ;
@@ -1673,6 +1714,22 @@ impl<'db> InferenceContext<'db> {
16731714 self . resolve_associated_type_with_params ( inner_ty, assoc_ty, & [ ] )
16741715 }
16751716
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+
16761733 fn resolve_associated_type_with_params (
16771734 & mut self ,
16781735 inner_ty : Ty ,
0 commit comments