Skip to content

Commit 3089d23

Browse files
committed
Remove lower::ty in favor of lower_nextsolver::ty
1 parent f6cf303 commit 3089d23

File tree

14 files changed

+112
-92
lines changed

14 files changed

+112
-92
lines changed

crates/hir-ty/src/builder.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
use chalk_ir::{
44
AdtId, DebruijnIndex, Scalar,
55
cast::{Cast, CastTo, Caster},
6-
fold::TypeFoldable,
7-
interner::HasInterner,
86
};
97
use hir_def::{GenericDefId, GenericParamId, TraitId, TypeAliasId, builtin_type::BuiltinType};
108
use smallvec::SmallVec;
119

1210
use crate::{
13-
Binders, BoundVar, CallableSig, GenericArg, GenericArgData, Interner, ProjectionTy,
14-
Substitution, TraitRef, Ty, TyDefId, TyExt, TyKind, consteval::unknown_const_as_generic,
15-
db::HirDatabase, error_lifetime, generics::generics, infer::unify::InferenceTable, primitive,
16-
to_assoc_type_id, to_chalk_trait_id,
11+
BoundVar, CallableSig, GenericArg, GenericArgData, Interner, ProjectionTy, Substitution,
12+
TraitRef, Ty, TyDefId, TyExt, TyKind,
13+
consteval::unknown_const_as_generic,
14+
db::HirDatabase,
15+
error_lifetime,
16+
generics::generics,
17+
infer::unify::InferenceTable,
18+
next_solver::{DbInterner, EarlyBinder, mapping::ChalkToNextSolver},
19+
primitive, to_assoc_type_id, to_chalk_trait_id,
1720
};
1821

1922
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -345,19 +348,20 @@ impl TyBuilder<TypeAliasId> {
345348
}
346349
}
347350

