Skip to content

Commit ce2aa97

Browse files
committed
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.
1 parent abce592 commit ce2aa97

File tree

60 files changed

+288
-284
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+288
-284
lines changed

compiler/rustc_ast_lowering/src/delegation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
8585
.delegation_fn_sigs
8686
.get(&local_def_id)
8787
.is_some_and(|sig| sig.has_self),
88-
None => self.tcx.associated_item(def_id).fn_has_self_parameter,
88+
None => self.tcx.associated_item(def_id).is_method(),
8989
},
9090
_ => span_bug!(span, "unexpected DefKind for delegation item"),
9191
}

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
647647
&& tc.polarity() == ty::PredicatePolarity::Positive
648648
&& supertrait_def_ids(tcx, tc.def_id())
649649
.flat_map(|trait_did| tcx.associated_items(trait_did).in_definition_order())
650-
.any(|item| item.fn_has_self_parameter)
650+
.any(|item| item.is_method())
651651
})
652652
}) {
653653
return None;

compiler/rustc_codegen_cranelift/src/main_shim.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
22
use rustc_hir::LangItem;
3-
use rustc_middle::ty::{AssocKind, GenericArg};
3+
use rustc_middle::ty::{AssocTag, GenericArg};
44
use rustc_session::config::EntryFnType;
55
use rustc_span::{DUMMY_SP, Ident};
66

@@ -107,7 +107,7 @@ pub(crate) fn maybe_create_entry_wrapper(
107107
.find_by_ident_and_kind(
108108
tcx,
109109
Ident::from_str("report"),
110-
AssocKind::Fn,
110+
AssocTag::Fn,
111111
termination_trait,
112112
)
113113
.unwrap();

compiler/rustc_hir_analysis/src/check/check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ fn best_definition_site_of_opaque<'tcx>(
443443
let impl_def_id = tcx.local_parent(parent);
444444
for assoc in tcx.associated_items(impl_def_id).in_definition_order() {
445445
match assoc.kind {
446-
ty::AssocKind::Const | ty::AssocKind::Fn => {
446+
ty::AssocKind::Const | ty::AssocKind::Fn { .. } => {
447447
if let ControlFlow::Break(span) = locator.check(assoc.def_id.expect_local())
448448
{
449449
return Some(span);
@@ -942,7 +942,7 @@ fn check_impl_items_against_trait<'tcx>(
942942

943943
if res.is_ok() {
944944
match ty_impl_item.kind {
945-
ty::AssocKind::Fn => {
945+
ty::AssocKind::Fn { .. } => {
946946
compare_impl_item::refine::check_refining_return_position_impl_trait_in_trait(
947947
tcx,
948948
ty_impl_item,

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub(super) fn compare_impl_item(
4343
debug!(?impl_trait_ref);
4444

4545
match impl_item.kind {
46-
ty::AssocKind::Fn => compare_impl_method(tcx, impl_item, trait_item, impl_trait_ref),
46+
ty::AssocKind::Fn { .. } => compare_impl_method(tcx, impl_item, trait_item, impl_trait_ref),
4747
ty::AssocKind::Type => compare_impl_ty(tcx, impl_item, trait_item, impl_trait_ref),
4848
ty::AssocKind::Const => compare_impl_const(tcx, impl_item, trait_item, impl_trait_ref),
4949
}
@@ -1036,7 +1036,7 @@ fn report_trait_method_mismatch<'tcx>(
10361036
);
10371037
match &terr {
10381038
TypeError::ArgumentMutability(0) | TypeError::ArgumentSorts(_, 0)
1039-
if trait_m.fn_has_self_parameter =>
1039+
if trait_m.is_method() =>
10401040
{
10411041
let ty = trait_sig.inputs()[0];
10421042
let sugg = get_self_string(ty, |ty| ty == impl_trait_ref.self_ty());
@@ -1255,7 +1255,7 @@ fn compare_self_type<'tcx>(
12551255
get_self_string(self_arg_ty, can_eq_self)
12561256
};
12571257

1258-
match (trait_m.fn_has_self_parameter, impl_m.fn_has_self_parameter) {
1258+
match (trait_m.is_method(), impl_m.is_method()) {
12591259
(false, false) | (true, true) => {}
12601260

12611261
(false, true) => {
@@ -1363,7 +1363,7 @@ fn compare_number_of_generics<'tcx>(
13631363
let mut err_occurred = None;
13641364
for (kind, trait_count, impl_count) in matchings {
13651365
if impl_count != trait_count {
1366-
let arg_spans = |kind: ty::AssocKind, generics: &hir::Generics<'_>| {
1366+
let arg_spans = |item: &ty::AssocItem, generics: &hir::Generics<'_>| {
13671367
let mut spans = generics
13681368
.params
13691369
.iter()
@@ -1373,7 +1373,7 @@ fn compare_number_of_generics<'tcx>(
13731373
} => {
13741374
// A fn can have an arbitrary number of extra elided lifetimes for the
13751375
// same signature.
1376-
!matches!(kind, ty::AssocKind::Fn)
1376+
!item.is_fn()
13771377
}
13781378
_ => true,
13791379
})
@@ -1386,7 +1386,7 @@ fn compare_number_of_generics<'tcx>(
13861386
};
13871387
let (trait_spans, impl_trait_spans) = if let Some(def_id) = trait_.def_id.as_local() {
13881388
let trait_item = tcx.hir_expect_trait_item(def_id);
1389-
let arg_spans: Vec<Span> = arg_spans(trait_.kind, trait_item.generics);
1389+
let arg_spans: Vec<Span> = arg_spans(&trait_, trait_item.generics);
13901390
let impl_trait_spans: Vec<Span> = trait_item
13911391
.generics
13921392
.params
@@ -1412,7 +1412,7 @@ fn compare_number_of_generics<'tcx>(
14121412
_ => None,
14131413
})
14141414
.collect();
1415-
let spans = arg_spans(impl_.kind, impl_item.generics);
1415+
let spans = arg_spans(&impl_, impl_item.generics);
14161416
let span = spans.first().copied();
14171417

14181418
let mut err = tcx.dcx().struct_span_err(

compiler/rustc_hir_analysis/src/check/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,14 @@ fn fn_sig_suggestion<'tcx>(
407407
.enumerate()
408408
.map(|(i, ty)| {
409409
Some(match ty.kind() {
410-
ty::Param(_) if assoc.fn_has_self_parameter && i == 0 => "self".to_string(),
410+
ty::Param(_) if assoc.is_method() && i == 0 => "self".to_string(),
411411
ty::Ref(reg, ref_ty, mutability) if i == 0 => {
412412
let reg = format!("{reg} ");
413413
let reg = match &reg[..] {
414414
"'_ " | " " => "",
415415
reg => reg,
416416
};
417-
if assoc.fn_has_self_parameter {
417+
if assoc.is_method() {
418418
match ref_ty.kind() {
419419
ty::Param(param) if param.name == kw::SelfUpper => {
420420
format!("&{}{}self", reg, mutability.prefix_str())
@@ -427,7 +427,7 @@ fn fn_sig_suggestion<'tcx>(
427427
}
428428
}
429429
_ => {
430-
if assoc.fn_has_self_parameter && i == 0 {
430+
if assoc.is_method() && i == 0 {
431431
format!("self: {ty}")
432432
} else {
433433
format!("_: {ty}")
@@ -489,7 +489,7 @@ fn suggestion_signature<'tcx>(
489489
);
490490

491491
match assoc.kind {
492-
ty::AssocKind::Fn => fn_sig_suggestion(
492+
ty::AssocKind::Fn { .. } => fn_sig_suggestion(
493493
tcx,
494494
tcx.liberate_late_bound_regions(
495495
assoc.def_id,

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, trait_def_id: LocalDefId) {
432432

433433
let item_required_bounds = match tcx.associated_item(item_def_id).kind {
434434
// In our example, this corresponds to `into_iter` method
435-
ty::AssocKind::Fn => {
435+
ty::AssocKind::Fn { .. } => {
436436
// For methods, we check the function signature's return type for any GATs
437437
// to constrain. In the `into_iter` case, we see that the return type
438438
// `Self::Iter<'a>` is a GAT we want to gather any potential missing bounds from.
@@ -1089,7 +1089,7 @@ fn check_associated_item(
10891089
);
10901090
Ok(())
10911091
}
1092-
ty::AssocKind::Fn => {
1092+
ty::AssocKind::Fn { .. } => {
10931093
let sig = tcx.fn_sig(item.def_id).instantiate_identity();
10941094
let hir_sig = sig_if_method.expect("bad signature for method");
10951095
check_fn_or_method(
@@ -1716,7 +1716,7 @@ fn check_method_receiver<'tcx>(
17161716
) -> Result<(), ErrorGuaranteed> {
17171717
let tcx = wfcx.tcx();
17181718

1719-
if !method.fn_has_self_parameter {
1719+
if !method.is_method() {
17201720
return Ok(());
17211721
}
17221722

compiler/rustc_hir_analysis/src/collect.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use rustc_trait_selection::traits::ObligationCtxt;
4444
use tracing::{debug, instrument};
4545

4646
use crate::errors;
47-
use crate::hir_ty_lowering::errors::assoc_kind_str;
47+
use crate::hir_ty_lowering::errors::assoc_tag_str;
4848
use crate::hir_ty_lowering::{FeedConstTy, HirTyLowerer, RegionInferReason};
4949

5050
pub(crate) mod dump;
@@ -450,7 +450,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
450450
item_def_id: DefId,
451451
item_segment: &rustc_hir::PathSegment<'tcx>,
452452
poly_trait_ref: ty::PolyTraitRef<'tcx>,
453-
kind: ty::AssocKind,
453+
assoc_tag: ty::AssocTag,
454454
) -> Result<(DefId, ty::GenericArgsRef<'tcx>), ErrorGuaranteed> {
455455
if let Some(trait_ref) = poly_trait_ref.no_bound_vars() {
456456
let item_args = self.lowerer().lower_generic_args_of_assoc_item(
@@ -525,7 +525,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
525525
inferred_sugg,
526526
bound,
527527
mpart_sugg,
528-
what: assoc_kind_str(kind),
528+
what: assoc_tag_str(assoc_tag),
529529
}))
530530
}
531531
}

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1811,7 +1811,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
18111811
self.tcx,
18121812
type_def_id,
18131813
constraint.ident,
1814-
ty::AssocKind::Fn,
1814+
ty::AssocTag::Fn,
18151815
) {
18161816
bound_vars.extend(
18171817
self.tcx
@@ -1843,7 +1843,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
18431843
self.tcx,
18441844
type_def_id,
18451845
constraint.ident,
1846-
ty::AssocKind::Type,
1846+
ty::AssocTag::Type,
18471847
)
18481848
.map(|(bound_vars, _)| bound_vars);
18491849
self.with(scope, |this| {
@@ -1875,13 +1875,13 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
18751875
tcx: TyCtxt<'tcx>,
18761876
def_id: DefId,
18771877
assoc_ident: Ident,
1878-
assoc_kind: ty::AssocKind,
1878+
assoc_tag: ty::AssocTag,
18791879
) -> Option<(Vec<ty::BoundVariableKind>, &'tcx ty::AssocItem)> {
18801880
let trait_defines_associated_item_named = |trait_def_id: DefId| {
18811881
tcx.associated_items(trait_def_id).find_by_ident_and_kind(
18821882
tcx,
18831883
assoc_ident,
1884-
assoc_kind,
1884+
assoc_tag,
18851885
trait_def_id,
18861886
)
18871887
};
@@ -1894,8 +1894,8 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
18941894
let Some((def_id, bound_vars)) = stack.pop() else {
18951895
break None;
18961896
};
1897-
// See issue #83753. If someone writes an associated type on a non-trait, just treat it as
1898-
// there being no supertrait HRTBs.
1897+
// See issue #83753. If someone writes an associated type on a non-trait, just treat it
1898+
// as there being no supertrait HRTBs.
18991899
match tcx.def_kind(def_id) {
19001900
DefKind::Trait | DefKind::TraitAlias | DefKind::Impl { .. } => {}
19011901
_ => break None,
@@ -2067,7 +2067,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
20672067
self.tcx,
20682068
trait_def_id,
20692069
item_segment.ident,
2070-
ty::AssocKind::Fn,
2070+
ty::AssocTag::Fn,
20712071
)
20722072
});
20732073

@@ -2112,7 +2112,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
21122112
self.tcx,
21132113
trait_def_id,
21142114
item_segment.ident,
2115-
ty::AssocKind::Fn,
2115+
ty::AssocTag::Fn,
21162116
) else {
21172117
return;
21182118
};

compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ pub(super) fn find_opaque_ty_constraints_for_impl_trait_in_assoc_type(
3232
for &assoc_id in tcx.associated_item_def_ids(impl_def_id) {
3333
let assoc = tcx.associated_item(assoc_id);
3434
match assoc.kind {
35-
ty::AssocKind::Const | ty::AssocKind::Fn => locator.check(assoc_id.expect_local()),
35+
ty::AssocKind::Const | ty::AssocKind::Fn { .. } => {
36+
locator.check(assoc_id.expect_local())
37+
}
3638
// Associated types don't have bodies, so they can't constrain hidden types
3739
ty::AssocKind::Type => {}
3840
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -431,16 +431,16 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
431431
) -> Result<(), ErrorGuaranteed> {
432432
let tcx = self.tcx();
433433

434-
let assoc_kind = if constraint.gen_args.parenthesized
434+
let assoc_tag = if constraint.gen_args.parenthesized
435435
== hir::GenericArgsParentheses::ReturnTypeNotation
436436
{
437-
ty::AssocKind::Fn
437+
ty::AssocTag::Fn
438438
} else if let hir::AssocItemConstraintKind::Equality { term: hir::Term::Const(_) } =
439439
constraint.kind
440440
{
441-
ty::AssocKind::Const
441+
ty::AssocTag::Const
442442
} else {
443-
ty::AssocKind::Type
443+
ty::AssocTag::Type
444444
};
445445

446446
// Given something like `U: Trait<T = X>`, we want to produce a predicate like
@@ -453,7 +453,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
453453
// trait SuperTrait<A> { type T; }
454454
let candidate = if self.probe_trait_that_defines_assoc_item(
455455
trait_ref.def_id(),
456-
assoc_kind,
456+
assoc_tag,
457457
constraint.ident,
458458
) {
459459
// Simple case: The assoc item is defined in the current trait.
@@ -464,7 +464,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
464464
self.probe_single_bound_for_assoc_item(
465465
|| traits::supertraits(tcx, trait_ref),
466466
AssocItemQSelf::Trait(trait_ref.def_id()),
467-
assoc_kind,
467+
assoc_tag,
468468
constraint.ident,
469469
path_span,
470470
Some(constraint),
@@ -474,7 +474,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
474474
let assoc_item = self
475475
.probe_assoc_item(
476476
constraint.ident,
477-
assoc_kind,
477+
assoc_tag,
478478
hir_ref_id,
479479
constraint.span,
480480
candidate.def_id(),
@@ -493,7 +493,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
493493
})
494494
.or_insert(constraint.span);
495495

496-
let projection_term = if let ty::AssocKind::Fn = assoc_kind {
496+
let projection_term = if let ty::AssocTag::Fn = assoc_tag {
497497
let bound_vars = tcx.late_bound_vars(constraint.hir_id);
498498
ty::Binder::bind_with_vars(
499499
self.lower_return_type_notation_ty(candidate, assoc_item.def_id, path_span)?.into(),
@@ -542,7 +542,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
542542
};
543543

544544
match constraint.kind {
545-
hir::AssocItemConstraintKind::Equality { .. } if let ty::AssocKind::Fn = assoc_kind => {
545+
hir::AssocItemConstraintKind::Equality { .. } if let ty::AssocTag::Fn = assoc_tag => {
546546
return Err(self.dcx().emit_err(crate::errors::ReturnTypeNotationEqualityBound {
547547
span: constraint.span,
548548
}));
@@ -679,7 +679,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
679679
trait_def_id,
680680
hir_ty.span,
681681
item_segment,
682-
ty::AssocKind::Type,
682+
ty::AssocTag::Type,
683683
);
684684
return Ty::new_error(tcx, guar);
685685
};
@@ -771,7 +771,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
771771
)
772772
},
773773
AssocItemQSelf::SelfTyAlias,
774-
ty::AssocKind::Fn,
774+
ty::AssocTag::Fn,
775775
assoc_ident,
776776
span,
777777
None,
@@ -783,7 +783,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
783783
) => self.probe_single_ty_param_bound_for_assoc_item(
784784
param_did.expect_local(),
785785
qself.span,
786-
ty::AssocKind::Fn,
786+
ty::AssocTag::Fn,
787787
assoc_ident,
788788
span,
789789
)?,
@@ -823,7 +823,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
823823

824824
let trait_def_id = bound.def_id();
825825
let assoc_ty = self
826-
.probe_assoc_item(assoc_ident, ty::AssocKind::Fn, qpath_hir_id, span, trait_def_id)
826+
.probe_assoc_item(assoc_ident, ty::AssocTag::Fn, qpath_hir_id, span, trait_def_id)
827827
.expect("failed to find associated type");
828828

829829
Ok((bound, assoc_ty.def_id))

0 commit comments

Comments
 (0)