Split assoc items out of trait_data/impl_data queries

This commit is contained in:
Lukas Wirth 2025-02-26 07:40:42 +01:00
parent 185f9deb45
commit 12f54eec27
34 changed files with 446 additions and 397 deletions

View file

@ -210,7 +210,7 @@ pub(crate) fn deref_by_trait(
};
let trait_id = trait_id()?;
let target = db
.trait_data(trait_id)
.trait_items(trait_id)
.associated_type_by_name(&Name::new_symbol_root(sym::Target.clone()))?;
let projection = {

View file

@ -294,7 +294,7 @@ impl chalk_solve::RustIrDatabase<Interner> for ChalkContext<'_> {
.lang_item(self.krate, LangItem::Future)
.and_then(|item| item.as_trait())
.and_then(|trait_| {
let alias = self.db.trait_data(trait_).associated_type_by_name(
let alias = self.db.trait_items(trait_).associated_type_by_name(
&Name::new_symbol_root(sym::Output.clone()),
)?;
Some((trait_, alias))
@ -684,7 +684,8 @@ pub(crate) fn trait_datum_query(
fundamental: trait_data.flags.contains(TraitFlags::IS_FUNDAMENTAL),
};
let where_clauses = convert_where_clauses(db, trait_.into(), &bound_vars);
let associated_ty_ids = trait_data.associated_types().map(to_assoc_type_id).collect();
let associated_ty_ids =
db.trait_items(trait_).associated_types().map(to_assoc_type_id).collect();
let trait_datum_bound = rust_ir::TraitDatumBound { where_clauses };
let well_known = db.lang_attr(trait_.into()).and_then(well_known_trait_from_lang_item);
let trait_datum = TraitDatum {
@ -856,8 +857,9 @@ fn impl_def_datum(db: &dyn HirDatabase, krate: Crate, impl_id: hir_def::ImplId)
let polarity = if negative { rust_ir::Polarity::Negative } else { rust_ir::Polarity::Positive };
let impl_datum_bound = rust_ir::ImplDatumBound { trait_ref, where_clauses };
let trait_data = db.trait_data(trait_);
let associated_ty_value_ids = impl_data
let trait_data = db.trait_items(trait_);
let associated_ty_value_ids = db
.impl_items(impl_id)
.items
.iter()
.filter_map(|(_, item)| match item {
@ -908,7 +910,7 @@ fn type_alias_associated_ty_value(
.0; // we don't return any assoc ty values if the impl'd trait can't be resolved
let assoc_ty = db
.trait_data(trait_ref.hir_trait_id())
.trait_items(trait_ref.hir_trait_id())
.associated_type_by_name(&type_alias_data.name)
.expect("assoc ty value should not exist"); // validated when building the impl data as well
let (ty, binders) = db.ty(type_alias.into()).into_value_and_skipped_binders();

View file

@ -492,7 +492,7 @@ impl FilterMapNextChecker {
Some(next_function_id),
match next_function_id.lookup(db.upcast()).container {
ItemContainerId::TraitId(iterator_trait_id) => {
let iterator_trait_items = &db.trait_data(iterator_trait_id).items;
let iterator_trait_items = &db.trait_items(iterator_trait_id).items;
iterator_trait_items.iter().find_map(|(name, it)| match it {
&AssocItemId::FunctionId(id) if *name == sym::filter_map.clone() => {
Some(id)

View file

@ -1342,7 +1342,7 @@ impl HirDisplay for Ty {
.lang_item(body.module(db.upcast()).krate(), LangItem::Future)
.and_then(LangItemTarget::as_trait);
let output = future_trait.and_then(|t| {
db.trait_data(t).associated_type_by_name(&Name::new_symbol_root(
db.trait_items(t).associated_type_by_name(&Name::new_symbol_root(
sym::Output.clone(),
))
});

View file

@ -103,7 +103,7 @@ where
// rustc checks for non-lifetime binders here, but we don't support HRTB yet
let trait_data = db.trait_data(trait_);
let trait_data = db.trait_items(trait_);
for (_, assoc_item) in &trait_data.items {
dyn_compatibility_violation_for_assoc_item(db, trait_, *assoc_item, cb)?;
}
@ -166,7 +166,7 @@ fn predicates_reference_self(db: &dyn HirDatabase, trait_: TraitId) -> bool {
// Same as the above, `predicates_reference_self`
fn bounds_reference_self(db: &dyn HirDatabase, trait_: TraitId) -> bool {
let trait_data = db.trait_data(trait_);
let trait_data = db.trait_items(trait_);
trait_data
.items
.iter()

View file

@ -1723,7 +1723,7 @@ impl<'a> InferenceContext<'a> {
fn resolve_output_on(&self, trait_: TraitId) -> Option<TypeAliasId> {
self.db
.trait_data(trait_)
.trait_items(trait_)
.associated_type_by_name(&Name::new_symbol_root(sym::Output.clone()))
}

View file

@ -815,7 +815,7 @@ impl InferenceContext<'_> {
{
if let Some(deref_fn) = self
.db
.trait_data(deref_trait)
.trait_items(deref_trait)
.method_by_name(&Name::new_symbol_root(sym::deref_mut.clone()))
{
break 'b deref_fn == f;

View file

@ -772,7 +772,7 @@ impl InferenceContext<'_> {
if let Some(deref_trait) = self.resolve_lang_trait(LangItem::Deref) {
if let Some(deref_fn) = self
.db
.trait_data(deref_trait)
.trait_items(deref_trait)
.method_by_name(&Name::new_symbol_root(sym::deref.clone()))
{
// FIXME: this is wrong in multiple ways, subst is empty, and we emit it even for builtin deref (note that
@ -930,7 +930,7 @@ impl InferenceContext<'_> {
self.write_expr_adj(*base, adj);
if let Some(func) = self
.db
.trait_data(index_trait)
.trait_items(index_trait)
.method_by_name(&Name::new_symbol_root(sym::index.clone()))
{
let subst = TyBuilder::subst_for_def(self.db, index_trait, None);
@ -1258,7 +1258,7 @@ impl InferenceContext<'_> {
let Some(trait_) = fn_x.get_id(self.db, self.table.trait_env.krate) else {
return;
};
let trait_data = self.db.trait_data(trait_);
let trait_data = self.db.trait_items(trait_);
if let Some(func) = trait_data.method_by_name(&fn_x.method_name()) {
let subst = TyBuilder::subst_for_def(self.db, trait_, None)
.push(callee_ty.clone())
@ -1426,7 +1426,7 @@ impl InferenceContext<'_> {
let trait_func = lang_items_for_bin_op(op).and_then(|(name, lang_item)| {
let trait_id = self.resolve_lang_item(lang_item)?.as_trait()?;
let func = self.db.trait_data(trait_id).method_by_name(&name)?;
let func = self.db.trait_items(trait_id).method_by_name(&name)?;
Some((trait_id, func))
});
let (trait_, func) = match trait_func {

View file

@ -134,7 +134,7 @@ impl InferenceContext<'_> {
{
if let Some(index_fn) = self
.db
.trait_data(index_trait)
.trait_items(index_trait)
.method_by_name(&Name::new_symbol_root(sym::index_mut.clone()))
{
*f = index_fn;
@ -201,7 +201,7 @@ impl InferenceContext<'_> {
mutability = Mutability::Not;
} else if let Some(deref_fn) = self
.db
.trait_data(deref_trait)
.trait_items(deref_trait)
.method_by_name(&Name::new_symbol_root(sym::deref_mut.clone()))
{
*f = deref_fn;

View file

@ -277,7 +277,7 @@ impl InferenceContext<'_> {
) -> Option<(ValueNs, Substitution)> {
let trait_ = trait_ref.hir_trait_id();
let item =
self.db.trait_data(trait_).items.iter().map(|(_name, id)| *id).find_map(|item| {
self.db.trait_items(trait_).items.iter().map(|(_name, id)| *id).find_map(|item| {
match item {
AssocItemId::FunctionId(func) => {
if segment.name == &self.db.function_data(func).name {

View file

@ -801,7 +801,7 @@ impl<'a> InferenceTable<'a> {
] {
let krate = self.trait_env.krate;
let fn_trait = fn_trait_name.get_id(self.db, krate)?;
let trait_data = self.db.trait_data(fn_trait);
let trait_data = self.db.trait_items(fn_trait);
let output_assoc_type =
trait_data.associated_type_by_name(&Name::new_symbol_root(output_assoc_name))?;

View file

@ -906,7 +906,7 @@ pub fn callable_sig_from_fn_trait(
let krate = trait_env.krate;
let fn_once_trait = FnTrait::FnOnce.get_id(db, krate)?;
let output_assoc_type = db
.trait_data(fn_once_trait)
.trait_items(fn_once_trait)
.associated_type_by_name(&Name::new_symbol_root(sym::Output.clone()))?;
let mut table = InferenceTable::new(db, trait_env.clone());

View file

@ -898,7 +898,7 @@ fn named_associated_type_shorthand_candidates<R>(
) -> Option<R> {
let mut search = |t| {
all_super_trait_refs(db, t, |t| {
let data = db.trait_data(t.hir_trait_id());
let data = db.trait_items(t.hir_trait_id());
for (name, assoc_id) in &data.items {
if let AssocItemId::TypeAliasId(alias) = assoc_id {
@ -1068,7 +1068,7 @@ pub(crate) fn generic_predicates_for_param_query(
};
all_super_traits(db.upcast(), tr).iter().any(|tr| {
db.trait_data(*tr).items.iter().any(|(name, item)| {
db.trait_items(*tr).items.iter().any(|(name, item)| {
matches!(item, AssocItemId::TypeAliasId(_)) && name == assoc_name
})
})

View file

@ -169,7 +169,7 @@ impl<'a, 'b> PathLoweringContext<'a, 'b> {
self.skip_resolved_segment();
let segment = self.current_or_prev_segment;
let found =
self.ctx.db.trait_data(trait_).associated_type_by_name(segment.name);
self.ctx.db.trait_items(trait_).associated_type_by_name(segment.name);
match found {
Some(associated_ty) => {

View file

@ -8,8 +8,8 @@ use arrayvec::ArrayVec;
use base_db::Crate;
use chalk_ir::{cast::Cast, UniverseIndex, WithKind};
use hir_def::{
data::{adt::StructFlags, ImplData, TraitFlags},
nameres::DefMap,
data::{adt::StructFlags, TraitFlags},
nameres::{assoc::ImplItems, DefMap},
AssocItemId, BlockId, ConstId, FunctionId, HasModule, ImplId, ItemContainerId, Lookup,
ModuleId, TraitId,
};
@ -325,7 +325,7 @@ impl InherentImpls {
let self_ty = db.impl_self_ty(impl_id);
let self_ty = self_ty.skip_binders();
match is_inherent_impl_coherent(db, def_map, &data, self_ty) {
match is_inherent_impl_coherent(db, def_map, impl_id, self_ty) {
true => {
// `fp` should only be `None` in error cases (either erroneous code or incomplete name resolution)
if let Some(fp) = TyFingerprint::for_inherent_impl(self_ty) {
@ -765,11 +765,10 @@ fn find_matching_impl(
mut impls: impl Iterator<Item = ImplId>,
mut table: InferenceTable<'_>,
actual_trait_ref: TraitRef,
) -> Option<(Arc<ImplData>, Substitution)> {
) -> Option<(Arc<ImplItems>, Substitution)> {
let db = table.db;
impls.find_map(|impl_| {
table.run_in_snapshot(|table| {
let impl_data = db.impl_data(impl_);
let impl_substs =
TyBuilder::subst_for_def(db, impl_, None).fill_with_inference_vars(table).build();
let trait_ref = db
@ -787,7 +786,7 @@ fn find_matching_impl(
let goal = crate::Goal::all(Interner, wcs);
table.try_obligation(goal.clone())?;
table.register_obligation(goal);
Some((impl_data, table.resolve_completely(impl_substs)))
Some((db.impl_items(impl_), table.resolve_completely(impl_substs)))
})
})
}
@ -795,7 +794,7 @@ fn find_matching_impl(
fn is_inherent_impl_coherent(
db: &dyn HirDatabase,
def_map: &DefMap,
impl_data: &ImplData,
impl_id: ImplId,
self_ty: &Ty,
) -> bool {
let self_ty = self_ty.kind(Interner);
@ -848,9 +847,10 @@ fn is_inherent_impl_coherent(
_ => false,
};
let items = db.impl_items(impl_id);
rustc_has_incoherent_inherent_impls
&& !impl_data.items.is_empty()
&& impl_data.items.iter().all(|&(_, assoc)| match assoc {
&& !items.items.is_empty()
&& items.items.iter().all(|&(_, assoc)| match assoc {
AssocItemId::FunctionId(it) => db.function_data(it).rustc_allow_incoherent_impl,
AssocItemId::ConstId(it) => db.const_data(it).rustc_allow_incoherent_impl,
AssocItemId::TypeAliasId(it) => db.type_alias_data(it).rustc_allow_incoherent_impl,
@ -1241,7 +1241,7 @@ fn iterate_trait_method_candidates(
// trait, but if we find out it doesn't, we'll skip the rest of the
// iteration
let mut known_implemented = false;
for &(_, item) in data.items.iter() {
for &(_, item) in db.trait_items(t).items.iter() {
// Don't pass a `visible_from_module` down to `is_valid_candidate`,
// since only inherent methods should be included into visibility checking.
let visible =
@ -1368,7 +1368,7 @@ fn iterate_inherent_methods(
) -> ControlFlow<()> {
let db = table.db;
for t in traits {
let data = db.trait_data(t);
let data = db.trait_items(t);
for &(_, item) in data.items.iter() {
// We don't pass `visible_from_module` as all trait items should be visible.
let visible = match is_valid_trait_method_candidate(
@ -1401,7 +1401,7 @@ fn iterate_inherent_methods(
callback: &mut dyn FnMut(ReceiverAdjustments, AssocItemId, bool) -> ControlFlow<()>,
) -> ControlFlow<()> {
for &impl_id in impls.for_self_ty(self_ty) {
for &(ref item_name, item) in table.db.impl_data(impl_id).items.iter() {
for &(ref item_name, item) in table.db.impl_items(impl_id).items.iter() {
let visible = match is_valid_impl_method_candidate(
table,
self_ty,

View file

@ -661,19 +661,19 @@ impl Evaluator<'_> {
.lang_item(crate_id, LangItem::Fn)
.and_then(|x| x.as_trait())
.and_then(|x| {
db.trait_data(x).method_by_name(&Name::new_symbol_root(sym::call.clone()))
db.trait_items(x).method_by_name(&Name::new_symbol_root(sym::call.clone()))
}),
cached_fn_mut_trait_func: db
.lang_item(crate_id, LangItem::FnMut)
.and_then(|x| x.as_trait())
.and_then(|x| {
db.trait_data(x).method_by_name(&Name::new_symbol_root(sym::call_mut.clone()))
db.trait_items(x).method_by_name(&Name::new_symbol_root(sym::call_mut.clone()))
}),
cached_fn_once_trait_func: db
.lang_item(crate_id, LangItem::FnOnce)
.and_then(|x| x.as_trait())
.and_then(|x| {
db.trait_data(x).method_by_name(&Name::new_symbol_root(sym::call_once.clone()))
db.trait_items(x).method_by_name(&Name::new_symbol_root(sym::call_once.clone()))
}),
})
}
@ -2818,7 +2818,9 @@ impl Evaluator<'_> {
) -> Result<()> {
let Some(drop_fn) = (|| {
let drop_trait = self.db.lang_item(self.crate_id, LangItem::Drop)?.as_trait()?;
self.db.trait_data(drop_trait).method_by_name(&Name::new_symbol_root(sym::drop.clone()))
self.db
.trait_items(drop_trait)
.method_by_name(&Name::new_symbol_root(sym::drop.clone()))
})() else {
// in some tests we don't have drop trait in minicore, and
// we can ignore drop in them.
@ -2928,7 +2930,7 @@ pub fn render_const_using_debug_impl(
not_supported!("core::fmt::Debug not found");
};
let Some(debug_fmt_fn) =
db.trait_data(debug_trait).method_by_name(&Name::new_symbol_root(sym::fmt.clone()))
db.trait_items(debug_trait).method_by_name(&Name::new_symbol_root(sym::fmt.clone()))
else {
not_supported!("core::fmt::Debug::fmt not found");
};

View file

@ -1261,7 +1261,7 @@ impl Evaluator<'_> {
if let Some(target) = self.db.lang_item(self.crate_id, LangItem::FnOnce) {
if let Some(def) = target.as_trait().and_then(|it| {
self.db
.trait_data(it)
.trait_items(it)
.method_by_name(&Name::new_symbol_root(sym::call_once.clone()))
}) {
self.exec_fn_trait(

View file

@ -195,7 +195,7 @@ impl MirLowerCtx<'_> {
self.resolve_lang_item(LangItem::DerefMut)?.as_trait()
{
if let Some(deref_fn) =
self.db.trait_data(deref_trait).method_by_name(
self.db.trait_items(deref_trait).method_by_name(
&Name::new_symbol_root(sym::deref_mut.clone()),
)
{
@ -353,7 +353,7 @@ impl MirLowerCtx<'_> {
.ok_or(MirLowerError::LangItemNotFound(trait_lang_item))?;
let deref_fn = self
.db
.trait_data(deref_trait)
.trait_items(deref_trait)
.method_by_name(&trait_method_name)
.ok_or(MirLowerError::LangItemNotFound(trait_lang_item))?;
let deref_fn_op = Operand::const_zst(

View file

@ -439,7 +439,7 @@ pub(crate) fn visit_module(
) {
visit_scope(db, crate_def_map, &crate_def_map[module_id].scope, cb);
for impl_id in crate_def_map[module_id].scope.impls() {
let impl_data = db.impl_data(impl_id);
let impl_data = db.impl_items(impl_id);
for &(_, item) in impl_data.items.iter() {
match item {
AssocItemId::FunctionId(it) => {
@ -488,7 +488,7 @@ pub(crate) fn visit_module(
});
}
ModuleDefId::TraitId(it) => {
let trait_data = db.trait_data(it);
let trait_data = db.trait_items(it);
for &(_, item) in trait_data.items.iter() {
match item {
AssocItemId::FunctionId(it) => cb(it.into()),

View file

@ -226,7 +226,7 @@ pub(super) fn associated_type_by_name_including_super_traits(
name: &Name,
) -> Option<(TraitRef, TypeAliasId)> {
all_super_trait_refs(db, trait_ref, |t| {
let assoc_type = db.trait_data(t.hir_trait_id()).associated_type_by_name(name)?;
let assoc_type = db.trait_items(t.hir_trait_id()).associated_type_by_name(name)?;
Some((t, assoc_type))
})
}