Skip to content

Commit 13cd525

Browse files
authored
Rollup merge of #139669 - nnethercote:overhaul-AssocItem, r=oli-obk
Overhaul `AssocItem` `AssocItem` has multiple fields that only make sense some of the time. E.g. the `name` can be empty if it's an RPITIT associated type. It's clearer and less error prone if these fields are moved to the relevant `kind` variants. r? ``@fee1-dead``
2 parents efcf4f9 + 78599d8 commit 13cd525

File tree

86 files changed

+609
-546
lines changed

Some content is hidden

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

86 files changed

+609
-546
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

+6-6
Original file line numberDiff line numberDiff line change
@@ -443,13 +443,13 @@ 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);
450450
}
451451
}
452-
ty::AssocKind::Type => {}
452+
ty::AssocKind::Type { .. } => {}
453453
}
454454
}
455455

@@ -740,7 +740,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
740740

741741
for &assoc_item in assoc_items.in_definition_order() {
742742
match assoc_item.kind {
743-
ty::AssocKind::Type if assoc_item.defaultness(tcx).has_value() => {
743+
ty::AssocKind::Type { .. } if assoc_item.defaultness(tcx).has_value() => {
744744
let trait_args = GenericArgs::identity_for_item(tcx, def_id);
745745
let _: Result<_, rustc_errors::ErrorGuaranteed> = check_type_bounds(
746746
tcx,
@@ -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,
@@ -952,8 +952,8 @@ fn check_impl_items_against_trait<'tcx>(
952952
.instantiate_identity(),
953953
);
954954
}
955-
ty::AssocKind::Const => {}
956-
ty::AssocKind::Type => {}
955+
ty::AssocKind::Const { .. } => {}
956+
ty::AssocKind::Type { .. } => {}
957957
}
958958
}
959959

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+33-28
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ 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),
47-
ty::AssocKind::Type => compare_impl_ty(tcx, impl_item, trait_item, impl_trait_ref),
48-
ty::AssocKind::Const => compare_impl_const(tcx, impl_item, trait_item, impl_trait_ref),
46+
ty::AssocKind::Fn { .. } => compare_impl_method(tcx, impl_item, trait_item, impl_trait_ref),
47+
ty::AssocKind::Type { .. } => compare_impl_ty(tcx, impl_item, trait_item, impl_trait_ref),
48+
ty::AssocKind::Const { .. } => {
49+
compare_impl_const(tcx, impl_item, trait_item, impl_trait_ref)
50+
}
4951
}
5052
}
5153

