Skip to content

Commit 7d9af9f

Browse files
Merge pull request #20717 from ShoyuVanilla/migrate-more
internal: Migrate more predicate things to next-solver
2 parents 83981c1 + 0a5457a commit 7d9af9f

File tree

5 files changed

+122
-153
lines changed

5 files changed

+122
-153
lines changed

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

Lines changed: 43 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,35 @@ use hir_def::{AdtId, lang_item::LangItem};
1111
use hir_expand::name::Name;
1212
use intern::sym;
1313
use rustc_hash::{FxHashMap, FxHashSet};
14-
use rustc_type_ir::inherent::Ty as _;
1514
use rustc_type_ir::{
16-
FloatVid, IntVid, TyVid, TypeVisitableExt,
17-
inherent::{IntoKind, Span, Term as _},
15+
FloatVid, IntVid, TyVid, TypeVisitableExt, UpcastFrom,
16+
inherent::{IntoKind, Span, Term as _, Ty as _},
1817
relate::{Relate, solver_relating::RelateExt},
19-
solve::{Certainty, GoalSource, NoSolution},
18+
solve::{Certainty, GoalSource},
2019
};
2120
use smallvec::SmallVec;
2221
use triomphe::Arc;
2322

2423
use super::{InferResult, InferenceContext, TypeError};
25-
use crate::next_solver::ErrorGuaranteed;
2624
use crate::{
2725
AliasTy, BoundVar, Canonical, Const, ConstValue, DebruijnIndex, GenericArg, GenericArgData,
28-
Goal, GoalData, InEnvironment, InferenceVar, Interner, Lifetime, OpaqueTyId, ParamKind,
29-
ProjectionTy, Scalar, Substitution, TraitEnvironment, TraitRef, Ty, TyBuilder, TyExt, TyKind,
30-
VariableKind, WhereClause,
26+
InferenceVar, Interner, Lifetime, OpaqueTyId, ProjectionTy, Scalar, Substitution,
27+
TraitEnvironment, Ty, TyExt, TyKind, VariableKind,
3128
consteval::unknown_const,
3229
db::HirDatabase,
3330
fold_generic_args, fold_tys_and_consts,
34-
next_solver::infer::InferOk,
3531
next_solver::{
36-
self, ClauseKind, DbInterner, ParamEnv, Predicate, PredicateKind, SolverDefIds, Term,
32+
self, ClauseKind, DbInterner, ErrorGuaranteed, ParamEnv, Predicate, PredicateKind,
33+
SolverDefIds, Term, TraitRef,
3734
fulfill::FulfillmentCtxt,
3835
infer::{
39-
DbInternerInferExt, InferCtxt,
36+
DbInternerInferExt, InferCtxt, InferOk,
4037
snapshot::CombinedSnapshot,
4138
traits::{Obligation, ObligationCause},
4239
},
4340
inspect::{InspectConfig, InspectGoal, ProofTreeVisitor},
4441
mapping::{ChalkToNextSolver, NextSolverToChalk},
4542
},
46-
to_chalk_trait_id,
4743
traits::{
4844
FnTrait, NextTraitSolveResult, next_trait_solve_canonical_in_ctxt, next_trait_solve_in_ctxt,
4945
},
@@ -877,26 +873,15 @@ impl<'db> InferenceTable<'db> {
877873
/// whether a trait *might* be implemented before deciding to 'lock in' the
878874
/// choice (during e.g. method resolution or deref).
879875
#[tracing::instrument(level = "debug", skip(self))]
880-
pub(crate) fn try_obligation(&mut self, goal: Goal) -> NextTraitSolveResult {
881-
let in_env = InEnvironment::new(&self.trait_env.env, goal);
882-
let canonicalized = self.canonicalize(in_env.to_nextsolver(self.interner));
876+
pub(crate) fn try_obligation(&mut self, predicate: Predicate<'db>) -> NextTraitSolveResult {
877+
let goal = next_solver::Goal { param_env: self.param_env, predicate };
878+
let canonicalized = self.canonicalize(goal);
883879

884880
next_trait_solve_canonical_in_ctxt(&self.infer_ctxt, canonicalized)
885881
}
886882

887-
#[tracing::instrument(level = "debug", skip(self))]
888-
pub(crate) fn solve_obligation(&mut self, goal: Goal) -> Result<Certainty, NoSolution> {
889-
let goal = InEnvironment::new(&self.trait_env.env, goal);
890-
let goal = goal.to_nextsolver(self.interner);
891-
let result = next_trait_solve_in_ctxt(&self.infer_ctxt, goal);
892-
result.map(|m| m.1)
893-
}
894-
895883
pub(crate) fn register_obligation(&mut self, predicate: Predicate<'db>) {
896-
let goal = next_solver::Goal {
897-
param_env: self.trait_env.env.to_nextsolver(self.interner),
898-
predicate,
899-
};
884+
let goal = next_solver::Goal { param_env: self.param_env, predicate };
900885
self.register_obligation_in_env(goal)
901886
}
902887

@@ -984,7 +969,7 @@ impl<'db> InferenceTable<'db> {
984969
&mut self,
985970
ty: &Ty,
986971
num_args: usize,
987-
) -> Option<(FnTrait, Vec<crate::next_solver::Ty<'db>>, crate::next_solver::Ty<'db>)> {
972+
) -> Option<(FnTrait, Vec<next_solver::Ty<'db>>, next_solver::Ty<'db>)> {
988973
for (fn_trait_name, output_assoc_name, subtraits) in [
989974
(FnTrait::FnOnce, sym::Output, &[FnTrait::Fn, FnTrait::FnMut][..]),
990975
(FnTrait::AsyncFnMut, sym::CallRefFuture, &[FnTrait::AsyncFn]),
@@ -997,42 +982,34 @@ impl<'db> InferenceTable<'db> {
997982
trait_data.associated_type_by_name(&Name::new_symbol_root(output_assoc_name))?;
998983

999984
let mut arg_tys = Vec::with_capacity(num_args);
1000-
let arg_ty = TyBuilder::tuple(num_args)
1001-
.fill(|it| {
1002-
let arg = match it {
1003-
ParamKind::Type => self.new_type_var(),
1004-
ParamKind::Lifetime => unreachable!("Tuple with lifetime parameter"),
1005-
ParamKind::Const(_) => unreachable!("Tuple with const parameter"),
1006-
};
1007-
arg_tys.push(arg.to_nextsolver(self.interner));
1008-
arg.cast(Interner)
985+
let arg_ty = next_solver::Ty::new_tup_from_iter(
986+
self.interner,
987+
std::iter::repeat_with(|| {
988+
let ty = self.next_ty_var();
989+
arg_tys.push(ty);
990+
ty
1009991
})
1010-
.build();
1011-
1012-
let b = TyBuilder::trait_ref(self.db, fn_trait);
1013-
if b.remaining() != 2 {
1014-
return None;
1015-
}
1016-
let mut trait_ref = b.push(ty.clone()).push(arg_ty).build();
1017-
1018-
let projection = TyBuilder::assoc_type_projection(
1019-
self.db,
1020-
output_assoc_type,
1021-
Some(trait_ref.substitution.clone()),
1022-
)
1023-
.fill_with_unknown()
1024-
.build();
1025-
1026-
let goal: Goal = trait_ref.clone().cast(Interner);
1027-
if !self.try_obligation(goal.clone()).no_solution() {
1028-
self.register_obligation(goal.to_nextsolver(self.interner));
1029-
let return_ty =
1030-
self.normalize_projection_ty(projection).to_nextsolver(self.interner);
992+
.take(num_args),
993+
);
994+
let args = [ty.to_nextsolver(self.interner), arg_ty];
995+
let trait_ref = crate::next_solver::TraitRef::new(self.interner, fn_trait.into(), args);
996+
997+
let projection = crate::next_solver::Ty::new_alias(
998+
self.interner,
999+
rustc_type_ir::AliasTyKind::Projection,
1000+
crate::next_solver::AliasTy::new(self.interner, output_assoc_type.into(), args),
1001+
);
1002+
1003+
let pred = crate::next_solver::Predicate::upcast_from(trait_ref, self.interner);
1004+
if !self.try_obligation(pred).no_solution() {
1005+
self.register_obligation(pred);
1006+
let return_ty = self.normalize_alias_ty(projection);
10311007
for &fn_x in subtraits {
10321008
let fn_x_trait = fn_x.get_id(self.db, krate)?;
1033-
trait_ref.trait_id = to_chalk_trait_id(fn_x_trait);
1034-
let goal = trait_ref.clone().cast(Interner);
1035-
if !self.try_obligation(goal).no_solution() {
1009+
let trait_ref =
1010+
crate::next_solver::TraitRef::new(self.interner, fn_x_trait.into(), args);
1011+
let pred = crate::next_solver::Predicate::upcast_from(trait_ref, self.interner);
1012+
if !self.try_obligation(pred).no_solution() {
10361013
return Some((fn_x, arg_tys, return_ty));
10371014
}
10381015
}
@@ -1171,12 +1148,11 @@ impl<'db> InferenceTable<'db> {
11711148
let Some(sized) = LangItem::Sized.resolve_trait(self.db, self.trait_env.krate) else {
11721149
return false;
11731150
};
1174-
let sized_pred = WhereClause::Implemented(TraitRef {
1175-
trait_id: to_chalk_trait_id(sized),
1176-
substitution: Substitution::from1(Interner, ty),
1177-
});
1178-
let goal = GoalData::DomainGoal(chalk_ir::DomainGoal::Holds(sized_pred)).intern(Interner);
1179-
self.try_obligation(goal).certain()
1151+
let sized_pred = Predicate::upcast_from(
1152+
TraitRef::new(self.interner, sized.into(), [ty.to_nextsolver(self.interner)]),
1153+
self.interner,
1154+
);
1155+
self.try_obligation(sized_pred).certain()
11801156
}
11811157
}
11821158

crates/hir-ty/src/lib.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ use intern::{Symbol, sym};
9393
use la_arena::{Arena, Idx};
9494
use mir::{MirEvalError, VTableMap};
9595
use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};
96-
use rustc_type_ir::inherent::SliceLike;
96+
use rustc_type_ir::{
97+
UpcastFrom,
98+
inherent::{SliceLike, Ty as _},
99+
};
97100
use syntax::ast::{ConstArg, make};
98101
use traits::FnTrait;
99102
use triomphe::Arc;
@@ -106,7 +109,7 @@ use crate::{
106109
infer::unify::InferenceTable,
107110
next_solver::{
108111
DbInterner,
109-
mapping::{ChalkToNextSolver, convert_ty_for_result},
112+
mapping::{ChalkToNextSolver, NextSolverToChalk, convert_ty_for_result},
110113
},
111114
};
112115

@@ -957,26 +960,32 @@ pub fn callable_sig_from_fn_trait(
957960
// Register two obligations:
958961
// - Self: FnOnce<?args_ty>
959962
// - <Self as FnOnce<?args_ty>>::Output == ?ret_ty
960-
let args_ty = table.new_type_var();
961-
let mut trait_ref = b.push(self_ty.clone()).push(args_ty.clone()).build();
962-
let projection = TyBuilder::assoc_type_projection(
963-
db,
964-
output_assoc_type,
965-
Some(trait_ref.substitution.clone()),
966-
)
967-
.build();
968-
969-
let goal: Goal = trait_ref.clone().cast(Interner);
970-
let pred = goal.to_nextsolver(table.interner);
971-
if !table.try_obligation(goal).no_solution() {
963+
let args_ty = table.next_ty_var();
964+
let args = [self_ty.to_nextsolver(table.interner), args_ty];
965+
let trait_ref = crate::next_solver::TraitRef::new(table.interner, fn_once_trait.into(), args);
966+
let projection = crate::next_solver::Ty::new_alias(
967+
table.interner,
968+
rustc_type_ir::AliasTyKind::Projection,
969+
crate::next_solver::AliasTy::new(table.interner, output_assoc_type.into(), args),
970+
);
971+
972+
let pred = crate::next_solver::Predicate::upcast_from(trait_ref, table.interner);
973+
if !table.try_obligation(pred).no_solution() {
972974
table.register_obligation(pred);
973-
let return_ty = table.normalize_projection_ty(projection);
975+
let return_ty = table.normalize_alias_ty(projection);
974976
for fn_x in [FnTrait::Fn, FnTrait::FnMut, FnTrait::FnOnce] {
975977
let fn_x_trait = fn_x.get_id(db, krate)?;
976-
trait_ref.trait_id = to_chalk_trait_id(fn_x_trait);
977-
if !table.try_obligation(trait_ref.clone().cast(Interner)).no_solution() {
978-
let ret_ty = table.resolve_completely(return_ty);
979-
let args_ty = table.resolve_completely(args_ty);
978+
let trait_ref =
979+
crate::next_solver::TraitRef::new(table.interner, fn_x_trait.into(), args);
980+
if !table
981+
.try_obligation(crate::next_solver::Predicate::upcast_from(
982+
trait_ref,
983+
table.interner,
984+
))
985+
.no_solution()
986+
{
987+
let ret_ty = table.resolve_completely(return_ty.to_chalk(table.interner));
988+
let args_ty = table.resolve_completely(args_ty.to_chalk(table.interner));
980989
let params = args_ty
981990
.as_tuple()?
982991
.iter(Interner)

crates/hir-ty/src/lower_nextsolver.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,18 @@ pub(crate) fn generic_predicates_for_param_cycle_result(
13551355
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
13561356
pub struct GenericPredicates<'db>(Option<Arc<[Clause<'db>]>>);
13571357

1358+
impl<'db> GenericPredicates<'db> {
1359+
pub fn instantiate(
1360+
&self,
1361+
interner: DbInterner<'db>,
1362+
args: GenericArgs<'db>,
1363+
) -> Option<impl Iterator<Item = Clause<'db>>> {
1364+
self.0
1365+
.as_ref()
1366+
.map(|it| EarlyBinder::bind(it.iter().copied()).iter_instantiated(interner, args))
1367+
}
1368+
}
1369+
13581370
impl<'db> ops::Deref for GenericPredicates<'db> {
13591371
type Target = [Clause<'db>];
13601372

0 commit comments

Comments
 (0)