348-
impl<T: HasInterner<Interner = Interner> + TypeFoldable<Interner>> TyBuilder<Binders<T>> {
349-
pub fn build(self) -> T {
351+
impl<'db, T: rustc_type_ir::TypeFoldable<DbInterner<'db>>> TyBuilder<EarlyBinder<'db, T>> {
352+
pub fn build(self, interner: DbInterner<'db>) -> T {
350353
let (b, subst) = self.build_internal();
351-
b.substitute(Interner, &subst)
354+
let args: crate::next_solver::GenericArgs<'db> = subst.to_nextsolver(interner);
355+
b.instantiate(interner, args)
352356
}
353357
}
354358

355-
impl TyBuilder<Binders<Ty>> {
359+
impl<'db> TyBuilder<EarlyBinder<'db, crate::next_solver::Ty<'db>>> {
356360
pub fn def_ty(
357-
db: &dyn HirDatabase,
361+
db: &'db dyn HirDatabase,
358362
def: TyDefId,
359363
parent_subst: Option<Substitution>,
360-
) -> TyBuilder<Binders<Ty>> {
364+
) -> TyBuilder<EarlyBinder<'db, crate::next_solver::Ty<'db>>> {
361365
let poly_ty = db.ty(def);
362366
let id: GenericDefId = match def {
363367
TyDefId::BuiltinType(_) => {
@@ -370,7 +374,10 @@ impl TyBuilder<Binders<Ty>> {
370374
TyBuilder::subst_for_def(db, id, parent_subst).with_data(poly_ty)
371375
}
372376

373-
pub fn impl_self_ty(db: &dyn HirDatabase, def: hir_def::ImplId) -> TyBuilder<Binders<Ty>> {
374-
TyBuilder::subst_for_def(db, def, None).with_data(db.impl_self_ty(def))
377+
pub fn impl_self_ty(
378+
db: &'db dyn HirDatabase,
379+
def: hir_def::ImplId,
380+
) -> TyBuilder<EarlyBinder<'db, crate::next_solver::Ty<'db>>> {
381+
TyBuilder::subst_for_def(db, def, None).with_data(db.impl_self_ty_ns(def))
375382
}
376383
}

crates/hir-ty/src/db.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,12 @@ pub trait HirDatabase: DefDatabase + std::fmt::Debug {
114114
#[salsa::invoke(crate::dyn_compatibility::dyn_compatibility_of_trait_query)]
115115
fn dyn_compatibility_of_trait(&self, trait_: TraitId) -> Option<DynCompatibilityViolation>;
116116

117-
#[salsa::invoke(crate::lower::ty_query)]
117+
#[salsa::invoke(crate::lower_nextsolver::ty_query)]
118118
#[salsa::transparent]
119-
fn ty(&self, def: TyDefId) -> Binders<Ty>;
119+
fn ty<'db>(
120+
&'db self,
121+
def: TyDefId,
122+
) -> crate::next_solver::EarlyBinder<'db, crate::next_solver::Ty<'db>>;
120123

121124
#[salsa::invoke(crate::lower::type_for_type_alias_with_diagnostics_query)]
122125
#[salsa::cycle(cycle_result = crate::lower::type_for_type_alias_with_diagnostics_cycle_result)]
@@ -277,13 +280,6 @@ pub trait HirDatabase: DefDatabase + std::fmt::Debug {
277280

278281
// next trait solver
279282

280-
#[salsa::invoke(crate::lower_nextsolver::ty_query)]
281-
#[salsa::transparent]
282-
fn ty_ns<'db>(
283-
&'db self,
284-
def: TyDefId,
285-
) -> crate::next_solver::EarlyBinder<'db, crate::next_solver::Ty<'db>>;
286-
287283
/// Returns the type of the value of the given constant, or `None` if the `ValueTyDefId` is
288284
/// a `StructId` or `EnumVariantId` with a record constructor.
289285
#[salsa::invoke(crate::lower_nextsolver::value_ty_query)]

crates/hir-ty/src/infer.rs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,7 @@ impl<'db> InferenceContext<'db> {
17081708
LifetimeElisionKind::Infer,
17091709
);
17101710
let mut path_ctx = ctx.at_path(path, node);
1711+
let interner = DbInterner::conjure();
17111712
let (resolution, unresolved) = if value_ns {
17121713
let Some(res) = path_ctx.resolve_path_in_value_ns(HygieneId::ROOT) else {
17131714
return (self.err_ty(), None);
@@ -1717,15 +1718,27 @@ impl<'db> InferenceContext<'db> {
17171718
ValueNs::EnumVariantId(var) => {
17181719
let substs = path_ctx.substs_from_path(var.into(), true, false);
17191720
drop(ctx);
1720-
let ty = self.db.ty(var.lookup(self.db).parent.into());
1721-
let ty = self.insert_type_vars(ty.substitute(Interner, &substs));
1721+
let args: crate::next_solver::GenericArgs<'_> =
1722+
substs.to_nextsolver(interner);
1723+
let ty = self
1724+
.db
1725+
.ty(var.lookup(self.db).parent.into())
1726+
.instantiate(interner, args)
1727+
.to_chalk(interner);
1728+
let ty = self.insert_type_vars(ty);
17221729
return (ty, Some(var.into()));
17231730
}
17241731
ValueNs::StructId(strukt) => {
17251732
let substs = path_ctx.substs_from_path(strukt.into(), true, false);
17261733
drop(ctx);
1727-
let ty = self.db.ty(strukt.into());
1728-
let ty = self.insert_type_vars(ty.substitute(Interner, &substs));
1734+
let args: crate::next_solver::GenericArgs<'_> =
1735+
substs.to_nextsolver(interner);
1736+
let ty = self
1737+
.db
1738+
.ty(strukt.into())
1739+
.instantiate(interner, args)
1740+
.to_chalk(interner);
1741+
let ty = self.insert_type_vars(ty);
17291742
return (ty, Some(strukt.into()));
17301743
}
17311744
ValueNs::ImplSelf(impl_id) => (TypeNs::SelfType(impl_id), None),
@@ -1746,22 +1759,29 @@ impl<'db> InferenceContext<'db> {
17461759
TypeNs::AdtId(AdtId::StructId(strukt)) => {
17471760
let substs = path_ctx.substs_from_path(strukt.into(), true, false);
17481761
drop(ctx);
1749-
let ty = self.db.ty(strukt.into());
1750-
let ty = self.insert_type_vars(ty.substitute(Interner, &substs));
1762+
let args: crate::next_solver::GenericArgs<'_> = substs.to_nextsolver(interner);
1763+
let ty = self.db.ty(strukt.into()).instantiate(interner, args).to_chalk(interner);
1764+
let ty = self.insert_type_vars(ty);
17511765
forbid_unresolved_segments((ty, Some(strukt.into())), unresolved)
17521766
}
17531767
TypeNs::AdtId(AdtId::UnionId(u)) => {
17541768
let substs = path_ctx.substs_from_path(u.into(), true, false);
17551769
drop(ctx);
1756-
let ty = self.db.ty(u.into());
1757-
let ty = self.insert_type_vars(ty.substitute(Interner, &substs));
1770+
let args: crate::next_solver::GenericArgs<'_> = substs.to_nextsolver(interner);
1771+
let ty = self.db.ty(u.into()).instantiate(interner, args).to_chalk(interner);
1772+
let ty = self.insert_type_vars(ty);
17581773
forbid_unresolved_segments((ty, Some(u.into())), unresolved)
17591774
}
17601775
TypeNs::EnumVariantId(var) => {
17611776
let substs = path_ctx.substs_from_path(var.into(), true, false);
17621777
drop(ctx);
1763-
let ty = self.db.ty(var.lookup(self.db).parent.into());
1764-
let ty = self.insert_type_vars(ty.substitute(Interner, &substs));
1778+
let args: crate::next_solver::GenericArgs<'_> = substs.to_nextsolver(interner);
1779+
let ty = self
1780+
.db
1781+
.ty(var.lookup(self.db).parent.into())
1782+
.instantiate(interner, args)
1783+
.to_chalk(interner);
1784+
let ty = self.insert_type_vars(ty);
17651785
forbid_unresolved_segments((ty, Some(var.into())), unresolved)
17661786
}
17671787
TypeNs::SelfType(impl_id) => {
@@ -1844,8 +1864,10 @@ impl<'db> InferenceContext<'db> {
18441864
};
18451865
let substs = path_ctx.substs_from_path_segment(it.into(), true, None, false);
18461866
drop(ctx);
1847-
let ty = self.db.ty(it.into());
1848-
let ty = self.insert_type_vars(ty.substitute(Interner, &substs));
1867+
let interner = DbInterner::conjure();
1868+
let args: crate::next_solver::GenericArgs<'_> = substs.to_nextsolver(interner);
1869+
let ty = self.db.ty(it.into()).instantiate(interner, args).to_chalk(interner);
1870+
let ty = self.insert_type_vars(ty);
18491871

18501872
self.resolve_variant_on_alias(ty, unresolved, mod_path)
18511873
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ impl<'db> InferenceTable<'db> {
780780
}
781781

782782
pub(crate) fn structurally_resolve_type(&mut self, ty: &Ty) -> Ty {
783-
if let TyKind::Alias(..) = ty.kind(Interner) {
783+
if let TyKind::Alias(chalk_ir::AliasTy::Projection(..)) = ty.kind(Interner) {
784784
self.structurally_normalize_ty(ty)
785785
} else {
786786
self.resolve_vars_with_obligations(ty.to_nextsolver(self.interner))

crates/hir-ty/src/layout/tests.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
use base_db::target::TargetData;
2-
use chalk_ir::{AdtId, TyKind};
32
use either::Either;
43
use hir_def::db::DefDatabase;
54
use project_model::{Sysroot, toolchain_info::QueryConfig};
65
use rustc_hash::FxHashMap;
6+
use rustc_type_ir::inherent::{GenericArgs as _, Ty as _};
77
use syntax::ToSmolStr;
88
use test_fixture::WithFixture;
99
use triomphe::Arc;
1010

1111
use crate::{
12-
Interner, Substitution,
1312
db::HirDatabase,
1413
layout::{Layout, LayoutError},
15-
next_solver::{DbInterner, mapping::ChalkToNextSolver},
14+
next_solver::{AdtDef, DbInterner, GenericArgs, mapping::ChalkToNextSolver},
1615
setup_tracing,
1716
test_db::TestDB,
1817
};
@@ -80,18 +79,18 @@ fn eval_goal(
8079
Some(adt_or_type_alias_id)
8180
})
8281
.unwrap();
83-
let goal_ty = match adt_or_type_alias_id {
84-
Either::Left(adt_id) => {
85-
TyKind::Adt(AdtId(adt_id), Substitution::empty(Interner)).intern(Interner)
86-
}
87-
Either::Right(ty_id) => {
88-
db.ty(ty_id.into()).substitute(Interner, &Substitution::empty(Interner))
89-
}
90-
};
9182
salsa::attach(&db, || {
9283
let interner = DbInterner::new_with(&db, None, None);
84+
let goal_ty = match adt_or_type_alias_id {
85+
Either::Left(adt_id) => crate::next_solver::Ty::new_adt(
86+
interner,
87+
AdtDef::new(adt_id, interner),
88+
GenericArgs::identity_for_item(interner, adt_id.into()),
89+
),
90+
Either::Right(ty_id) => db.ty(ty_id.into()).instantiate_identity(),
91+
};
9392
db.layout_of_ty(
94-
goal_ty.to_nextsolver(interner),
93+
goal_ty,
9594
db.trait_environment(match adt_or_type_alias_id {
9695
Either::Left(adt) => hir_def::GenericDefId::AdtId(adt),
9796
Either::Right(ty) => hir_def::GenericDefId::TypeAliasId(ty),

crates/hir-ty/src/lower.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,16 +1458,6 @@ fn type_for_enum_variant_constructor(
14581458
}
14591459
}
14601460

1461-
#[salsa_macros::tracked(cycle_result = type_for_adt_cycle_result)]
1462-
fn type_for_adt_tracked(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> {
1463-
type_for_adt(db, adt)
1464-
}
1465-
1466-
fn type_for_adt_cycle_result(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> {
1467-
let generics = generics(db, adt.into());
1468-
make_binders(db, &generics, TyKind::Error.intern(Interner))
1469-
}
1470-
14711461
fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> {
14721462
let generics = generics(db, adt.into());
14731463
let subst = generics.bound_vars_subst(db, DebruijnIndex::INNERMOST);
@@ -1547,18 +1537,6 @@ impl ValueTyDefId {
15471537
}
15481538
}
15491539

1550-
/// Build the declared type of an item. This depends on the namespace; e.g. for
1551-
/// `struct Foo(usize)`, we have two types: The type of the struct itself, and
1552-
/// the constructor function `(usize) -> Foo` which lives in the values
1553-
/// namespace.
1554-
pub(crate) fn ty_query(db: &dyn HirDatabase, def: TyDefId) -> Binders<Ty> {
1555-
match def {
1556-
TyDefId::BuiltinType(it) => Binders::empty(Interner, TyBuilder::builtin(it)),
1557-
TyDefId::AdtId(it) => type_for_adt_tracked(db, it),
1558-
TyDefId::TypeAliasId(it) => db.type_for_type_alias_with_diagnostics(it).0,
1559-
}
1560-
}
1561-
15621540
pub(crate) fn value_ty_query(db: &dyn HirDatabase, def: ValueTyDefId) -> Option<Binders<Ty>> {
15631541
match def {
15641542
ValueTyDefId::FunctionId(it) => Some(type_for_fn(db, it)),

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ use crate::{
2828
error_lifetime,
2929
generics::{Generics, generics},
3030
lower::{LifetimeElisionKind, named_associated_type_shorthand_candidates},
31+
next_solver::{
32+
DbInterner,
33+
mapping::{ChalkToNextSolver, NextSolverToChalk},
34+
},
3135
static_lifetime, to_assoc_type_id, to_chalk_trait_id, to_placeholder_idx,
3236
utils::associated_type_by_name_including_super_traits,
3337
};
@@ -256,7 +260,8 @@ impl<'a, 'b> PathLoweringContext<'a, 'b> {
256260
}
257261
ParamLoweringMode::Variable => TyBuilder::impl_self_ty(self.ctx.db, impl_id)
258262
.fill_with_bound_vars(self.ctx.in_binders, 0)
259-
.build(),
263+
.build(DbInterner::conjure())
264+
.to_chalk(DbInterner::conjure()),
260265
}
261266
}
262267
TypeNs::AdtSelfType(adt) => {
@@ -267,7 +272,9 @@ impl<'a, 'b> PathLoweringContext<'a, 'b> {
267272
generics.bound_vars_subst(self.ctx.db, self.ctx.in_binders)
268273
}
269274
};
270-
self.ctx.db.ty(adt.into()).substitute(Interner, &substs)
275+
let interner = DbInterner::conjure();
276+
let args: crate::next_solver::GenericArgs<'_> = substs.to_nextsolver(interner);
277+
self.ctx.db.ty(adt.into()).instantiate(interner, args).to_chalk(interner)
271278
}
272279

273280
TypeNs::AdtId(it) => self.lower_path_inner(it.into(), infer_args),
@@ -537,7 +544,9 @@ impl<'a, 'b> PathLoweringContext<'a, 'b> {
537544
TyDefId::TypeAliasId(it) => it.into(),
538545
};
539546
let substs = self.substs_from_path_segment(generic_def, infer_args, None, false);
540-
self.ctx.db.ty(typeable).substitute(Interner, &substs)
547+
let interner = DbInterner::conjure();
548+
let args: crate::next_solver::GenericArgs<'_> = substs.to_nextsolver(interner);
549+
self.ctx.db.ty(typeable).instantiate(interner, args).to_chalk(interner)
541550
}
542551

543552
/// Collect generic arguments from a path into a `Substs`. See also

crates/hir-ty/src/method_resolution.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,8 +1697,10 @@ fn is_valid_impl_method_candidate(
16971697
return IsValidCandidate::NotVisible;
16981698
}
16991699
let self_ty_matches = table.run_in_snapshot(|table| {
1700-
let expected_self_ty =
1701-
TyBuilder::impl_self_ty(db, impl_id).fill_with_inference_vars(table).build();
1700+
let expected_self_ty = TyBuilder::impl_self_ty(db, impl_id)
1701+
.fill_with_inference_vars(table)
1702+
.build(DbInterner::conjure())
1703+
.to_chalk(DbInterner::conjure());
17021704
table.unify(&expected_self_ty, self_ty)
17031705
});
17041706
if !self_ty_matches {

crates/hir-ty/src/mir/eval/shim.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use hir_expand::name::Name;
1414
use intern::{Symbol, sym};
1515
use stdx::never;
1616

17+
use crate::next_solver::mapping::NextSolverToChalk;
1718
use crate::{
1819
DropGlue,
1920
display::DisplayTarget,
@@ -1371,9 +1372,8 @@ impl Evaluator<'_> {
13711372
result = (l as i8).cmp(&(r as i8));
13721373
}
13731374
if let Some(e) = LangItem::Ordering.resolve_enum(self.db, self.crate_id) {
1374-
let ty = self.db.ty(e.into());
1375-
let r = self
1376-
.compute_discriminant(ty.skip_binders().clone(), &[result as i8 as u8])?;
1375+
let ty = self.db.ty(e.into()).skip_binder().to_chalk(interner);
1376+
let r = self.compute_discriminant(ty.clone(), &[result as i8 as u8])?;
13771377
destination.write_from_bytes(self, &r.to_le_bytes()[0..destination.size])?;
13781378
Ok(())
13791379
} else {

crates/hir-ty/src/next_solver/interner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,9 +1091,9 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
10911091
ItemContainerId::ImplId(it) => it,
10921092
_ => panic!("assoc ty value should be in impl"),
10931093
};
1094-
self.db().ty_ns(id.into())
1094+
self.db().ty(id.into())
10951095
}
1096-
SolverDefId::AdtId(id) => self.db().ty_ns(id.into()),
1096+
SolverDefId::AdtId(id) => self.db().ty(id.into()),
10971097
// FIXME(next-solver): This uses the types of `query mir_borrowck` in rustc.
10981098
//
10991099
// We currently always use the type from HIR typeck which ignores regions. This

0 commit comments

Comments
 (0)