@@ -654,7 +656,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
654656
cause.span,
655657
E0053,
656658
"method `{}` has an incompatible return type for trait",
657-
trait_m.name
659+
trait_m.name()
658660
);
659661
infcx.err_ctxt().note_type_err(
660662
&mut diag,
@@ -1032,11 +1034,11 @@ fn report_trait_method_mismatch<'tcx>(
10321034
impl_err_span,
10331035
E0053,
10341036
"method `{}` has an incompatible type for trait",
1035-
trait_m.name
1037+
trait_m.name()
10361038
);
10371039
match &terr {
10381040
TypeError::ArgumentMutability(0) | TypeError::ArgumentSorts(_, 0)
1039-
if trait_m.fn_has_self_parameter =>
1041+
if trait_m.is_method() =>
10401042
{
10411043
let ty = trait_sig.inputs()[0];
10421044
let sugg = get_self_string(ty, |ty| ty == impl_trait_ref.self_ty());
@@ -1255,7 +1257,7 @@ fn compare_self_type<'tcx>(
12551257
get_self_string(self_arg_ty, can_eq_self)
12561258
};
12571259

1258-
match (trait_m.fn_has_self_parameter, impl_m.fn_has_self_parameter) {
1260+
match (trait_m.is_method(), impl_m.is_method()) {
12591261
(false, false) | (true, true) => {}
12601262

12611263
(false, true) => {
@@ -1266,14 +1268,14 @@ fn compare_self_type<'tcx>(
12661268
impl_m_span,
12671269
E0185,
12681270
"method `{}` has a `{}` declaration in the impl, but not in the trait",
1269-
trait_m.name,
1271+
trait_m.name(),
12701272
self_descr
12711273
);
12721274
err.span_label(impl_m_span, format!("`{self_descr}` used in impl"));
12731275
if let Some(span) = tcx.hir_span_if_local(trait_m.def_id) {
12741276
err.span_label(span, format!("trait method declared without `{self_descr}`"));
12751277
} else {
1276-
err.note_trait_signature(trait_m.name, trait_m.signature(tcx));
1278+
err.note_trait_signature(trait_m.name(), trait_m.signature(tcx));
12771279
}
12781280
return Err(err.emit_unless(delay));
12791281
}
@@ -1286,14 +1288,14 @@ fn compare_self_type<'tcx>(
12861288
impl_m_span,
12871289
E0186,
12881290
"method `{}` has a `{}` declaration in the trait, but not in the impl",
1289-
trait_m.name,
1291+
trait_m.name(),
12901292
self_descr
12911293
);
12921294
err.span_label(impl_m_span, format!("expected `{self_descr}` in impl"));
12931295
if let Some(span) = tcx.hir_span_if_local(trait_m.def_id) {
12941296
err.span_label(span, format!("`{self_descr}` used in trait"));
12951297
} else {
1296-
err.note_trait_signature(trait_m.name, trait_m.signature(tcx));
1298+
err.note_trait_signature(trait_m.name(), trait_m.signature(tcx));
12971299
}
12981300

12991301
return Err(err.emit_unless(delay));
@@ -1363,7 +1365,7 @@ fn compare_number_of_generics<'tcx>(
13631365
let mut err_occurred = None;
13641366
for (kind, trait_count, impl_count) in matchings {
13651367
if impl_count != trait_count {
1366-
let arg_spans = |kind: ty::AssocKind, generics: &hir::Generics<'_>| {
1368+
let arg_spans = |item: &ty::AssocItem, generics: &hir::Generics<'_>| {
13671369
let mut spans = generics
13681370
.params
13691371
.iter()
@@ -1373,7 +1375,7 @@ fn compare_number_of_generics<'tcx>(
13731375
} => {
13741376
// A fn can have an arbitrary number of extra elided lifetimes for the
13751377
// same signature.
1376-
!matches!(kind, ty::AssocKind::Fn)
1378+
!item.is_fn()
13771379
}
13781380
_ => true,
13791381
})
@@ -1386,7 +1388,7 @@ fn compare_number_of_generics<'tcx>(
13861388
};
13871389
let (trait_spans, impl_trait_spans) = if let Some(def_id) = trait_.def_id.as_local() {
13881390
let trait_item = tcx.hir_expect_trait_item(def_id);
1389-
let arg_spans: Vec<Span> = arg_spans(trait_.kind, trait_item.generics);
1391+
let arg_spans: Vec<Span> = arg_spans(&trait_, trait_item.generics);
13901392
let impl_trait_spans: Vec<Span> = trait_item
13911393
.generics
13921394
.params
@@ -1412,7 +1414,7 @@ fn compare_number_of_generics<'tcx>(
14121414
_ => None,
14131415
})
14141416
.collect();
1415-
let spans = arg_spans(impl_.kind, impl_item.generics);
1417+
let spans = arg_spans(&impl_, impl_item.generics);
14161418
let span = spans.first().copied();
14171419

14181420
let mut err = tcx.dcx().struct_span_err(
@@ -1421,7 +1423,7 @@ fn compare_number_of_generics<'tcx>(
14211423
"{} `{}` has {} {kind} parameter{} but its trait \
14221424
declaration has {} {kind} parameter{}",
14231425
item_kind,
1424-
trait_.name,
1426+
trait_.name(),
14251427
impl_count,
14261428
pluralize!(impl_count),
14271429
trait_count,
@@ -1512,7 +1514,7 @@ fn compare_number_of_method_arguments<'tcx>(
15121514
impl_span,
15131515
E0050,
15141516
"method `{}` has {} but the declaration in trait `{}` has {}",
1515-
trait_m.name,
1517+
trait_m.name(),
15161518
potentially_plural_count(impl_number_args, "parameter"),
15171519
tcx.def_path_str(trait_m.def_id),
15181520
trait_number_args
@@ -1527,7 +1529,7 @@ fn compare_number_of_method_arguments<'tcx>(
15271529
),
15281530
);
15291531
} else {
1530-
err.note_trait_signature(trait_m.name, trait_m.signature(tcx));
1532+
err.note_trait_signature(trait_m.name(), trait_m.signature(tcx));
15311533
}
15321534

