Change direct_super_traits to use generic_predicates_for_param_ns

This commit is contained in:
jackh726 2025-08-11 02:34:53 +00:00
parent 17b94c41b1
commit aa890b49ff
4 changed files with 50 additions and 25 deletions

View File

@ -4,10 +4,7 @@
use std::{cell::LazyCell, iter};
use base_db::Crate;
use chalk_ir::{
DebruijnIndex,
fold::{FallibleTypeFolder, Shift},
};
use chalk_ir::{DebruijnIndex, fold::FallibleTypeFolder};
use hir_def::{
EnumId, EnumVariantId, FunctionId, Lookup, TraitId, TypeAliasId, TypeOrConstParamId,
db::DefDatabase,
@ -20,6 +17,7 @@ use hir_expand::name::Name;
use intern::sym;
use rustc_abi::TargetDataLayout;
use rustc_hash::FxHashSet;
use rustc_type_ir::inherent::{IntoKind, SliceLike};
use smallvec::{SmallVec, smallvec};
use span::Edition;
use stdx::never;
@ -31,6 +29,11 @@ use crate::{
db::HirDatabase,
layout::{Layout, TagEncoding},
mir::pad16,
next_solver::{
DbInterner,
mapping::{ChalkToNextSolver, convert_args_for_result},
},
to_chalk_trait_id,
};
pub(crate) fn fn_traits(db: &dyn DefDatabase, krate: Crate) -> impl Iterator<Item = TraitId> + '_ {
@ -191,25 +194,37 @@ fn direct_super_traits_cb(db: &dyn DefDatabase, trait_: TraitId, cb: impl FnMut(
}
fn direct_super_trait_refs(db: &dyn HirDatabase, trait_ref: &TraitRef, cb: impl FnMut(TraitRef)) {
let interner = DbInterner::new_with(db, None, None);
let generic_params = db.generic_params(trait_ref.hir_trait_id().into());
let trait_self = match generic_params.trait_self_param() {
Some(p) => TypeOrConstParamId { parent: trait_ref.hir_trait_id().into(), local_id: p },
None => return,
};
db.generic_predicates_for_param(trait_self.parent, trait_self, None)
let trait_ref_args: crate::next_solver::GenericArgs<'_> =
trait_ref.substitution.to_nextsolver(interner);
db.generic_predicates_for_param_ns(trait_self.parent, trait_self, None)
.iter()
.filter_map(|pred| {
pred.as_ref().filter_map(|pred| match pred.skip_binders() {
// FIXME: how to correctly handle higher-ranked bounds here?
WhereClause::Implemented(tr) => Some(
tr.clone()
.shifted_out_to(Interner, DebruijnIndex::ONE)
.expect("FIXME unexpected higher-ranked trait bound"),
),
let pred = pred.kind();
// FIXME: how to correctly handle higher-ranked bounds here?
let pred = pred.no_bound_vars().expect("FIXME unexpected higher-ranked trait bound");
match pred {
rustc_type_ir::ClauseKind::Trait(t) => {
let t =
rustc_type_ir::EarlyBinder::bind(t).instantiate(interner, trait_ref_args);
let trait_id = match t.def_id() {
crate::next_solver::SolverDefId::TraitId(id) => to_chalk_trait_id(id),
_ => unreachable!(),
};
let substitution =
convert_args_for_result(interner, t.trait_ref.args.as_slice());
let tr = chalk_ir::TraitRef { trait_id, substitution };
Some(tr)
}
_ => None,
})
}
})
.map(|pred| pred.substitute(Interner, &trait_ref.substitution))
.for_each(cb);
}

View File

@ -86,7 +86,8 @@ use hir_ty::{
method_resolution,
mir::{MutBorrowKind, interpret_mir},
next_solver::{
DbInterner, GenericArgs, SolverDefId, infer::InferCtxt, mapping::ChalkToNextSolver,
ClauseKind, DbInterner, GenericArgs, SolverDefId, infer::InferCtxt,
mapping::ChalkToNextSolver,
},
primitive::UintTy,
traits::FnTrait,
@ -114,6 +115,7 @@ pub use crate::{
VisibleTraits,
},
};
use rustc_type_ir::inherent::IntoKind;
// Be careful with these re-exports.
//
@ -4245,11 +4247,15 @@ impl TypeParam {
/// parameter, not additional bounds that might be added e.g. by a method if
/// the parameter comes from an impl!
pub fn trait_bounds(self, db: &dyn HirDatabase) -> Vec<Trait> {
db.generic_predicates_for_param(self.id.parent(), self.id.into(), None)
db.generic_predicates_for_param_ns(self.id.parent(), self.id.into(), None)
.iter()
.filter_map(|pred| match &pred.skip_binders().skip_binders() {
hir_ty::WhereClause::Implemented(trait_ref) => {
Some(Trait::from(trait_ref.hir_trait_id()))
.filter_map(|pred| match &pred.kind().skip_binder() {
ClauseKind::Trait(trait_ref) => {
let trait_ = match trait_ref.def_id() {
SolverDefId::TraitId(t) => t,
_ => unreachable!(),
};
Some(Trait::from(trait_))
}
_ => None,
})

View File

@ -581,11 +581,13 @@ fn goto_type_action_for_def(
});
}
if let Ok(generic_def) = GenericDef::try_from(def) {
generic_def.type_or_const_params(db).into_iter().for_each(|it| {
walk_and_push_ty(db, &it.ty(db), &mut push_new_def);
});
}
salsa::attach(db, || {
if let Ok(generic_def) = GenericDef::try_from(def) {
generic_def.type_or_const_params(db).into_iter().for_each(|it| {
walk_and_push_ty(db, &it.ty(db), &mut push_new_def);
});
}
});
let ty = match def {
Definition::Local(it) => Some(it.ty(db)),

View File

@ -674,7 +674,9 @@ impl Analysis {
position: FilePosition,
) -> Cancellable<Option<Vec<HighlightedRange>>> {
self.with_db(|db| {
highlight_related::highlight_related(&Semantics::new(db), config, position)
salsa::attach(db, || {
highlight_related::highlight_related(&Semantics::new(db), config, position)
})
})
}