Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/hir-ty/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,6 @@ impl<'db> TyBuilder<EarlyBinder<'db, crate::next_solver::Ty<'db>>> {
db: &'db dyn HirDatabase,
def: hir_def::ImplId,
) -> TyBuilder<EarlyBinder<'db, crate::next_solver::Ty<'db>>> {
TyBuilder::subst_for_def(db, def, None).with_data(db.impl_self_ty_ns(def))
TyBuilder::subst_for_def(db, def, None).with_data(db.impl_self_ty(def))
}
}
32 changes: 12 additions & 20 deletions crates/hir-ty/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use smallvec::SmallVec;
use triomphe::Arc;

use crate::{
Binders, Const, ImplTraitId, ImplTraits, InferenceResult, Substitution, TraitEnvironment,
TraitRef, Ty, TyDefId, ValueTyDefId, chalk_db,
Binders, Const, ImplTraitId, ImplTraits, InferenceResult, Substitution, TraitEnvironment, Ty,
TyDefId, ValueTyDefId, chalk_db,
consteval::ConstEvalError,
drop::DropGlue,
dyn_compatibility::DynCompatibilityViolation,
Expand Down Expand Up @@ -143,9 +143,12 @@ pub trait HirDatabase: DefDatabase + std::fmt::Debug {
def: ImplId,
) -> (crate::next_solver::EarlyBinder<'db, crate::next_solver::Ty<'db>>, Diagnostics);

#[salsa::invoke(crate::lower::impl_self_ty_query)]
#[salsa::invoke(crate::lower_nextsolver::impl_self_ty_query)]
#[salsa::transparent]
fn impl_self_ty(&self, def: ImplId) -> Binders<Ty>;
fn impl_self_ty<'db>(
&'db self,
def: ImplId,
) -> crate::next_solver::EarlyBinder<'db, crate::next_solver::Ty<'db>>;

// FIXME: Make this a non-interned query.
#[salsa::invoke_interned(crate::lower_nextsolver::const_param_ty_with_diagnostics_query)]
Expand All @@ -169,9 +172,12 @@ pub trait HirDatabase: DefDatabase + std::fmt::Debug {
Diagnostics,
)>;

#[salsa::invoke(crate::lower::impl_trait_query)]
#[salsa::invoke(crate::lower_nextsolver::impl_trait_query)]
#[salsa::transparent]
fn impl_trait(&self, def: ImplId) -> Option<Binders<TraitRef>>;
fn impl_trait<'db>(
&'db self,
def: ImplId,
) -> Option<crate::next_solver::EarlyBinder<'db, crate::next_solver::TraitRef<'db>>>;

#[salsa::invoke(crate::lower_nextsolver::field_types_with_diagnostics_query)]
fn field_types_with_diagnostics<'db>(
Expand Down Expand Up @@ -325,24 +331,10 @@ pub trait HirDatabase: DefDatabase + std::fmt::Debug {

// next trait solver

#[salsa::invoke(crate::lower_nextsolver::impl_self_ty_query)]
#[salsa::transparent]
fn impl_self_ty_ns<'db>(
&'db self,
def: ImplId,
) -> crate::next_solver::EarlyBinder<'db, crate::next_solver::Ty<'db>>;

#[salsa::invoke(crate::lower_nextsolver::const_param_ty_query)]
#[salsa::transparent]
fn const_param_ty_ns<'db>(&'db self, def: ConstParamId) -> crate::next_solver::Ty<'db>;

#[salsa::invoke(crate::lower_nextsolver::impl_trait_query)]
#[salsa::transparent]
fn impl_trait_ns<'db>(
&'db self,
def: ImplId,
) -> Option<crate::next_solver::EarlyBinder<'db, crate::next_solver::TraitRef<'db>>>;