15331535
err.span_label(
@@ -1581,7 +1583,7 @@ fn compare_synthetic_generics<'tcx>(
15811583
impl_span,
15821584
E0643,
15831585
"method `{}` has incompatible signature for trait",
1584-
trait_m.name
1586+
trait_m.name()
15851587
);
15861588
err.span_label(trait_span, "declaration in trait here");
15871589
if impl_synthetic {
@@ -1703,7 +1705,7 @@ fn compare_generic_param_kinds<'tcx>(
17031705
trait_item: ty::AssocItem,
17041706
delay: bool,
17051707
) -> Result<(), ErrorGuaranteed> {
1706-
assert_eq!(impl_item.kind, trait_item.kind);
1708+
assert_eq!(impl_item.as_tag(), trait_item.as_tag());
17071709

17081710
let ty_const_params_of = |def_id| {
17091711
tcx.generics_of(def_id).own_params.iter().filter(|param| {
@@ -1741,7 +1743,7 @@ fn compare_generic_param_kinds<'tcx>(
17411743
E0053,
17421744
"{} `{}` has an incompatible generic parameter for trait `{}`",
17431745
impl_item.descr(),
1744-
trait_item.name,
1746+
trait_item.name(),
17451747
&tcx.def_path_str(tcx.parent(trait_item.def_id))
17461748
);
17471749

@@ -1877,7 +1879,7 @@ fn compare_const_predicate_entailment<'tcx>(
18771879
cause.span,
18781880
E0326,
18791881
"implemented const `{}` has an incompatible type for trait",
1880-
trait_ct.name
1882+
trait_ct.name()
18811883
);
18821884

18831885
let trait_c_span = trait_ct.def_id.as_local().map(|trait_ct_def_id| {
@@ -2235,16 +2237,19 @@ fn param_env_with_gat_bounds<'tcx>(
22352237
// of the RPITITs associated with the same body. This is because checking
22362238
// the item bounds of RPITITs often involves nested RPITITs having to prove
22372239
// bounds about themselves.
2238-
let impl_tys_to_install = match impl_ty.opt_rpitit_info {
2239-
None => vec![impl_ty],
2240-
Some(
2241-
ty::ImplTraitInTraitData::Impl { fn_def_id }
2242-
| ty::ImplTraitInTraitData::Trait { fn_def_id, .. },
2243-
) => tcx
2240+
let impl_tys_to_install = match impl_ty.kind {
2241+
ty::AssocKind::Type {
2242+
data:
2243+
ty::AssocTypeData::Rpitit(
2244+
ty::ImplTraitInTraitData::Impl { fn_def_id }
2245+
| ty::ImplTraitInTraitData::Trait { fn_def_id, .. },
2246+
),
2247+
} => tcx
22442248
.associated_types_for_impl_traits_in_associated_fn(fn_def_id)
22452249
.iter()
22462250
.map(|def_id| tcx.associated_item(*def_id))
22472251
.collect(),
2252+
_ => vec![impl_ty],
22482253
};
22492254

22502255
for impl_ty in impl_tys_to_install {

compiler/rustc_hir_analysis/src/check/mod.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ fn missing_items_err(
205205

206206
let missing_items_msg = missing_items
207207
.clone()
208-
.map(|trait_item| trait_item.name.to_string())
208+
.map(|trait_item| trait_item.name().to_string())
209209
.collect::<Vec<_>>()
210210
.join("`, `");
211211

@@ -236,7 +236,7 @@ fn missing_items_err(
236236
let code = format!("{padding}{snippet}\n{padding}");
237237
if let Some(span) = tcx.hir_span_if_local(trait_item.def_id) {
238238
missing_trait_item_label
239-
.push(errors::MissingTraitItemLabel { span, item: trait_item.name });
239+
.push(errors::MissingTraitItemLabel { span, item: trait_item.name() });
240240
missing_trait_item.push(errors::MissingTraitItemSuggestion {
241241
span: sugg_sp,
242242
code,
@@ -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,
@@ -499,22 +499,22 @@ fn suggestion_signature<'tcx>(
499499
tcx.predicates_of(assoc.def_id).instantiate_own(tcx, args),
500500
assoc,
501501
),
502-
ty::AssocKind::Type => {
502+
ty::AssocKind::Type { .. } => {
503503
let (generics, where_clauses) = bounds_from_generic_predicates(
504504
tcx,
505505
tcx.predicates_of(assoc.def_id).instantiate_own(tcx, args),
506506
);
507-
format!("type {}{generics} = /* Type */{where_clauses};", assoc.name)
507+
format!("type {}{generics} = /* Type */{where_clauses};", assoc.name())
508508
}
509-
ty::AssocKind::Const => {
509+
ty::AssocKind::Const { name } => {
510510
let ty = tcx.type_of(assoc.def_id).instantiate_identity();
511511
let val = tcx
512512
.infer_ctxt()
513513
.build(TypingMode::non_body_analysis())
514514
.err_ctxt()
515515
.ty_kind_suggestion(tcx.param_env(assoc.def_id), ty)
516516
.unwrap_or_else(|| "value".to_string());
517-
format!("const {}: {} = {};", assoc.name, ty, val)
517+
format!("const {}: {} = {};", name, ty, val)
518518
}
519519
}
520520
}

0 commit comments

Comments
 (0)