Move has_self field to hir::AssocKind::Fn.

`hir::AssocItem` currently has a boolean `fn_has_self_parameter` field,
which is misplaced, because it's only relevant for associated fns, not
for associated consts or types. This commit moves it (and renames it) to
the `AssocKind::Fn` variant, where it belongs.

This requires introducing a new C-style enum, `AssocTag`, which is like
`AssocKind` but without the fields. This is because `AssocKind` values
are passed to various functions like `find_by_ident_and_kind` to
indicate what kind of associated item should be searched for, and having
to specify `has_self` isn't relevant there.

New methods:
- Predicates `AssocItem::is_fn` and `AssocItem::is_method`.
- `AssocItem::as_tag` which converts `AssocItem::kind` to `AssocTag`.

Removed `find_by_name_and_kinds`, which is unused.

`AssocItem::descr` can now distinguish between methods and associated
functions, which slightly improves some error messages.
This commit is contained in:
Nicholas Nethercote
2025-04-11 06:28:59 +10:00
parent abce592029
commit ce2aa97cd6
60 changed files with 288 additions and 284 deletions

View File

@@ -1811,7 +1811,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
self.tcx,
type_def_id,
constraint.ident,
ty::AssocKind::Fn,
ty::AssocTag::Fn,
) {
bound_vars.extend(
self.tcx
@@ -1843,7 +1843,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
self.tcx,
type_def_id,
constraint.ident,
ty::AssocKind::Type,
ty::AssocTag::Type,
)
.map(|(bound_vars, _)| bound_vars);
self.with(scope, |this| {
@@ -1875,13 +1875,13 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
def_id: DefId,
assoc_ident: Ident,
assoc_kind: ty::AssocKind,
assoc_tag: ty::AssocTag,
) -> Option<(Vec<ty::BoundVariableKind>, &'tcx ty::AssocItem)> {
let trait_defines_associated_item_named = |trait_def_id: DefId| {
tcx.associated_items(trait_def_id).find_by_ident_and_kind(
tcx,
assoc_ident,
assoc_kind,
assoc_tag,
trait_def_id,
)
};
@@ -1894,8 +1894,8 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
let Some((def_id, bound_vars)) = stack.pop() else {
break None;
};
// See issue #83753. If someone writes an associated type on a non-trait, just treat it as
// there being no supertrait HRTBs.
// See issue #83753. If someone writes an associated type on a non-trait, just treat it
// as there being no supertrait HRTBs.
match tcx.def_kind(def_id) {
DefKind::Trait | DefKind::TraitAlias | DefKind::Impl { .. } => {}
_ => break None,
@@ -2067,7 +2067,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
self.tcx,
trait_def_id,
item_segment.ident,
ty::AssocKind::Fn,
ty::AssocTag::Fn,
)
});
@@ -2112,7 +2112,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
self.tcx,
trait_def_id,
item_segment.ident,
ty::AssocKind::Fn,
ty::AssocTag::Fn,
) else {
return;
};

View File

@@ -32,7 +32,9 @@ pub(super) fn find_opaque_ty_constraints_for_impl_trait_in_assoc_type(
for &assoc_id in tcx.associated_item_def_ids(impl_def_id) {
let assoc = tcx.associated_item(assoc_id);
match assoc.kind {
ty::AssocKind::Const | ty::AssocKind::Fn => locator.check(assoc_id.expect_local()),
ty::AssocKind::Const | ty::AssocKind::Fn { .. } => {
locator.check(assoc_id.expect_local())
}
// Associated types don't have bodies, so they can't constrain hidden types
ty::AssocKind::Type => {}
}