Skip to content

Commit d24e8c1

Browse files
jackh726ShoyuVanilla
authored andcommitted
WIP switch inference table to next-solver
1 parent 6da1ce7 commit d24e8c1

File tree

17 files changed

+2292
-526
lines changed

17 files changed

+2292
-526
lines changed

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, 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 = ChalkToNextSolver::from_nextsolver(unevaluated_const.args, 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 = ChalkToNextSolver::from_nextsolver(unevaluated_const.args, 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/closure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ impl InferenceContext<'_> {
568568
let supplied_sig = self.supplied_sig_of_closure(body, ret_type, arg_types, closure_kind);
569569

570570
let snapshot = self.table.snapshot();
571-
if !self.table.unify(&expected_sig.substitution, &supplied_sig.expected_sig.substitution) {
571+
if !self.table.unify::<_, crate::next_solver::GenericArgs<'_>>(&expected_sig.substitution.0, &supplied_sig.expected_sig.substitution.0) {
572572
self.table.rollback_to(snapshot);
573573
}
574574

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

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,20 @@
77
88
use std::iter;
99

10-
use chalk_ir::{BoundVar, Goal, Mutability, TyKind, TyVariableKind, cast::Cast};
11-
use hir_def::{hir::ExprId, lang_item::LangItem};
10+
use chalk_ir::{BoundVar, Mutability, TyKind, TyVariableKind, cast::Cast};
11+
use hir_def::{
12+
hir::ExprId,
13+
lang_item::LangItem,
14+
};
15+
use rustc_type_ir::solve::Certainty;
1216
use stdx::always;
1317
use triomphe::Arc;
1418

1519
use crate::{
16-
Canonical, DomainGoal, FnAbi, FnPointer, FnSig, InEnvironment, Interner, Lifetime,
17-
Substitution, TraitEnvironment, Ty, TyBuilder, TyExt,
18-
autoderef::{Autoderef, AutoderefKind},
19-
db::HirDatabase,
20-
infer::{
20+
autoderef::{Autoderef, AutoderefKind}, db::HirDatabase, infer::{
2121
Adjust, Adjustment, AutoBorrow, InferOk, InferenceContext, OverloadedDeref, PointerCast,
2222
TypeError, TypeMismatch,
23-
},
24-
traits::NextTraitSolveResult,
25-
utils::ClosureSubst,
23+
}, utils::ClosureSubst, Canonical, FnAbi, FnPointer, FnSig, Goal, InEnvironment, Interner, Lifetime, Substitution, TraitEnvironment, Ty, TyBuilder, TyExt
2624
};
2725

2826
use super::unify::InferenceTable;
@@ -42,7 +40,7 @@ fn simple(kind: Adjust) -> impl FnOnce(Ty) -> Vec<Adjustment> {
4240
fn success(
4341
adj: Vec<Adjustment>,
4442
target: Ty,
45-
goals: Vec<InEnvironment<Goal<Interner>>>,
43+
goals: Vec<InEnvironment<Goal>>,
4644
) -> CoerceResult {
4745
Ok(InferOk { goals, value: (adj, target) })
4846
}
@@ -304,7 +302,7 @@ impl InferenceTable<'_> {
304302
fn coerce_inner(&mut self, from_ty: Ty, to_ty: &Ty, coerce_never: CoerceNever) -> CoerceResult {
305303
if from_ty.is_never() {
306304
if let TyKind::InferenceVar(tv, TyVariableKind::General) = to_ty.kind(Interner) {
307-
self.set_diverging(*tv, true);
305+
self.set_diverging(*tv, TyVariableKind::General, true);
308306
}
309307
if coerce_never == CoerceNever::Yes {
310308
// Subtle: If we are coercing from `!` to `?T`, where `?T` is an unbound
@@ -707,41 +705,15 @@ impl InferenceTable<'_> {
707705
b.push(coerce_from).push(to_ty.clone()).build()
708706
};
709707

710-
let goal: InEnvironment<DomainGoal> =
711-
InEnvironment::new(&self.trait_env.env, coerce_unsized_tref.cast(Interner));
712-
713-
let canonicalized = self.canonicalize_with_free_vars(goal);
714-
715-
// FIXME: rustc's coerce_unsized is more specialized -- it only tries to
716-
// solve `CoerceUnsized` and `Unsize` goals at this point and leaves the
717-
// rest for later. Also, there's some logic about sized type variables.
718-
// Need to find out in what cases this is necessary
719-
let solution = self.db.trait_solve(
720-
krate,
721-
self.trait_env.block,
722-
canonicalized.value.clone().cast(Interner),
723-
);
724-
725-
match solution {
726-
// FIXME: this is a weaker guarantee than Chalk's `Guidance::Unique`
727-
// was. Chalk's unique guidance at least guarantees that the real solution
728-
// is some "subset" of the solutions matching the guidance, but the
729-
// substs for `Certainty::No` don't have that same guarantee (I think).
730-
NextTraitSolveResult::Certain(v) => {
731-
canonicalized.apply_solution(
732-
self,
733-
Canonical {
734-
binders: v.binders,
735-
// FIXME handle constraints
736-
value: v.value.subst,
737-
},
738-
);
739-
}
740-
// ...so, should think about how to get some actually get some guidance here
741-
NextTraitSolveResult::Uncertain(..) | NextTraitSolveResult::NoSolution => {
742-
return Err(TypeError);
708+
let goal: Goal = coerce_unsized_tref.cast(Interner);
709+
710+
self.commit_if_ok(|table| {
711+
match table.solve_obligation(goal) {
712+
Ok(Certainty::Yes) => Ok(()),
713+
Ok(Certainty::Maybe(_)) => Ok(()),
714+
Err(_) => Err(TypeError),
743715
}
744-
}
716+
})?;
745717

746718
let unsize =
747719
Adjustment { kind: Adjust::Pointer(PointerCast::Unsize), target: to_ty.clone() };

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

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,9 @@ use stdx::always;
2323
use syntax::ast::RangeOp;
2424

2525
use crate::{
26-
Adjust, Adjustment, AdtId, AutoBorrow, Binders, CallableDefId, CallableSig, DeclContext,
27-
DeclOrigin, IncorrectGenericsLenKind, Interner, Rawness, Scalar, Substitution,
28-
TraitEnvironment, TraitRef, Ty, TyBuilder, TyExt, TyKind,
29-
autoderef::{Autoderef, builtin_deref, deref_by_trait},
30-
consteval,
31-
generics::generics,
32-
infer::{
33-
BreakableKind,
34-
coerce::{CoerceMany, CoerceNever, CoercionCause},
35-
find_continuable,
36-
pat::contains_explicit_ref_binding,
37-
},
38-
lang_items::lang_items_for_bin_op,
39-
lower::{
40-
LifetimeElisionKind, ParamLoweringMode, lower_to_chalk_mutability,
41-
path::{GenericArgsLowerer, TypeLikeConst, substs_from_args_and_bindings},
42-
},
43-
mapping::{ToChalk, from_chalk},
44-
method_resolution::{self, VisibleFromModule},
45-
primitive::{self, UintTy},
46-
static_lifetime, to_chalk_trait_id,
47-
traits::FnTrait,
26+
autoderef::{builtin_deref, deref_by_trait, Autoderef}, consteval, generics::generics, infer::{
27+
coerce::{CoerceMany, CoerceNever, CoercionCause}, find_continuable, pat::contains_explicit_ref_binding, BreakableKind
28+
}, lang_items::lang_items_for_bin_op, lower::{lower_to_chalk_mutability, path::{substs_from_args_and_bindings, GenericArgsLowerer, TypeLikeConst}, ParamLoweringMode}, mapping::{from_chalk, ToChalk}, method_resolution::{self, VisibleFromModule}, next_solver::mapping::ChalkToNextSolver, primitive::{self, UintTy}, static_lifetime, to_chalk_trait_id, traits::FnTrait, Adjust, Adjustment, AdtId, AutoBorrow, Binders, CallableDefId, CallableSig, DeclContext, DeclOrigin, IncorrectGenericsLenKind, Interner, LifetimeElisionKind, Rawness, Scalar, Substitution, TraitEnvironment, TraitRef, Ty, TyBuilder, TyExt, TyKind
4829
};
4930

5031
use super::{
@@ -826,7 +807,7 @@ impl InferenceContext<'_> {
826807
let index_ty = self.infer_expr(*index, &Expectation::none(), ExprIsRead::Yes);
827808

828809
if let Some(index_trait) = self.resolve_lang_trait(LangItem::Index) {
829-
let canonicalized = self.canonicalize(base_ty.clone());
810+
let canonicalized = ChalkToNextSolver::from_nextsolver(self.canonicalize(base_ty.clone().to_nextsolver(self.table.interner)), self.table.interner);
830811
let receiver_adjustments = method_resolution::resolve_indexing_op(
831812
self.db,
832813
self.table.trait_env.clone(),
@@ -932,6 +913,7 @@ impl InferenceContext<'_> {
932913
}
933914
None => {
934915
let expected_ty = expected.to_option(&mut self.table);
916+
tracing::debug!(?expected_ty);
935917
let opt_ty = match expected_ty.as_ref().map(|it| it.kind(Interner)) {
936918
Some(TyKind::Scalar(Scalar::Int(_) | Scalar::Uint(_))) => expected_ty,
937919
Some(TyKind::Scalar(Scalar::Char)) => {
@@ -1678,7 +1660,7 @@ impl InferenceContext<'_> {
16781660
None => {
16791661
// no field found, lets attempt to resolve it like a function so that IDE things
16801662
// work out while people are typing
1681-
let canonicalized_receiver = self.canonicalize(receiver_ty.clone());
1663+
let canonicalized_receiver = self.canonicalize(receiver_ty.clone().to_nextsolver(self.table.interner));
16821664
let resolved = method_resolution::lookup_method(
16831665
self.db,
16841666
&canonicalized_receiver,
@@ -1824,7 +1806,7 @@ impl InferenceContext<'_> {
18241806
expected: &Expectation,
18251807
) -> Ty {
18261808
let receiver_ty = self.infer_expr_inner(receiver, &Expectation::none(), ExprIsRead::Yes);
1827-
let canonicalized_receiver = self.canonicalize(receiver_ty.clone());
1809+
let canonicalized_receiver = self.canonicalize(receiver_ty.clone().to_nextsolver(self.table.interner));
18281810

18291811
let resolved = method_resolution::lookup_method(
18301812
self.db,

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,7 @@ use hir_expand::name::Name;
1010
use stdx::never;
1111

1212
use crate::{
13-
InferenceDiagnostic, Interner, Substitution, TraitRef, TraitRefExt, Ty, TyBuilder, TyExt,
14-
TyKind, ValueTyDefId,
15-
builder::ParamKind,
16-
consteval, error_lifetime,
17-
generics::generics,
18-
infer::diagnostics::InferenceTyLoweringContext as TyLoweringContext,
19-
lower::LifetimeElisionKind,
20-
method_resolution::{self, VisibleFromModule},
21-
to_chalk_trait_id,
13+
builder::ParamKind, consteval, error_lifetime, generics::generics, infer::diagnostics::InferenceTyLoweringContext as TyLoweringContext, method_resolution::{self, VisibleFromModule}, next_solver::mapping::ChalkToNextSolver, to_chalk_trait_id, InferenceDiagnostic, Interner, LifetimeElisionKind, Substitution, TraitRef, TraitRefExt, Ty, TyBuilder, TyExt, TyKind, ValueTyDefId
2214
};
2315

2416
use super::{ExprOrPatId, InferenceContext, InferenceTyDiagnosticSource};
@@ -322,7 +314,7 @@ impl InferenceContext<'_> {
322314
return Some(result);
323315
}
324316

325-
let canonical_ty = self.canonicalize(ty.clone());
317+
let canonical_ty = self.canonicalize(ty.clone().to_nextsolver(self.table.interner));
326318

327319
let mut not_visible = None;
328320
let res = method_resolution::iterate_method_candidates(

0 commit comments

Comments
 (0)