Skip to content

Commit 074b518

Browse files
authored
Unrolled build for #139669
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 58c2dd9 + 78599d8 commit 074b518

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

Diff for: 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
}

Diff for: 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;

Diff for: 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();

Diff for: 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

Diff for: 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

@@ -651,7 +653,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
651653
cause.span,
652654
E0053,
653655
"method `{}` has an incompatible return type for trait",
654-
trait_m.name
656+
trait_m.name()
655657
);
656658
infcx.err_ctxt().note_type_err(
657659
&mut diag,
@@ -1029,11 +1031,11 @@ fn report_trait_method_mismatch<'tcx>(
10291031
impl_err_span,
10301032
E0053,
10311033
"method `{}` has an incompatible type for trait",
1032-
trait_m.name
1034+
trait_m.name()
10331035
);
10341036
match &terr {
10351037
TypeError::ArgumentMutability(0) | TypeError::ArgumentSorts(_, 0)
1036-
if trait_m.fn_has_self_parameter =>
1038+
if trait_m.is_method() =>
10371039
{
10381040
let ty = trait_sig.inputs()[0];
10391041
let sugg = get_self_string(ty, |ty| ty == impl_trait_ref.self_ty());
@@ -1252,7 +1254,7 @@ fn compare_self_type<'tcx>(
12521254
get_self_string(self_arg_ty, can_eq_self)
12531255
};
12541256

1255-
match (trait_m.fn_has_self_parameter, impl_m.fn_has_self_parameter) {
1257+
match (trait_m.is_method(), impl_m.is_method()) {
12561258
(false, false) | (true, true) => {}
12571259

12581260
(false, true) => {
@@ -1263,14 +1265,14 @@ fn compare_self_type<'tcx>(
12631265
impl_m_span,
12641266
E0185,
12651267
"method `{}` has a `{}` declaration in the impl, but not in the trait",
1266-
trait_m.name,
1268+
trait_m.name(),
12671269
self_descr
12681270
);
12691271
err.span_label(impl_m_span, format!("`{self_descr}` used in impl"));
12701272
if let Some(span) = tcx.hir_span_if_local(trait_m.def_id) {
12711273
err.span_label(span, format!("trait method declared without `{self_descr}`"));
12721274
} else {
1273-
err.note_trait_signature(trait_m.name, trait_m.signature(tcx));
1275+
err.note_trait_signature(trait_m.name(), trait_m.signature(tcx));
12741276
}
12751277
return Err(err.emit_unless(delay));
12761278
}
@@ -1283,14 +1285,14 @@ fn compare_self_type<'tcx>(
12831285
impl_m_span,
12841286
E0186,
12851287
"method `{}` has a `{}` declaration in the trait, but not in the impl",
1286-
trait_m.name,
1288+
trait_m.name(),
12871289
self_descr
12881290
);
12891291
err.span_label(impl_m_span, format!("expected `{self_descr}` in impl"));
12901292
if let Some(span) = tcx.hir_span_if_local(trait_m.def_id) {
12911293
err.span_label(span, format!("`{self_descr}` used in trait"));
12921294
} else {
1293-
err.note_trait_signature(trait_m.name, trait_m.signature(tcx));
1295+
err.note_trait_signature(trait_m.name(), trait_m.signature(tcx));
12941296
}
12951297

12961298
return Err(err.emit_unless(delay));
@@ -1360,7 +1362,7 @@ fn compare_number_of_generics<'tcx>(
13601362
let mut err_occurred = None;
13611363
for (kind, trait_count, impl_count) in matchings {
13621364
if impl_count != trait_count {
1363-
let arg_spans = |kind: ty::AssocKind, generics: &hir::Generics<'_>| {
1365+
let arg_spans = |item: &ty::AssocItem, generics: &hir::Generics<'_>| {
13641366
let mut spans = generics
13651367
.params
13661368
.iter()
@@ -1370,7 +1372,7 @@ fn compare_number_of_generics<'tcx>(
13701372
} => {
13711373
// A fn can have an arbitrary number of extra elided lifetimes for the
13721374
// same signature.
1373-
!matches!(kind, ty::AssocKind::Fn)
1375+
!item.is_fn()
13741376
}
13751377
_ => true,
13761378
})
@@ -1383,7 +1385,7 @@ fn compare_number_of_generics<'tcx>(
13831385
};
13841386
let (trait_spans, impl_trait_spans) = if let Some(def_id) = trait_.def_id.as_local() {
13851387
let trait_item = tcx.hir_expect_trait_item(def_id);
1386-
let arg_spans: Vec<Span> = arg_spans(trait_.kind, trait_item.generics);
1388+
let arg_spans: Vec<Span> = arg_spans(&trait_, trait_item.generics);
13871389
let impl_trait_spans: Vec<Span> = trait_item
13881390
.generics
13891391
.params
@@ -1409,7 +1411,7 @@ fn compare_number_of_generics<'tcx>(
14091411
_ => None,
14101412
})
14111413
.collect();
1412-
let spans = arg_spans(impl_.kind, impl_item.generics);
1414+
let spans = arg_spans(&impl_, impl_item.generics);
14131415
let span = spans.first().copied();
14141416

