Skip to content

Commit 412932e

Browse files
Merge pull request #20578 from ShoyuVanilla/inftbl
Migrate `InferenceTable` into next-solver
2 parents 6da1ce7 + 4a8bc8d commit 412932e

39 files changed

+2071
-819
lines changed

crates/hir-ty/src/autoderef.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use triomphe::Arc;
1313

1414
use crate::{
1515
Canonical, Goal, Interner, ProjectionTyExt, TraitEnvironment, Ty, TyBuilder, TyKind,
16-
db::HirDatabase, infer::unify::InferenceTable,
16+
db::HirDatabase, infer::unify::InferenceTable, next_solver::mapping::ChalkToNextSolver,
1717
};
1818

1919
const AUTODEREF_RECURSION_LIMIT: usize = 20;
@@ -98,7 +98,7 @@ impl<'table, 'db> Autoderef<'table, 'db> {
9898
explicit: bool,
9999
use_receiver_trait: bool,
100100
) -> Self {
101-
let ty = table.resolve_ty_shallow(&ty);
101+
let ty = table.structurally_resolve_type(&ty);
102102
Autoderef { table, ty, at_start: true, steps: Vec::new(), explicit, use_receiver_trait }
103103
}
104104

@@ -114,7 +114,7 @@ impl<'table, 'db> Autoderef<'table, 'db, usize> {
114114
explicit: bool,
115115
use_receiver_trait: bool,
116116
) -> Self {
117-
let ty = table.resolve_ty_shallow(&ty);
117+
let ty = table.structurally_resolve_type(&ty);
118118
Autoderef { table, ty, at_start: true, steps: 0, explicit, use_receiver_trait }
119119
}
120120
}
@@ -160,7 +160,7 @@ pub(crate) fn autoderef_step(
160160
use_receiver_trait: bool,
161161
) -> Option<(AutoderefKind, Ty)> {
162162
if let Some(derefed) = builtin_deref(table.db, &ty, explicit) {
163-
Some((AutoderefKind::Builtin, table.resolve_ty_shallow(derefed)))
163+
Some((AutoderefKind::Builtin, table.structurally_resolve_type(derefed)))
164164
} else {
165165
Some((AutoderefKind::Overloaded, deref_by_trait(table, ty, use_receiver_trait)?))
166166
}
@@ -187,7 +187,7 @@ pub(crate) fn deref_by_trait(
187187
use_receiver_trait: bool,
188188
) -> Option<Ty> {
189189
let _p = tracing::info_span!("deref_by_trait").entered();
190-
if table.resolve_ty_shallow(&ty).inference_var(Interner).is_some() {
190+
if table.structurally_resolve_type(&ty).inference_var(Interner).is_some() {
191191
// don't try to deref unknown variables
192192
return None;
193193
}
@@ -229,8 +229,8 @@ pub(crate) fn deref_by_trait(
229229
return None;
230230
}
231231

232-
table.register_obligation(implements_goal);
232+
table.register_obligation(implements_goal.to_nextsolver(table.interner));
233233

234234
let result = table.normalize_projection_ty(projection);
235-
Some(table.resolve_ty_shallow(&result))
235+
Some(table.structurally_resolve_type(&result))
236236
}

crates/hir-ty/src/chalk_ext.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -245,26 +245,30 @@ impl TyExt for Ty {
245245
}
246246

247247
fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<QuantifiedWhereClause>> {
248+
let handle_async_block_type_impl_trait = |def: DefWithBodyId| {
249+
let krate = def.module(db).krate();
250+
if let Some(future_trait) = LangItem::Future.resolve_trait(db, krate) {
251+
// This is only used by type walking.
252+
// Parameters will be walked outside, and projection predicate is not used.
253+
// So just provide the Future trait.
254+
let impl_bound = Binders::empty(
255+
Interner,
256+
WhereClause::Implemented(TraitRef {
257+
trait_id: to_chalk_trait_id(future_trait),
258+
substitution: Substitution::empty(Interner),
259+
}),
260+
);
261+
Some(vec![impl_bound])
262+
} else {
263+
None
264+
}
265+
};
266+
248267
match self.kind(Interner) {
249268
TyKind::OpaqueType(opaque_ty_id, subst) => {
250269
match db.lookup_intern_impl_trait_id((*opaque_ty_id).into()) {
251270
ImplTraitId::AsyncBlockTypeImplTrait(def, _expr) => {
252-
let krate = def.module(db).krate();
253-
if let Some(future_trait) = LangItem::Future.resolve_trait(db, krate) {
254-
// This is only used by type walking.
255-
// Parameters will be walked outside, and projection predicate is not used.
256-
// So just provide the Future trait.
257-
let impl_bound = Binders::empty(
258-
Interner,
259-
WhereClause::Implemented(TraitRef {
260-
trait_id: to_chalk_trait_id(future_trait),
261-
substitution: Substitution::empty(Interner),
262-
}),
263-
);
264-
Some(vec![impl_bound])
265-
} else {
266-
None
267-
}
271+
handle_async_block_type_impl_trait(def)
268272
}
269273
ImplTraitId::ReturnTypeImplTrait(func, idx) => {
270274
db.return_type_impl_traits(func).map(|it| {
@@ -299,8 +303,9 @@ impl TyExt for Ty {
299303
data.substitute(Interner, &opaque_ty.substitution)
300304
})
301305
}
302-
// It always has an parameter for Future::Output type.
303-
ImplTraitId::AsyncBlockTypeImplTrait(..) => unreachable!(),
306+
ImplTraitId::AsyncBlockTypeImplTrait(def, _) => {
307+
return handle_async_block_type_impl_trait(def);
308+
}
304309
};
305310

306311
predicates.map(|it| it.into_value_and_skipped_binders().0)

crates/hir-ty/src/consteval_nextsolver.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::{
2727
next_solver::{
2828
Const, ConstBytes, ConstKind, DbInterner, ErrorGuaranteed, GenericArg, GenericArgs,
2929
ParamConst, SolverDefId, Ty, ValueConst,
30-
mapping::{ChalkToNextSolver, convert_args_for_result, convert_binder_to_early_binder},
30+
mapping::{ChalkToNextSolver, NextSolverToChalk, convert_binder_to_early_binder},
3131
},
3232
};
3333

@@ -145,7 +145,7 @@ pub fn try_const_usize<'db>(db: &'db dyn HirDatabase, c: Const<'db>) -> Option<u
145145
SolverDefId::StaticId(id) => GeneralConstId::StaticId(id),
146146
_ => unreachable!(),
147147
};
148-
let subst = convert_args_for_result(interner, unevaluated_const.args.as_slice());
148+
let subst = unevaluated_const.args.to_chalk(interner);
149149
let ec = db.const_eval(c, subst, None).ok()?.to_nextsolver(interner);
150150
try_const_usize(db, ec)
151151
}
@@ -168,7 +168,7 @@ pub fn try_const_isize<'db>(db: &'db dyn HirDatabase, c: &Const<'db>) -> Option<
168168
SolverDefId::StaticId(id) => GeneralConstId::StaticId(id),
169169
_ => unreachable!(),
170170
};
171-
let subst = convert_args_for_result(interner, unevaluated_const.args.as_slice());
171+
let subst = unevaluated_const.args.to_chalk(interner);
172172
let ec = db.const_eval(c, subst, None).ok()?.to_nextsolver(interner);
173173
try_const_isize(db, &ec)
174174
}

crates/hir-ty/src/infer.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ use stdx::{always, never};
5555
use triomphe::Arc;
5656

5757
use crate::{
58-
AliasEq, AliasTy, Binders, ClosureId, Const, DomainGoal, GenericArg, Goal, ImplTraitId,
59-
ImplTraitIdx, InEnvironment, IncorrectGenericsLenKind, Interner, Lifetime, OpaqueTyId,
60-
ParamLoweringMode, PathLoweringDiagnostic, ProjectionTy, Substitution, TraitEnvironment, Ty,
61-
TyBuilder, TyExt,
58+
AliasEq, AliasTy, Binders, ClosureId, Const, DomainGoal, GenericArg, ImplTraitId, ImplTraitIdx,
59+
IncorrectGenericsLenKind, Interner, Lifetime, OpaqueTyId, ParamLoweringMode,
60+
PathLoweringDiagnostic, ProjectionTy, Substitution, TraitEnvironment, Ty, TyBuilder, TyExt,
6261
db::HirDatabase,
6362
fold_tys,
6463
generics::Generics,
@@ -70,6 +69,7 @@ use crate::{
7069
},
7170
lower::{ImplTraitLoweringMode, LifetimeElisionKind, diagnostics::TyLoweringDiagnostic},
7271
mir::MirSpan,
72+
next_solver::{self, mapping::ChalkToNextSolver},
7373
static_lifetime, to_assoc_type_id,
7474
traits::FnTrait,
7575
utils::UnevaluatedConstEvaluatorFolder,
@@ -182,13 +182,13 @@ impl BindingMode {
182182
}
183183

184184
#[derive(Debug)]
185-
pub(crate) struct InferOk<T> {
185+
pub(crate) struct InferOk<'db, T> {
186186
value: T,
187-
goals: Vec<InEnvironment<Goal>>,
187+
goals: Vec<next_solver::Goal<'db, next_solver::Predicate<'db>>>,
188188
}
189189

190-
impl<T> InferOk<T> {
191-
fn map<U>(self, f: impl FnOnce(T) -> U) -> InferOk<U> {
190+
impl<'db, T> InferOk<'db, T> {
191+
fn map<U>(self, f: impl FnOnce(T) -> U) -> InferOk<'db, U> {
192192
InferOk { value: f(self.value), goals: self.goals }
193193
}
194194
}
@@ -203,7 +203,7 @@ pub enum InferenceTyDiagnosticSource {
203203

204204
#[derive(Debug)]
205205
pub(crate) struct TypeError;
206-
pub(crate) type InferResult<T> = Result<InferOk<T>, TypeError>;
206+
pub(crate) type InferResult<'db, T> = Result<InferOk<'db, T>, TypeError>;
207207

208208
#[derive(Debug, PartialEq, Eq, Clone)]
209209
pub enum InferenceDiagnostic {
@@ -832,6 +832,7 @@ impl<'db> InferenceContext<'db> {
832832
coercion_casts,
833833
diagnostics: _,
834834
} = &mut result;
835+
table.resolve_obligations_as_possible();
835836
table.fallback_if_possible();
836837

837838
// Comment from rustc:
@@ -1480,7 +1481,8 @@ impl<'db> InferenceContext<'db> {
14801481
}
14811482

14821483
fn push_obligation(&mut self, o: DomainGoal) {
1483-
self.table.register_obligation(o.cast(Interner));
1484+
let goal: crate::Goal = o.cast(Interner);
1485+
self.table.register_obligation(goal.to_nextsolver(self.table.interner));
14841486
}
14851487

14861488
fn unify(&mut self, ty1: &Ty, ty2: &Ty) -> bool {
@@ -1746,7 +1748,7 @@ impl<'db> InferenceContext<'db> {
17461748

17471749
ty = self.table.insert_type_vars(ty);
17481750
ty = self.table.normalize_associated_types_in(ty);
1749-
ty = self.table.resolve_ty_shallow(&ty);
1751+
ty = self.table.structurally_resolve_type(&ty);
17501752
if ty.is_unknown() {
17511753
return (self.err_ty(), None);
17521754
}
@@ -1817,7 +1819,7 @@ impl<'db> InferenceContext<'db> {
18171819
let ty = match ty.kind(Interner) {
18181820
TyKind::Alias(AliasTy::Projection(proj_ty)) => {
18191821
let ty = self.table.normalize_projection_ty(proj_ty.clone());
1820-
self.table.resolve_ty_shallow(&ty)
1822+
self.table.structurally_resolve_type(&ty)
18211823
}
18221824
_ => ty,
18231825
};
@@ -2047,7 +2049,7 @@ impl Expectation {
20472049
fn adjust_for_branches(&self, table: &mut unify::InferenceTable<'_>) -> Expectation {
20482050
match self {
20492051
Expectation::HasType(ety) => {
2050-
let ety = table.resolve_ty_shallow(ety);
2052+
let ety = table.structurally_resolve_type(ety);
20512053
if ety.is_ty_var() { Expectation::None } else { Expectation::HasType(ety) }
20522054
}
20532055
Expectation::RValueLikeUnsized(ety) => Expectation::RValueLikeUnsized(ety.clone()),

crates/hir-ty/src/infer/closure.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use crate::{
3939
infer::{BreakableKind, CoerceMany, Diverges, coerce::CoerceNever},
4040
make_binders,
4141
mir::{BorrowKind, MirSpan, MutBorrowKind, ProjectionElem},
42+
next_solver::mapping::ChalkToNextSolver,
4243
to_assoc_type_id,
4344
traits::FnTrait,
4445
utils::{self, elaborate_clause_supertraits},
@@ -437,10 +438,10 @@ impl InferenceContext<'_> {
437438
associated_ty_id: to_assoc_type_id(future_output),
438439
substitution: Substitution::from1(Interner, ret_param_future.clone()),
439440
});
440-
self.table.register_obligation(
441+
let goal: crate::Goal =
441442
crate::AliasEq { alias: future_projection, ty: ret_param_future_output.clone() }
442-
.cast(Interner),
443-
);
443+
.cast(Interner);
444+
self.table.register_obligation(goal.to_nextsolver(self.table.interner));
444445

445446
Some(FnSubst(Substitution::from_iter(
446447
Interner,
@@ -568,7 +569,10 @@ impl InferenceContext<'_> {
568569
let supplied_sig = self.supplied_sig_of_closure(body, ret_type, arg_types, closure_kind);
569570

570571
let snapshot = self.table.snapshot();
571-
if !self.table.unify(&expected_sig.substitution, &supplied_sig.expected_sig.substitution) {
572+
if !self.table.unify::<_, crate::next_solver::GenericArgs<'_>>(
573+
&expected_sig.substitution.0,
574+
&supplied_sig.expected_sig.substitution.0,
575+
) {
572576
self.table.rollback_to(snapshot);
573577
}
574578

0 commit comments

Comments
 (0)