Skip to content

Commit 73a4013

Browse files
committed
Remove lower::value_ty in favor of lower_nextsolver::value_ty
1 parent 3089d23 commit 73a4013

File tree

8 files changed

+61
-134
lines changed

8 files changed

+61
-134
lines changed

crates/hir-ty/src/db.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,11 @@ pub trait HirDatabase: DefDatabase + std::fmt::Debug {
127127

128128
/// Returns the type of the value of the given constant, or `None` if the `ValueTyDefId` is
129129
/// a `StructId` or `EnumVariantId` with a record constructor.
130-
#[salsa::invoke(crate::lower::value_ty_query)]
131-
fn value_ty(&self, def: ValueTyDefId) -> Option<Binders<Ty>>;
130+
#[salsa::invoke(crate::lower_nextsolver::value_ty_query)]
131+
fn value_ty<'db>(
132+
&'db self,
133+
def: ValueTyDefId,
134+
) -> Option<crate::next_solver::EarlyBinder<'db, crate::next_solver::Ty<'db>>>;
132135

133136
#[salsa::invoke(crate::lower::impl_self_ty_with_diagnostics_query)]
134137
#[salsa::cycle(cycle_result = crate::lower::impl_self_ty_with_diagnostics_cycle_result)]
@@ -280,14 +283,6 @@ pub trait HirDatabase: DefDatabase + std::fmt::Debug {
280283

281284
// next trait solver
282285

283-
/// Returns the type of the value of the given constant, or `None` if the `ValueTyDefId` is
284-
/// a `StructId` or `EnumVariantId` with a record constructor.
285-
#[salsa::invoke(crate::lower_nextsolver::value_ty_query)]
286-
fn value_ty_ns<'db>(
287-
&'db self,
288-
def: ValueTyDefId,
289-
) -> Option<crate::next_solver::EarlyBinder<'db, crate::next_solver::Ty<'db>>>;
290-
291286
#[salsa::invoke(crate::lower_nextsolver::type_for_type_alias_with_diagnostics_query)]
292287
#[salsa::cycle(cycle_result = crate::lower_nextsolver::type_for_type_alias_with_diagnostics_cycle_result)]
293288
fn type_for_type_alias_with_diagnostics_ns<'db>(

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

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ use syntax::ast::RangeOp;
2323
use tracing::debug;
2424