14151417
let mut err = tcx.dcx().struct_span_err(
@@ -1418,7 +1420,7 @@ fn compare_number_of_generics<'tcx>(
14181420
"{} `{}` has {} {kind} parameter{} but its trait \
14191421
declaration has {} {kind} parameter{}",
14201422
item_kind,
1421-
trait_.name,
1423+
trait_.name(),
14221424
impl_count,
14231425
pluralize!(impl_count),
14241426
trait_count,
@@ -1509,7 +1511,7 @@ fn compare_number_of_method_arguments<'tcx>(
15091511
impl_span,
15101512
E0050,
15111513
"method `{}` has {} but the declaration in trait `{}` has {}",
1512-
trait_m.name,
1514+
trait_m.name(),
15131515
potentially_plural_count(impl_number_args, "parameter"),
15141516
tcx.def_path_str(trait_m.def_id),
15151517
trait_number_args
@@ -1524,7 +1526,7 @@ fn compare_number_of_method_arguments<'tcx>(
15241526
),
15251527
);
15261528
} else {
1527-
err.note_trait_signature(trait_m.name, trait_m.signature(tcx));
1529+
err.note_trait_signature(trait_m.name(), trait_m.signature(tcx));
15281530
}
15291531

15301532
err.span_label(
@@ -1578,7 +1580,7 @@ fn compare_synthetic_generics<'tcx>(
15781580
impl_span,
15791581
E0643,
15801582
"method `{}` has incompatible signature for trait",
1581-
trait_m.name
1583+
trait_m.name()
15821584
);
15831585
err.span_label(trait_span, "declaration in trait here");
15841586
if impl_synthetic {
@@ -1700,7 +1702,7 @@ fn compare_generic_param_kinds<'tcx>(
17001702
trait_item: ty::AssocItem,
17011703
delay: bool,
17021704
) -> Result<(), ErrorGuaranteed> {
1703-
assert_eq!(impl_item.kind, trait_item.kind);
1705+
assert_eq!(impl_item.as_tag(), trait_item.as_tag());
17041706

17051707
let ty_const_params_of = |def_id| {
17061708
tcx.generics_of(def_id).own_params.iter().filter(|param| {
@@ -1738,7 +1740,7 @@ fn compare_generic_param_kinds<'tcx>(
17381740
E0053,
17391741
"{} `{}` has an incompatible generic parameter for trait `{}`",
17401742
impl_item.descr(),
1741-
trait_item.name,
1743+
trait_item.name(),
17421744
&tcx.def_path_str(tcx.parent(trait_item.def_id))
17431745
);
17441746

@@ -1874,7 +1876,7 @@ fn compare_const_predicate_entailment<'tcx>(
18741876
cause.span,
18751877
E0326,
18761878
"implemented const `{}` has an incompatible type for trait",
1877-
trait_ct.name
1879+
trait_ct.name()
18781880
);
18791881

18801882
let trait_c_span = trait_ct.def_id.as_local().map(|trait_ct_def_id| {
@@ -2232,16 +2234,19 @@ fn param_env_with_gat_bounds<'tcx>(
22322234
// of the RPITITs associated with the same body. This is because checking
22332235
// the item bounds of RPITITs often involves nested RPITITs having to prove
22342236
// bounds about themselves.
2235-
let impl_tys_to_install = match impl_ty.opt_rpitit_info {
2236-
None => vec![impl_ty],
2237-
Some(
2238-
ty::ImplTraitInTraitData::Impl { fn_def_id }
2239-
| ty::ImplTraitInTraitData::Trait { fn_def_id, .. },
2240-
) => tcx
2237+
let impl_tys_to_install = match impl_ty.kind {
2238+
ty::AssocKind::Type {
2239+
data:
2240+
ty::AssocTypeData::Rpitit(
2241+
ty::ImplTraitInTraitData::Impl { fn_def_id }
2242+
| ty::ImplTraitInTraitData::Trait { fn_def_id, .. },
2243+
),
2244+
} => tcx
22412245
.associated_types_for_impl_traits_in_associated_fn(fn_def_id)
22422246
.iter()
22432247
.map(|def_id| tcx.associated_item(*def_id))
22442248
.collect(),
2249+
_ => vec![impl_ty],
22452250
};
22462251

22472252
for impl_ty in impl_tys_to_install {

Diff for: 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)