Skip to content

Commit 91010b3

Browse files
committed
Move name field from AssocItem to AssocKind variants.
To accurately reflect that RPITIT assoc items don't have a name. This avoids the use of `kw::Empty` to mean "no name", which is error prone. Helps with #137978.
1 parent b8ed2c1 commit 91010b3

File tree

43 files changed

+276
-231
lines changed

Some content is hidden

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

43 files changed

+276
-231
lines changed

Diff for: 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);
@@ -952,7 +952,7 @@ fn check_impl_items_against_trait<'tcx>(
952952
.instantiate_identity(),
953953
);
954954
}
955-
ty::AssocKind::Const => {}
955+
ty::AssocKind::Const { .. } => {}
956956
ty::AssocKind::Type { .. } => {}
957957
}
958958
}

Diff for: compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ pub(super) fn compare_impl_item(
4545
match impl_item.kind {
4646
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),
48-
ty::AssocKind::Const => compare_impl_const(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,7 +1034,7 @@ 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)
@@ -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));
@@ -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 {
@@ -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| {
@@ -2237,8 +2239,8 @@ fn param_env_with_gat_bounds<'tcx>(
22372239
// bounds about themselves.
22382240
let impl_tys_to_install = match impl_ty.kind {
22392241
ty::AssocKind::Type {
2240-
opt_rpitit_info:
2241-
Some(
2242+
data:
2243+
ty::AssocTypeData::Rpitit(
22422244
ty::ImplTraitInTraitData::Impl { fn_def_id }
22432245
| ty::ImplTraitInTraitData::Trait { fn_def_id, .. },
22442246
),

Diff for: compiler/rustc_hir_analysis/src/check/mod.rs

+5-5
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,
@@ -504,17 +504,17 @@ fn suggestion_signature<'tcx>(
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
}

Diff for: compiler/rustc_hir_analysis/src/check/wfcheck.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, trait_def_id: LocalDefId) {
474474
gat_generics,
475475
)
476476
}
477-
ty::AssocKind::Const => None,
477+
ty::AssocKind::Const { .. } => None,
478478
};
479479

480480
if let Some(item_required_bounds) = item_required_bounds {
@@ -1076,7 +1076,7 @@ fn check_associated_item(
10761076
};
10771077

10781078
match item.kind {
1079-
ty::AssocKind::Const => {
1079+
ty::AssocKind::Const { .. } => {
10801080
let ty = tcx.type_of(item.def_id).instantiate_identity();
10811081
let ty = wfcx.normalize(span, Some(WellFormedLoc::Ty(item_id)), ty);
10821082
wfcx.register_wf_obligation(span, loc, ty.into());

Diff for: compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
5151

5252
for &item1 in impl_items1.in_definition_order() {
5353
let collision = impl_items2
54-
.filter_by_name_unhygienic(item1.name)
54+
.filter_by_name_unhygienic(item1.name())
5555
.any(|&item2| self.compare_hygienically(item1, item2));
5656

5757
if collision {
@@ -113,7 +113,7 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
113113
let mut res = Ok(());
114114
for &item1 in impl_items1.in_definition_order() {
115115
let collision = impl_items2
116-
.filter_by_name_unhygienic(item1.name)
116+
.filter_by_name_unhygienic(item1.name())
117117
.find(|&&item2| self.compare_hygienically(item1, item2));
118118

119119
if let Some(item2) = collision {
@@ -230,11 +230,11 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
230230
let mut ids = impl_items
231231
.in_definition_order()
232232
.filter_map(|item| {
233-
let entry = connected_region_ids.entry(item.name);
233+
let entry = connected_region_ids.entry(item.name());
234234
if let IndexEntry::Occupied(e) = &entry {
235235
Some(*e.get())
236236
} else {
237-
idents_to_add.push(item.name);
237+
idents_to_add.push(item.name());
238238
None
239239
}
240240
})

Diff for: compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ 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 { .. } => {
35+
ty::AssocKind::Const { .. } | ty::AssocKind::Fn { .. } => {
3636
locator.check(assoc_id.expect_local())
3737
}
3838
// Associated types don't have bodies, so they can't constrain hidden types

Diff for: compiler/rustc_hir_analysis/src/errors/wrong_number_of_generic_args.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
492492
.gen_args
493493
.constraints
494494
.iter()
495-
.any(|constraint| constraint.ident.name == item.name)
495+
.any(|constraint| constraint.ident.name == item.name())
496496
})
497497
.filter(|item| !item.is_impl_trait_in_trait())
498498
.map(|item| self.tcx.item_ident(item.def_id).to_string())

Diff for: compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+21-17
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
168168
let all_candidate_names: Vec<_> = all_candidates()
169169
.flat_map(|r| tcx.associated_items(r.def_id()).in_definition_order())
170170
.filter_map(|item| {
171-
(!item.is_impl_trait_in_trait() && item.as_tag() == assoc_tag).then_some(item.name)
171+
if !item.is_impl_trait_in_trait() && item.as_tag() == assoc_tag {
172+
item.opt_name()
173+
} else {
174+
None
175+
}
172176
})
173177
.collect();
174178

@@ -200,7 +204,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
200204
.iter()
201205
.flat_map(|trait_def_id| tcx.associated_items(*trait_def_id).in_definition_order())
202206
.filter_map(|item| {
203-
(!item.is_impl_trait_in_trait() && item.as_tag() == assoc_tag).then_some(item.name)
207+
(!item.is_impl_trait_in_trait() && item.as_tag() == assoc_tag)
208+
.then_some(item.name())
204209
})
205210
.collect();
206211

@@ -337,7 +342,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
337342
) -> ErrorGuaranteed {
338343
let tcx = self.tcx();
339344

340-
let bound_on_assoc_const_label = if let ty::AssocKind::Const = assoc_item.kind
345+
let bound_on_assoc_const_label = if let ty::AssocKind::Const { .. } = assoc_item.kind
341346
&& let Some(constraint) = constraint
342347
&& let hir::AssocItemConstraintKind::Bound { .. } = constraint.kind
343348
{
@@ -761,7 +766,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
761766
// `issue-22560.rs`.
762767
let mut dyn_compatibility_violations = Ok(());
763768
for (assoc_item, trait_ref) in &missing_assoc_types {
764-
names.entry(trait_ref).or_default().push(assoc_item.name);
769+
names.entry(trait_ref).or_default().push(assoc_item.name());
765770
names_len += 1;
766771

767772
let violations =
@@ -852,16 +857,17 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
852857
let mut names: UnordMap<_, usize> = Default::default();
853858
for (item, _) in &missing_assoc_types {
854859
types_count += 1;
855-
*names.entry(item.name).or_insert(0) += 1;
860+
*names.entry(item.name()).or_insert(0) += 1;
856861
}
857862
let mut dupes = false;
858863
let mut shadows = false;
859864
for (item, trait_ref) in &missing_assoc_types {
860-
let prefix = if names[&item.name] > 1 {
865+
let name = item.name();
866+
let prefix = if names[&name] > 1 {
861867
let trait_def_id = trait_ref.def_id();
862868
dupes = true;
863869
format!("{}::", tcx.def_path_str(trait_def_id))
864-
} else if bound_names.get(&item.name).is_some_and(|x| *x != item) {
870+
} else if bound_names.get(&name).is_some_and(|x| *x != item) {
865871
let trait_def_id = trait_ref.def_id();
866872
shadows = true;
867873
format!("{}::", tcx.def_path_str(trait_def_id))
@@ -871,7 +877,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
871877

872878
let mut is_shadowed = false;
873879

874-
if let Some(assoc_item) = bound_names.get(&item.name)
880+
if let Some(assoc_item) = bound_names.get(&name)
875881
&& *assoc_item != item
876882
{
877883
is_shadowed = true;
@@ -880,17 +886,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
880886
if assoc_item.def_id.is_local() { ", consider renaming it" } else { "" };
881887
err.span_label(
882888
tcx.def_span(assoc_item.def_id),
883-
format!("`{}{}` shadowed here{}", prefix, item.name, rename_message),
889+
format!("`{}{}` shadowed here{}", prefix, name, rename_message),
884890
);
885891
}
886892

887893
let rename_message = if is_shadowed { ", consider renaming it" } else { "" };
888894

889895
if let Some(sp) = tcx.hir_span_if_local(item.def_id) {
890-
err.span_label(
891-
sp,
892-
format!("`{}{}` defined here{}", prefix, item.name, rename_message),
893-
);
896+
err.span_label(sp, format!("`{}{}` defined here{}", prefix, name, rename_message));
894897
}
895898
}
896899
if potential_assoc_types.len() == missing_assoc_types.len() {
@@ -903,7 +906,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
903906
{
904907
let types: Vec<_> = missing_assoc_types
905908
.iter()
906-
.map(|(item, _)| format!("{} = Type", item.name))
909+
.map(|(item, _)| format!("{} = Type", item.name()))
907910
.collect();
908911
let code = if let Some(snippet) = snippet.strip_suffix('>') {
909912
// The user wrote `Trait<'a>` or similar and we don't have a type we can
@@ -938,16 +941,17 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
938941
let mut names: FxIndexMap<_, usize> = FxIndexMap::default();
939942
for (item, _) in &missing_assoc_types {
940943
types_count += 1;
941-
*names.entry(item.name).or_insert(0) += 1;
944+
*names.entry(item.name()).or_insert(0) += 1;
942945
}
943946
let mut label = vec![];
944947
for (item, trait_ref) in &missing_assoc_types {
945-
let postfix = if names[&item.name] > 1 {
948+
let name = item.name();
949+
let postfix = if names[&name] > 1 {
946950
format!(" (from trait `{}`)", trait_ref.print_trait_sugared())
947951
} else {
948952
String::new()
949953
};
950-
label.push(format!("`{}`{}", item.name, postfix));
954+
label.push(format!("`{}`{}", name, postfix));
951955
}
952956
if !label.is_empty() {
953957
err.span_label(

Diff for: compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
502502
.associated_items(trait_def_id)
503503
.in_definition_order()
504504
.filter(|assoc| assoc.namespace() == Namespace::ValueNS)
505-
.map(|cand| cand.name)
505+
.map(|cand| cand.name())
506506
.collect();
507507
if let Some(typo) = find_best_match_for_name(&names, segment.ident.name, None) {
508508
diag.span_suggestion_verbose(

Diff for: compiler/rustc_hir_analysis/src/impl_wf_check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pub(crate) fn enforce_impl_lifetime_params_are_constrained(
119119
vec![]
120120
}
121121
}
122-
ty::AssocKind::Fn { .. } | ty::AssocKind::Const => vec![],
122+
ty::AssocKind::Fn { .. } | ty::AssocKind::Const { .. } => vec![],
123123
}
124124
})
125125
.collect();

0 commit comments

Comments
 (0)