2525
use crate::autoderef::overloaded_deref_ty;
26-
use crate::next_solver::ErrorGuaranteed;
2726
use crate::next_solver::infer::DefineOpaqueTypes;
2827
use crate::next_solver::obligation_ctxt::ObligationCtxt;
28+
use crate::next_solver::{DbInterner, ErrorGuaranteed};
2929
use crate::{
30-
Adjust, Adjustment, AdtId, AutoBorrow, Binders, CallableDefId, CallableSig, DeclContext,
31-
DeclOrigin, IncorrectGenericsLenKind, Interner, LifetimeElisionKind, Rawness, Scalar,
32-
Substitution, TraitEnvironment, TraitRef, Ty, TyBuilder, TyExt, TyKind, consteval,
30+
Adjust, Adjustment, AdtId, AutoBorrow, CallableDefId, CallableSig, DeclContext, DeclOrigin,
31+
IncorrectGenericsLenKind, Interner, LifetimeElisionKind, Rawness, Scalar, Substitution,
32+
TraitEnvironment, TraitRef, Ty, TyBuilder, TyExt, TyKind, consteval,
3333
generics::generics,
3434
infer::{
3535
AllowTwoPhase, BreakableKind,
@@ -1481,7 +1481,10 @@ impl<'db> InferenceContext<'db> {
14811481

14821482
self.write_method_resolution(tgt_expr, func, subst.clone());
14831483

1484-
let method_ty = self.db.value_ty(func.into()).unwrap().substitute(Interner, &subst);
1484+
let interner = DbInterner::new_with(self.db, None, None);
1485+
let args: crate::next_solver::GenericArgs<'_> = subst.to_nextsolver(interner);
1486+
let method_ty =
1487+
self.db.value_ty(func.into()).unwrap().instantiate(interner, args).to_chalk(interner);
14851488
self.register_obligations_for_call(&method_ty);
14861489

14871490
self.infer_expr_coerce(rhs, &Expectation::has_type(rhs_ty.clone()), ExprIsRead::Yes);
@@ -1800,11 +1803,17 @@ impl<'db> InferenceContext<'db> {
18001803
self.write_expr_adj(receiver, adjustments.into_boxed_slice());
18011804
self.write_method_resolution(tgt_expr, func, substs.clone());
18021805

1806+
let interner = DbInterner::new_with(self.db, None, None);
1807+
let args: crate::next_solver::GenericArgs<'_> =
1808+
substs.to_nextsolver(interner);
18031809
self.check_method_call(
18041810
tgt_expr,
18051811
&[],
1806-
self.db.value_ty(func.into()).unwrap(),
1807-
substs,
1812+
self.db
1813+
.value_ty(func.into())
1814+
.unwrap()
1815+
.instantiate(interner, args)
1816+
.to_chalk(interner),
18081817
ty,
18091818
expected,
18101819
)
@@ -1963,11 +1972,16 @@ impl<'db> InferenceContext<'db> {
19631972

19641973
let substs = self.substs_for_method_call(tgt_expr, func.into(), generic_args);
19651974
self.write_method_resolution(tgt_expr, func, substs.clone());
1975+
let interner = DbInterner::new_with(self.db, None, None);
1976+
let gen_args: crate::next_solver::GenericArgs<'_> = substs.to_nextsolver(interner);
19661977
self.check_method_call(
19671978
tgt_expr,
19681979
args,
1969-
self.db.value_ty(func.into()).expect("we have a function def"),
1970-
substs,
1980+
self.db
1981+
.value_ty(func.into())
1982+
.expect("we have a function def")
1983+
.instantiate(interner, gen_args)
1984+
.to_chalk(interner),
19711985
ty,
19721986
expected,
19731987
)
@@ -2012,11 +2026,15 @@ impl<'db> InferenceContext<'db> {
20122026
let recovered = match assoc_func_with_same_name {
20132027
Some(f) => {
20142028
let substs = self.substs_for_method_call(tgt_expr, f.into(), generic_args);
2029+
let interner = DbInterner::new_with(self.db, None, None);
2030+
let args: crate::next_solver::GenericArgs<'_> =
2031+
substs.to_nextsolver(interner);
20152032
let f = self
20162033
.db
20172034
.value_ty(f.into())
20182035
.expect("we have a function def")
2019-
.substitute(Interner, &substs);
2036+
.instantiate(interner, args)
2037+
.to_chalk(interner);
20202038
let sig = f.callable_sig(self.db).expect("we have a function def");
20212039
Some((f, sig, true))
20222040
}
@@ -2056,12 +2074,10 @@ impl<'db> InferenceContext<'db> {
20562074
&mut self,
20572075
tgt_expr: ExprId,
20582076
args: &[ExprId],
2059-
method_ty: Binders<Ty>,
2060-
substs: Substitution,
2077+
method_ty: Ty,
20612078
receiver_ty: Ty,
20622079
expected: &Expectation,
20632080
) -> Ty {
2064-
let method_ty = method_ty.substitute(Interner, &substs);
20652081
self.register_obligations_for_call(&method_ty);
20662082
let interner = self.table.interner;
20672083
let ((formal_receiver_ty, param_tys), ret_ty, is_varargs) =

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ use crate::{
1717
generics::generics,
1818
infer::diagnostics::InferenceTyLoweringContext as TyLoweringContext,
1919
method_resolution::{self, VisibleFromModule},
20-
next_solver::mapping::ChalkToNextSolver,
20+
next_solver::{
21+
DbInterner,
22+
mapping::{ChalkToNextSolver, NextSolverToChalk},
23+
},
2124
to_chalk_trait_id,
2225
};
2326

@@ -36,7 +39,9 @@ impl<'db> InferenceContext<'db> {
3639

3740
self.add_required_obligations_for_value_path(generic_def, &substs);
3841

39-
let ty = self.db.value_ty(value_def)?.substitute(Interner, &substs);
42+
let interner = DbInterner::new_with(self.db, None, None);
43+
let args: crate::next_solver::GenericArgs<'_> = substs.to_nextsolver(interner);
44+
let ty = self.db.value_ty(value_def)?.instantiate(interner, args).to_chalk(interner);
4045
let ty = self.process_remote_user_written_ty(ty);
4146
Some(ty)
4247
}
@@ -89,9 +94,9 @@ impl<'db> InferenceContext<'db> {
8994

9095
let generic_def = value_def.to_generic_def_id(self.db);
9196
if let GenericDefId::StaticId(_) = generic_def {
97+
let interner = DbInterner::new_with(self.db, None, None);
9298
// `Static` is the kind of item that can never be generic currently. We can just skip the binders to get its type.
93-
let (ty, binders) = self.db.value_ty(value_def)?.into_value_and_skipped_binders();
94-
stdx::always!(binders.is_empty(Interner), "non-empty binders for non-generic def",);
99+
let ty = self.db.value_ty(value_def)?.skip_binder().to_chalk(interner);
95100
return Some(ValuePathResolution::NonGeneric(ty));
96101
};
97102

crates/hir-ty/src/lower.rs

Lines changed: 1 addition & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ use hir_def::{
3030
builtin_type::BuiltinType,
3131
expr_store::{ExpressionStore, path::Path},
3232
hir::generics::{GenericParamDataRef, TypeOrConstParamData, WherePredicate},
33-
item_tree::FieldsShape,
3433
lang_item::LangItem,
3534
resolver::{HasResolver, LifetimeNs, Resolver, TypeNs},
3635
signatures::{FunctionSignature, TraitFlags, TypeAliasFlags},
@@ -59,7 +58,7 @@ use crate::{
5958
path::{PathDiagnosticCallback, PathLoweringContext},
6059
},
6160
make_binders,
62-
mapping::{ToChalk, from_chalk_trait_id, lt_to_placeholder_idx},
61+
mapping::{from_chalk_trait_id, lt_to_placeholder_idx},
6362
static_lifetime, to_chalk_trait_id, to_placeholder_idx,
6463
utils::all_super_trait_refs,
6564
variable_kinds_from_iter,
@@ -1352,51 +1351,6 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig {
13521351
make_binders(db, &generics, sig)
13531352
}
13541353

1355-
/// Build the declared type of a function. This should not need to look at the
1356-
/// function body.
1357-
fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> {
1358-
let generics = generics(db, def.into());
1359-
let substs = generics.bound_vars_subst(db, DebruijnIndex::INNERMOST);
1360-
make_binders(
1361-
db,
1362-
&generics,
1363-
TyKind::FnDef(CallableDefId::FunctionId(def).to_chalk(db), substs).intern(Interner),
1364-
)
1365-
}
1366-
1367-
/// Build the declared type of a const.
1368-
fn type_for_const(db: &dyn HirDatabase, def: ConstId) -> Binders<Ty> {
1369-
let data = db.const_signature(def);
1370-
let generics = generics(db, def.into());
1371-
let resolver = def.resolver(db);
1372-
let parent = def.loc(db).container;
1373-
let mut ctx = TyLoweringContext::new(
1374-
db,
1375-
&resolver,
1376-
&data.store,
1377-
def.into(),
1378-
LifetimeElisionKind::for_const(parent),
1379-
)
1380-
.with_type_param_mode(ParamLoweringMode::Variable);
1381-
1382-
make_binders(db, &generics, ctx.lower_ty(data.type_ref))
1383-
}
1384-
1385-
/// Build the declared type of a static.
1386-
fn type_for_static(db: &dyn HirDatabase, def: StaticId) -> Binders<Ty> {
1387-
let data = db.static_signature(def);
1388-
let resolver = def.resolver(db);
1389-
let mut ctx = TyLoweringContext::new(
1390-
db,
1391-
&resolver,
1392-
&data.store,
1393-
def.into(),
1394-
LifetimeElisionKind::Elided(static_lifetime()),
1395-
);
1396-
1397-
Binders::empty(Interner, ctx.lower_ty(data.type_ref))
1398-
}
1399-
14001354
fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnSig {
14011355
let field_tys = db.field_types(def.into());
14021356
let params = field_tys.iter().map(|(_, ty)| ty.skip_binders().clone());
@@ -1407,24 +1361,6 @@ fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnS
14071361
)
14081362
}
14091363

1410-
/// Build the type of a tuple struct constructor.
1411-
fn type_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> Option<Binders<Ty>> {
1412-
let struct_data = def.fields(db);
1413-
match struct_data.shape {
1414-
FieldsShape::Record => None,
1415-
FieldsShape::Unit => Some(type_for_adt(db, def.into())),
1416-
FieldsShape::Tuple => {
1417-
let generics = generics(db, AdtId::from(def).into());
1418-
let substs = generics.bound_vars_subst(db, DebruijnIndex::INNERMOST);
1419-
Some(make_binders(
1420-
db,
1421-
&generics,
1422-
TyKind::FnDef(CallableDefId::StructId(def).to_chalk(db), substs).intern(Interner),
1423-
))
1424-
}
1425-
}
1426-
}
1427-
14281364
fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -> PolyFnSig {
14291365
let field_tys = db.field_types(def.into());
14301366
let params = field_tys.iter().map(|(_, ty)| ty.skip_binders().clone());
@@ -1436,28 +1372,6 @@ fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId)
14361372
)
14371373
}
14381374

1439-
/// Build the type of a tuple enum variant constructor.
1440-
fn type_for_enum_variant_constructor(
1441-
db: &dyn HirDatabase,
1442-
def: EnumVariantId,
1443-
) -> Option<Binders<Ty>> {
1444-
let e = def.lookup(db).parent;
1445-
match def.fields(db).shape {
1446-
FieldsShape::Record => None,
1447-
FieldsShape::Unit => Some(type_for_adt(db, e.into())),
1448-
FieldsShape::Tuple => {
1449-
let generics = generics(db, e.into());
1450-
let substs = generics.bound_vars_subst(db, DebruijnIndex::INNERMOST);
1451-
Some(make_binders(
1452-
db,
1453-
&generics,
1454-
TyKind::FnDef(CallableDefId::EnumVariantId(def).to_chalk(db), substs)
1455-
.intern(Interner),
1456-
))
1457-
}
1458-
}
1459-
}
1460-
14611375
fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> {
14621376
let generics = generics(db, adt.into());
14631377
let subst = generics.bound_vars_subst(db, DebruijnIndex::INNERMOST);
@@ -1537,17 +1451,6 @@ impl ValueTyDefId {
15371451
}
15381452
}
15391453

1540-
pub(crate) fn value_ty_query(db: &dyn HirDatabase, def: ValueTyDefId) -> Option<Binders<Ty>> {
1541-
match def {
1542-
ValueTyDefId::FunctionId(it) => Some(type_for_fn(db, it)),
1543-
ValueTyDefId::StructId(it) => type_for_struct_constructor(db, it),
1544-
ValueTyDefId::UnionId(it) => Some(type_for_adt(db, it.into())),
1545-
ValueTyDefId::EnumVariantId(it) => type_for_enum_variant_constructor(db, it),
1546-
ValueTyDefId::ConstId(it) => Some(type_for_const(db, it)),
1547-
ValueTyDefId::StaticId(it) => Some(type_for_static(db, it)),
1548-
}
1549-
}
1550-
15511454
pub(crate) fn impl_self_ty_query(db: &dyn HirDatabase, impl_id: ImplId) -> Binders<Ty> {
15521455
db.impl_self_ty_with_diagnostics(impl_id).0
15531456
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,15 +1099,13 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
10991099
// We currently always use the type from HIR typeck which ignores regions. This
11001100
// should be fine.
11011101
SolverDefId::InternedOpaqueTyId(_) => self.type_of_opaque_hir_typeck(def_id),
1102-
SolverDefId::FunctionId(id) => self.db.value_ty_ns(id.into()).unwrap(),
1102+
SolverDefId::FunctionId(id) => self.db.value_ty(id.into()).unwrap(),
11031103
SolverDefId::Ctor(id) => {
11041104
let id = match id {
11051105
Ctor::Struct(id) => id.into(),
11061106
Ctor::Enum(id) => id.into(),
11071107
};
1108-
self.db
1109-
.value_ty_ns(id)
1110-
.expect("`SolverDefId::Ctor` should have a function-like ctor")
1108+
self.db.value_ty(id).expect("`SolverDefId::Ctor` should have a function-like ctor")
11111109
}
11121110
_ => panic!("Unexpected def_id `{def_id:?}` provided for `type_of`"),
11131111
}

crates/hir/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4872,6 +4872,7 @@ impl<'db> Type<'db> {
48724872
let Some(ty) = db.value_ty(def.into()) else {
48734873
return Type::new(db, def, TyKind::Error.intern(Interner));
48744874
};
4875+
let interner = DbInterner::new_with(db, None, None);
48754876
let substs = TyBuilder::unknown_subst(
48764877
db,
48774878
match def.into() {
@@ -4882,10 +4883,13 @@ impl<'db> Type<'db> {
48824883
ValueTyDefId::EnumVariantId(it) => {
48834884
GenericDefId::AdtId(AdtId::EnumId(it.lookup(db).parent))
48844885
}
4885-
ValueTyDefId::StaticId(_) => return Type::new(db, def, ty.skip_binders().clone()),
4886+
ValueTyDefId::StaticId(_) => {
4887+
return Type::new(db, def, ty.skip_binder().to_chalk(interner));
4888+
}
48864889
},
48874890
);
4888-
Type::new(db, def, ty.substitute(Interner, &substs))
4891+
let args: crate::next_solver::GenericArgs<'_> = substs.to_nextsolver(interner);
4892+
Type::new(db, def, ty.instantiate(interner, args).to_chalk(interner))
48894893
}
48904894

48914895
pub fn new_slice(ty: Self) -> Self {

crates/hir/src/source_analyzer.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ use hir_ty::{
4646
from_assoc_type_id,
4747
lang_items::lang_items_for_bin_op,
4848
method_resolution,
49+
next_solver::{
50+
DbInterner,
51+
mapping::{ChalkToNextSolver, NextSolverToChalk},
52+
},
4953
};
5054
use intern::sym;
5155
use itertools::Itertools;
@@ -372,8 +376,10 @@ impl<'db> SourceAnalyzer<'db> {
372376
) -> Option<Callable<'db>> {
373377
let expr_id = self.expr_id(call.clone().into())?.as_expr()?;
374378
let (func, substs) = self.infer()?.method_resolution(expr_id)?;
375-
let ty = db.value_ty(func.into())?.substitute(Interner, &substs);
376-
let ty = Type::new_with_resolver(db, &self.resolver, ty);
379+
let interner = DbInterner::new_with(db, None, None);
380+
let args: hir_ty::next_solver::GenericArgs<'_> = substs.to_nextsolver(interner);
381+
let ty = db.value_ty(func.into())?.instantiate(interner, args);
382+
let ty = Type::new_with_resolver(db, &self.resolver, ty.to_chalk(interner));
377383
let mut res = ty.as_callable(db)?;
378384
res.is_bound_method = true;
379385
Some(res)

crates/ide-db/src/syntax_helpers/suggest_name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ mod tests {
473473
frange.range,
474474
"selection is not an expression(yet contained in one)"
475475
);
476-
let name = NameGenerator::default().for_variable(&expr, &sema);
476+
let name = salsa::attach(sema.db, || NameGenerator::default().for_variable(&expr, &sema));
477477
assert_eq!(&name, expected);
478478
}
479479

0 commit comments

Comments
 (0)