#[salsa::invoke(crate::lower_nextsolver::field_types_query)]
#[salsa::transparent]
fn field_types_ns<'db>(
Expand Down
4 changes: 3 additions & 1 deletion crates/hir-ty/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,9 @@ impl<'db> InferenceContext<'db> {
TypeNs::SelfType(impl_id) => {
let generics = crate::generics::generics(self.db, impl_id.into());
let substs = generics.placeholder_subst(self.db);
let mut ty = self.db.impl_self_ty(impl_id).substitute(Interner, &substs);
let args: crate::next_solver::GenericArgs<'_> = substs.to_nextsolver(interner);
let mut ty =
self.db.impl_self_ty(impl_id).instantiate(interner, args).to_chalk(interner);

let Some(remaining_idx) = unresolved else {
drop(ctx);
Expand Down
10 changes: 8 additions & 2 deletions crates/hir-ty/src/infer/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ impl<'db> InferenceContext<'db> {
}
ValueNs::ImplSelf(impl_id) => {
let generics = crate::generics::generics(self.db, impl_id.into());
let interner = DbInterner::new_with(self.db, None, None);
let substs = generics.placeholder_subst(self.db);
let ty = self.db.impl_self_ty(impl_id).substitute(Interner, &substs);
let args: crate::next_solver::GenericArgs<'_> = substs.to_nextsolver(interner);
let ty =
self.db.impl_self_ty(impl_id).instantiate(interner, args).to_chalk(interner);
return if let Some((AdtId::StructId(struct_id), substs)) = ty.as_adt() {
Some(ValuePathResolution::GenericDef(
struct_id.into(),
Expand Down Expand Up @@ -359,10 +362,13 @@ impl<'db> InferenceContext<'db> {
};
let substs = match container {
ItemContainerId::ImplId(impl_id) => {
let interner = DbInterner::new_with(self.db, None, None);
let impl_substs = TyBuilder::subst_for_def(self.db, impl_id, None)
.fill_with_inference_vars(&mut self.table)
.build();
let impl_self_ty = self.db.impl_self_ty(impl_id).substitute(Interner, &impl_substs);
let args: crate::next_solver::GenericArgs<'_> = impl_substs.to_nextsolver(interner);
let impl_self_ty =
self.db.impl_self_ty(impl_id).instantiate(interner, args).to_chalk(interner);
self.unify(&impl_self_ty, &ty);
impl_substs
}
Expand Down
77 changes: 12 additions & 65 deletions crates/hir-ty/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use chalk_ir::{
use either::Either;
use hir_def::{
AdtId, AssocItemId, ConstId, ConstParamId, EnumId, EnumVariantId, FunctionId, GenericDefId,
GenericParamId, ImplId, ItemContainerId, LocalFieldId, Lookup, StaticId, StructId, TypeAliasId,
GenericParamId, ItemContainerId, LocalFieldId, Lookup, StaticId, StructId, TypeAliasId,
TypeOrConstParamId, UnionId, VariantId,
builtin_type::BuiltinType,
expr_store::{ExpressionStore, path::Path},
Expand All @@ -34,8 +34,8 @@ use hir_def::{
resolver::{HasResolver, LifetimeNs, Resolver, TypeNs},
signatures::{FunctionSignature, TraitFlags},
type_ref::{
ConstRef, LifetimeRefId, LiteralConstRef, PathId, TraitBoundModifier,
TraitRef as HirTraitRef, TypeBound, TypeRef, TypeRefId,
ConstRef, LifetimeRefId, LiteralConstRef, PathId, TraitBoundModifier, TypeBound, TypeRef,
TypeRefId,
},
};
use hir_expand::name::Name;
Expand All @@ -59,6 +59,10 @@ use crate::{
},
make_binders,
mapping::{from_chalk_trait_id, lt_to_placeholder_idx},
next_solver::{
DbInterner,
mapping::{ChalkToNextSolver, NextSolverToChalk},
},
static_lifetime, to_chalk_trait_id, to_placeholder_idx,
utils::all_super_trait_refs,
variable_kinds_from_iter,
Expand Down Expand Up @@ -565,14 +569,6 @@ impl<'a> TyLoweringContext<'a> {
Some((ctx.lower_trait_ref_from_resolved_path(resolved, explicit_self_ty, false), ctx))
}

fn lower_trait_ref(
&mut self,
trait_ref: &HirTraitRef,
explicit_self_ty: Ty,
) -> Option<TraitRef> {
self.lower_trait_ref_from_path(trait_ref.path, explicit_self_ty).map(|it| it.0)
}

/// When lowering predicates from parents (impl, traits) for children defs (fns, consts, types), `generics` should
/// contain the `Generics` for the **child**, while `predicate_owner` should contain the `GenericDefId` of the
/// **parent**. This is important so we generate the correct bound var/placeholder.
Expand Down Expand Up @@ -851,21 +847,21 @@ fn named_associated_type_shorthand_candidates<R>(
})
};

let interner = DbInterner::new_with(db, None, None);
match res {
TypeNs::SelfType(impl_id) => {
// we're _in_ the impl -- the binders get added back later. Correct,
// but it would be nice to make this more explicit
let trait_ref = db.impl_trait(impl_id)?.into_value_and_skipped_binders().0;
let trait_ref = db.impl_trait(impl_id)?;

let impl_id_as_generic_def: GenericDefId = impl_id.into();
if impl_id_as_generic_def != def {
let subst = TyBuilder::subst_for_def(db, impl_id, None)
.fill_with_bound_vars(DebruijnIndex::INNERMOST, 0)
.build();
let trait_ref = subst.apply(trait_ref, Interner);
let args: crate::next_solver::GenericArgs<'_> = subst.to_nextsolver(interner);
let trait_ref = trait_ref.instantiate(interner, args).to_chalk(interner);
search(trait_ref)
} else {
search(trait_ref)
search(trait_ref.skip_binder().to_chalk(interner))
}
}
TypeNs::GenericParam(param_id) => {
Expand Down Expand Up @@ -1335,31 +1331,6 @@ impl ValueTyDefId {
}
}

pub(crate) fn impl_self_ty_query(db: &dyn HirDatabase, impl_id: ImplId) -> Binders<Ty> {
impl_self_ty_with_diagnostics_query(db, impl_id).0
}

pub(crate) fn impl_self_ty_with_diagnostics_query(
db: &dyn HirDatabase,
impl_id: ImplId,
) -> (Binders<Ty>, Diagnostics) {
let impl_data = db.impl_signature(impl_id);
let resolver = impl_id.resolver(db);
let generics = generics(db, impl_id.into());
let mut ctx = TyLoweringContext::new(
db,
&resolver,
&impl_data.store,
impl_id.into(),
LifetimeElisionKind::AnonymousCreateParameter { report_in_path: true },
)
.with_type_param_mode(ParamLoweringMode::Variable);
(
make_binders(db, &generics, ctx.lower_ty(impl_data.self_ty)),
create_diagnostics(ctx.diagnostics),
)
}

pub(crate) fn const_param_ty_query(db: &dyn HirDatabase, def: ConstParamId) -> Ty {
const_param_ty_with_diagnostics_query(db, def).0
}
Expand Down Expand Up @@ -1397,30 +1368,6 @@ pub(crate) fn const_param_ty_cycle_result(
TyKind::Error.intern(Interner)
}

pub(crate) fn impl_trait_query(db: &dyn HirDatabase, impl_id: ImplId) -> Option<Binders<TraitRef>> {
impl_trait_with_diagnostics_query(db, impl_id).map(|it| it.0)
}

pub(crate) fn impl_trait_with_diagnostics_query(
db: &dyn HirDatabase,
impl_id: ImplId,
) -> Option<(Binders<TraitRef>, Diagnostics)> {
let impl_data = db.impl_signature(impl_id);
let resolver = impl_id.resolver(db);
let mut ctx = TyLoweringContext::new(
db,
&resolver,
&impl_data.store,
impl_id.into(),
LifetimeElisionKind::AnonymousCreateParameter { report_in_path: true },
)
.with_type_param_mode(ParamLoweringMode::Variable);
let (self_ty, binders) = db.impl_self_ty(impl_id).into_value_and_skipped_binders();
let target_trait = impl_data.target_trait.as_ref()?;
let trait_ref = Binders::new(binders, ctx.lower_trait_ref(target_trait, self_ty)?);
Some((trait_ref, create_diagnostics(ctx.diagnostics)))
}

pub(crate) fn return_type_impl_traits(
db: &dyn HirDatabase,
def: hir_def::FunctionId,
Expand Down
9 changes: 8 additions & 1 deletion crates/hir-ty/src/lower/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,15 @@ impl<'a, 'b> PathLoweringContext<'a, 'b> {
// `def` can be either impl itself or item within, and we need impl itself
// now.
let generics = generics.parent_or_self();
let interner = DbInterner::new_with(self.ctx.db, None, None);
let subst = generics.placeholder_subst(self.ctx.db);
self.ctx.db.impl_self_ty(impl_id).substitute(Interner, &subst)
let args: crate::next_solver::GenericArgs<'_> =
subst.to_nextsolver(interner);
self.ctx
.db
.impl_self_ty(impl_id)
.instantiate(interner, args)
.to_chalk(interner)
}
ParamLoweringMode::Variable => TyBuilder::impl_self_ty(self.ctx.db, impl_id)
.fill_with_bound_vars(self.ctx.in_binders, 0)
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-ty/src/lower_nextsolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ pub(crate) fn impl_trait_with_diagnostics_query<'db>(
impl_id.into(),
LifetimeElisionKind::AnonymousCreateParameter { report_in_path: true },
);
let self_ty = db.impl_self_ty_ns(impl_id).skip_binder();
let self_ty = db.impl_self_ty(impl_id).skip_binder();
let target_trait = impl_data.target_trait.as_ref()?;
let trait_ref = EarlyBinder::bind(ctx.lower_trait_ref(target_trait, self_ty)?);
Some((trait_ref, create_diagnostics(ctx.diagnostics)))
Expand Down Expand Up @@ -2024,7 +2024,7 @@ fn named_associated_type_shorthand_candidates<'db, R>(

match res {
TypeNs::SelfType(impl_id) => {
let trait_ref = db.impl_trait_ns(impl_id)?;
let trait_ref = db.impl_trait(impl_id)?;

// FIXME(next-solver): same method in `lower` checks for impl or not
// Is that needed here?
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/lower_nextsolver/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ impl<'a, 'b, 'db> PathLoweringContext<'a, 'b, 'db> {
}
}
}
TypeNs::SelfType(impl_id) => self.ctx.db.impl_self_ty_ns(impl_id).skip_binder(),
TypeNs::SelfType(impl_id) => self.ctx.db.impl_self_ty(impl_id).skip_binder(),
TypeNs::AdtSelfType(adt) => {
let args = crate::next_solver::GenericArgs::identity_for_item(
self.ctx.interner,
Expand Down
22 changes: 14 additions & 8 deletions crates/hir-ty/src/method_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,12 @@ impl TraitImpls {
continue;
}
let target_trait = match db.impl_trait(impl_id) {
Some(tr) => tr.skip_binders().hir_trait_id(),
Some(tr) => tr.skip_binder().def_id.0,
None => continue,
};
let self_ty = db.impl_self_ty(impl_id);
let self_ty_fp = TyFingerprint::for_trait_impl(self_ty.skip_binders());
let interner = DbInterner::new_with(db, None, None);
let self_ty = db.impl_self_ty(impl_id).instantiate_identity().to_chalk(interner);
let self_ty_fp = TyFingerprint::for_trait_impl(&self_ty);
map.entry(target_trait).or_default().entry(self_ty_fp).or_default().push(impl_id);
}

Expand Down Expand Up @@ -414,8 +415,8 @@ impl InherentImpls {
continue;
}

let self_ty = db.impl_self_ty(impl_id);
let self_ty = self_ty.skip_binders();
let interner = DbInterner::new_with(db, None, None);
let self_ty = &db.impl_self_ty(impl_id).instantiate_identity().to_chalk(interner);

match is_inherent_impl_coherent(db, def_map, impl_id, self_ty) {
true => {
Expand Down Expand Up @@ -897,10 +898,13 @@ fn find_matching_impl(
table.run_in_snapshot(|table| {
let impl_substs =
TyBuilder::subst_for_def(db, impl_, None).fill_with_inference_vars(table).build();
let args: crate::next_solver::GenericArgs<'_> =
impl_substs.to_nextsolver(table.interner);
let trait_ref = db
.impl_trait(impl_)
.expect("non-trait method in find_matching_impl")
.substitute(Interner, &impl_substs);
.instantiate(table.interner, args)
.to_chalk(table.interner);

if !table.unify(&trait_ref, &actual_trait_ref) {
return None;
Expand Down Expand Up @@ -1018,7 +1022,9 @@ pub fn check_orphan_rules(db: &dyn HirDatabase, impl_: ImplId) -> bool {
let local_crate = impl_.lookup(db).container.krate();
let is_local = |tgt_crate| tgt_crate == local_crate;

let trait_ref = impl_trait.substitute(Interner, &substs);
let interner = DbInterner::new_with(db, None, None);
let args: crate::next_solver::GenericArgs<'_> = substs.to_nextsolver(interner);
let trait_ref = impl_trait.instantiate(interner, args).to_chalk(interner);
let trait_id = from_chalk_trait_id(trait_ref.trait_id);
if is_local(trait_id.module(db).krate()) {
// trait to be implemented is local
Expand Down Expand Up @@ -1824,7 +1830,7 @@ fn is_valid_impl_fn_candidate(
let _p = tracing::info_span!("subst_for_def").entered();
let impl_subst = table.infer_ctxt.fresh_args_for_item(impl_id.into());
let expect_self_ty = db
.impl_self_ty_ns(impl_id)
.impl_self_ty(impl_id)
.instantiate(table.interner, &impl_subst)
.to_chalk(table.interner);

Expand Down
5 changes: 4 additions & 1 deletion crates/hir-ty/src/mir/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,12 @@ impl MirEvalError {
let self_ = match func.lookup(db).container {
ItemContainerId::ImplId(impl_id) => Some({
let generics = crate::generics::generics(db, impl_id.into());
let interner = DbInterner::new_with(db, None, None);
let substs = generics.placeholder_subst(db);
let args: crate::next_solver::GenericArgs<'_> =
substs.to_nextsolver(interner);
db.impl_self_ty(impl_id)
.substitute(Interner, &substs)
.instantiate(interner, args)
.display(db, display_target)
.to_string()
}),
Expand Down
Loading