Skip to content

Commit 8cdb3cd

Browse files
committed
Auto merge of #93095 - Aaron1011:remove-assoc-ident, r=cjgillot
Store a `Symbol` instead of an `Ident` in `AssocItem` This is the same idea as #92533, but for `AssocItem` instead of `VariantDef`/`FieldDef`. With this change, we no longer have any uses of `#[stable_hasher(project(...))]`
2 parents 92ed874 + c8941d3 commit 8cdb3cd

File tree

28 files changed

+100
-92
lines changed

28 files changed

+100
-92
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
798798
.map(|assoc_items| {
799799
assoc_items
800800
.in_definition_order()
801-
.map(|assoc_item_def| assoc_item_def.ident)
801+
.map(|assoc_item_def| assoc_item_def.ident(self.infcx.tcx))
802802
.filter(|&ident| {
803803
let original_method_ident = path_segment.ident;
804804
original_method_ident != ident

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2650,7 +2650,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
26502650
infer::LateBoundRegion(_, br, infer::AssocTypeProjection(def_id)) => format!(
26512651
" for lifetime parameter {}in trait containing associated type `{}`",
26522652
br_string(br),
2653-
self.tcx.associated_item(def_id).ident
2653+
self.tcx.associated_item(def_id).name
26542654
),
26552655
infer::EarlyBoundRegion(_, name) => format!(" for lifetime parameter `{}`", name),
26562656
infer::UpvarRegion(ref upvar_id, _) => {

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
7070
.map(|s| format!("`{}`", s))
7171
.unwrap_or_else(|| "`fn` parameter".to_string()),
7272
lifetime,
73-
ctxt.assoc_item.ident,
73+
ctxt.assoc_item.name,
7474
);
7575
err.span_label(param.param_ty_span, &format!("this data with {}...", lifetime));
7676
err.span_label(
@@ -231,7 +231,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
231231
// Handle case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a
232232
// `'static` lifetime when called as a method on a binding: `bar.qux()`.
233233
if self.find_impl_on_dyn_trait(&mut err, param.param_ty, &ctxt) {
234-
override_error_code = Some(ctxt.assoc_item.ident);
234+
override_error_code = Some(ctxt.assoc_item.name);
235235
}
236236
}
237237
}
@@ -252,7 +252,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
252252
self.get_impl_ident_and_self_ty_from_trait(*item_def_id, &v.0)
253253
{
254254
if self.suggest_constrain_dyn_trait_in_impl(&mut err, &v.0, ident, self_ty) {
255-
override_error_code = Some(ident);
255+
override_error_code = Some(ident.name);
256256
}
257257
}
258258
}

compiler/rustc_metadata/src/rmeta/decoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
12791279
};
12801280

12811281
ty::AssocItem {
1282-
ident,
1282+
name: ident.name,
12831283
kind,
12841284
vis: self.get_visibility(id),
12851285
defaultness: container.defaultness(),

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12911291
record!(self.tables.kind[def_id] <- EntryKind::AssocType(container));
12921292
}
12931293
}
1294-
self.encode_ident_span(def_id, impl_item.ident);
1294+
self.encode_ident_span(def_id, impl_item.ident(self.tcx));
12951295
self.encode_item_type(def_id);
12961296
if let Some(trait_item_def_id) = impl_item.trait_item_def_id {
12971297
record!(self.tables.trait_item_def_id[def_id] <- trait_item_def_id);

compiler/rustc_middle/src/ty/assoc.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ impl AssocItemContainer {
4444
#[derive(Copy, Clone, Debug, PartialEq, HashStable, Eq, Hash)]
4545
pub struct AssocItem {
4646
pub def_id: DefId,
47-
#[stable_hasher(project(name))]
48-
pub ident: Ident,
47+
pub name: Symbol,
4948
pub kind: AssocKind,
5049
pub vis: Visibility,
5150
pub defaultness: hir::Defaultness,
@@ -61,6 +60,10 @@ pub struct AssocItem {
6160
}
6261

6362
impl AssocItem {
63+
pub fn ident(&self, tcx: TyCtxt<'_>) -> Ident {
64+
Ident::new(self.name, tcx.def_ident_span(self.def_id).unwrap())
65+
}
66+
6467
pub fn signature(&self, tcx: TyCtxt<'_>) -> String {
6568
match self.kind {
6669
ty::AssocKind::Fn => {
@@ -70,9 +73,9 @@ impl AssocItem {
7073
// regions just fine, showing `fn(&MyType)`.
7174
tcx.fn_sig(self.def_id).skip_binder().to_string()
7275
}
73-
ty::AssocKind::Type => format!("type {};", self.ident),
76+
ty::AssocKind::Type => format!("type {};", self.name),
7477
ty::AssocKind::Const => {
75-
format!("const {}: {:?};", self.ident, tcx.type_of(self.def_id))
78+
format!("const {}: {:?};", self.name, tcx.type_of(self.def_id))
7679
}
7780
}
7881
}
@@ -115,7 +118,7 @@ pub struct AssocItems<'tcx> {
115118
impl<'tcx> AssocItems<'tcx> {
116119
/// Constructs an `AssociatedItems` map from a series of `ty::AssocItem`s in definition order.
117120
pub fn new(items_in_def_order: impl IntoIterator<Item = &'tcx ty::AssocItem>) -> Self {
118-
let items = items_in_def_order.into_iter().map(|item| (item.ident.name, item)).collect();
121+
let items = items_in_def_order.into_iter().map(|item| (item.name, item)).collect();
119122
AssocItems { items }
120123
}
121124

@@ -149,7 +152,7 @@ impl<'tcx> AssocItems<'tcx> {
149152
) -> Option<&ty::AssocItem> {
150153
self.filter_by_name_unhygienic(ident.name)
151154
.filter(|item| item.kind == kind)
152-
.find(|item| tcx.hygienic_eq(ident, item.ident, parent_def_id))
155+
.find(|item| tcx.hygienic_eq(ident, item.ident(tcx), parent_def_id))
153156
}
154157

155158
/// Returns the associated item with the given name and any of `AssocKind`, if one exists.
@@ -162,7 +165,7 @@ impl<'tcx> AssocItems<'tcx> {
162165
) -> Option<&ty::AssocItem> {
163166
self.filter_by_name_unhygienic(ident.name)
164167
.filter(|item| kinds.contains(&item.kind))
165-
.find(|item| tcx.hygienic_eq(ident, item.ident, parent_def_id))
168+
.find(|item| tcx.hygienic_eq(ident, item.ident(tcx), parent_def_id))
166169
}
167170

168171
/// Returns the associated item with the given name in the given `Namespace`, if one exists.
@@ -175,6 +178,6 @@ impl<'tcx> AssocItems<'tcx> {
175178
) -> Option<&ty::AssocItem> {
176179
self.filter_by_name_unhygienic(ident.name)
177180
.filter(|item| item.kind.namespace() == ns)
178-
.find(|item| tcx.hygienic_eq(ident, item.ident, parent_def_id))
181+
.find(|item| tcx.hygienic_eq(ident, item.ident(tcx), parent_def_id))
179182
}
180183
}

compiler/rustc_middle/src/ty/error.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -972,10 +972,10 @@ fn foo(&self) -> Self::T { String::new() }
972972
let (span, sugg) = if has_params {
973973
let pos = span.hi() - BytePos(1);
974974
let span = Span::new(pos, pos, span.ctxt(), span.parent());
975-
(span, format!(", {} = {}", assoc.ident, ty))
975+
(span, format!(", {} = {}", assoc.ident(self), ty))
976976
} else {
977977
let item_args = self.format_generic_args(assoc_substs);
978-
(span.shrink_to_hi(), format!("<{}{} = {}>", assoc.ident, item_args, ty))
978+
(span.shrink_to_hi(), format!("<{}{} = {}>", assoc.ident(self), item_args, ty))
979979
};
980980
db.span_suggestion_verbose(span, msg, sugg, MaybeIncorrect);
981981
return true;

compiler/rustc_middle/src/ty/print/pretty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ pub trait PrettyPrinter<'tcx>:
908908
if !first {
909909
p!(", ");
910910
}
911-
p!(write("{} = ", self.tcx().associated_item(assoc_item_def_id).ident));
911+
p!(write("{} = ", self.tcx().associated_item(assoc_item_def_id).name));
912912

913913
match term.skip_binder() {
914914
Term::Ty(ty) => {
@@ -2455,7 +2455,7 @@ define_print_and_forward_display! {
24552455
}
24562456

24572457
ty::ExistentialProjection<'tcx> {
2458-
let name = cx.tcx().associated_item(self.item_def_id).ident;
2458+
let name = cx.tcx().associated_item(self.item_def_id).name;
24592459
p!(write("{} = ", name), print(self.term))
24602460
}
24612461

compiler/rustc_symbol_mangling/src/v0.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
556556
cx = cx.print_def_path(trait_ref.def_id, trait_ref.substs)?;
557557
}
558558
ty::ExistentialPredicate::Projection(projection) => {
559-
let name = cx.tcx.associated_item(projection.item_def_id).ident;
559+
let name = cx.tcx.associated_item(projection.item_def_id).name;
560560
cx.push("p");
561561
cx.push_ident(name.as_str());
562562
cx = match projection.term {

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1353,14 +1353,15 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
13531353
.map(|id| (trait_assoc_item, id))
13541354
})
13551355
.and_then(|(trait_assoc_item, id)| {
1356+
let trait_assoc_ident = trait_assoc_item.ident(self.tcx);
13561357
self.tcx.find_map_relevant_impl(
13571358
id,
13581359
proj.projection_ty.self_ty(),
13591360
|did| {
13601361
self.tcx
13611362
.associated_items(did)
13621363
.in_definition_order()
1363-
.filter(|assoc| assoc.ident == trait_assoc_item.ident)
1364+
.filter(|assoc| assoc.ident(self.tcx) == trait_assoc_ident)
13641365
.next()
13651366
},
13661367
)

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
13671367
err.span_suggestion(
13681368
span,
13691369
"use the fully qualified path to an implementation",
1370-
format!("<Type as {}>::{}", self.tcx.def_path_str(trait_ref), assoc_item.ident),
1370+
format!("<Type as {}>::{}", self.tcx.def_path_str(trait_ref), assoc_item.name),
13711371
Applicability::HasPlaceholders,
13721372
);
13731373
}

compiler/rustc_trait_selection/src/traits/object_safety.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ fn object_safety_violations_for_trait(
8989
.filter(|item| item.kind == ty::AssocKind::Fn)
9090
.filter_map(|item| {
9191
object_safety_violation_for_method(tcx, trait_def_id, &item)
92-
.map(|(code, span)| ObjectSafetyViolation::Method(item.ident.name, code, span))
92+
.map(|(code, span)| ObjectSafetyViolation::Method(item.name, code, span))
9393
})
9494
.filter(|violation| {
9595
if let ObjectSafetyViolation::Method(
@@ -125,15 +125,21 @@ fn object_safety_violations_for_trait(
125125
tcx.associated_items(trait_def_id)
126126
.in_definition_order()
127127
.filter(|item| item.kind == ty::AssocKind::Const)
128-
.map(|item| ObjectSafetyViolation::AssocConst(item.ident.name, item.ident.span)),
128+
.map(|item| {
129+
let ident = item.ident(tcx);
130+
ObjectSafetyViolation::AssocConst(ident.name, ident.span)
131+
}),
129132
);
130133

131134
violations.extend(
132135
tcx.associated_items(trait_def_id)
133136
.in_definition_order()
134137
.filter(|item| item.kind == ty::AssocKind::Type)
135138
.filter(|item| !tcx.generics_of(item.def_id).params.is_empty())
136-
.map(|item| ObjectSafetyViolation::GAT(item.ident.name, item.ident.span)),
139+
.map(|item| {
140+
let ident = item.ident(tcx);
141+
ObjectSafetyViolation::GAT(ident.name, ident.span)
142+
}),
137143
);
138144

139145
debug!(
@@ -367,15 +373,15 @@ fn object_safety_violation_for_method(
367373
(MethodViolationCode::ReferencesSelfInput(arg), Some(node)) => node
368374
.fn_decl()
369375
.and_then(|decl| decl.inputs.get(arg + 1))
370-
.map_or(method.ident.span, |arg| arg.span),
376+
.map_or(method.ident(tcx).span, |arg| arg.span),
371377
(MethodViolationCode::UndispatchableReceiver, Some(node)) => node
372378
.fn_decl()
373379
.and_then(|decl| decl.inputs.get(0))
374-
.map_or(method.ident.span, |arg| arg.span),
380+
.map_or(method.ident(tcx).span, |arg| arg.span),
375381
(MethodViolationCode::ReferencesSelfOutput, Some(node)) => {
376-
node.fn_decl().map_or(method.ident.span, |decl| decl.output.span())
382+
node.fn_decl().map_or(method.ident(tcx).span, |decl| decl.output.span())
377383
}
378-
_ => method.ident.span,
384+
_ => method.ident(tcx).span,
379385
};
380386
(v, span)
381387
})
@@ -404,10 +410,10 @@ fn virtual_call_violation_for_method<'tcx>(
404410
);
405411
// Get the span pointing at where the `self` receiver should be.
406412
let sm = tcx.sess.source_map();
407-
let self_span = method.ident.span.to(tcx
413+
let self_span = method.ident(tcx).span.to(tcx
408414
.hir()
409415
.span_if_local(method.def_id)
410-
.unwrap_or_else(|| sm.next_point(method.ident.span))
416+
.unwrap_or_else(|| sm.next_point(method.ident(tcx).span))
411417
.shrink_to_hi());
412418
let self_span = sm.span_through_char(self_span, '(').shrink_to_hi();
413419
return Some(MethodViolationCode::StaticMethod(

compiler/rustc_trait_selection/src/traits/project.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1600,7 +1600,7 @@ fn confirm_generator_candidate<'cx, 'tcx>(
16001600
gen_sig,
16011601
)
16021602
.map_bound(|(trait_ref, yield_ty, return_ty)| {
1603-
let name = tcx.associated_item(obligation.predicate.item_def_id).ident.name;
1603+
let name = tcx.associated_item(obligation.predicate.item_def_id).name;
16041604
let ty = if name == sym::Return {
16051605
return_ty
16061606
} else if name == sym::Yield {
@@ -1842,7 +1842,7 @@ fn confirm_impl_candidate<'cx, 'tcx>(
18421842
// just return Error.
18431843
debug!(
18441844
"confirm_impl_candidate: no associated type {:?} for {:?}",
1845-
assoc_ty.item.ident, obligation.predicate
1845+
assoc_ty.item.name, obligation.predicate
18461846
);
18471847
return Progress { ty: tcx.ty_error(), obligations: nested };
18481848
}

compiler/rustc_ty_utils/src/assoc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fn associated_item_from_trait_item_ref(
100100
};
101101

102102
ty::AssocItem {
103-
ident: trait_item_ref.ident,
103+
name: trait_item_ref.ident.name,
104104
kind,
105105
vis: tcx.visibility(def_id),
106106
defaultness: trait_item_ref.defaultness,
@@ -124,7 +124,7 @@ fn associated_item_from_impl_item_ref(
124124
};
125125

126126
ty::AssocItem {
127-
ident: impl_item_ref.ident,
127+
name: impl_item_ref.ident.name,
128128
kind,
129129
vis: tcx.visibility(def_id),
130130
defaultness: impl_item_ref.defaultness,

compiler/rustc_typeck/src/astconv/errors.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
214214
.map(|r| self.tcx().associated_items(r.def_id()).in_definition_order())
215215
.flatten()
216216
.filter_map(
217-
|item| if item.kind == ty::AssocKind::Type { Some(item.ident.name) } else { None },
217+
|item| if item.kind == ty::AssocKind::Type { Some(item.name) } else { None },
218218
)
219219
.collect();
220220

@@ -270,7 +270,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
270270
let trait_def_id = assoc_item.container.id();
271271
names.push(format!(
272272
"`{}` (from trait `{}`)",
273-
assoc_item.ident,
273+
assoc_item.name,
274274
tcx.def_path_str(trait_def_id),
275275
));
276276
}
@@ -327,19 +327,19 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
327327
let mut names: FxHashMap<_, usize> = FxHashMap::default();
328328
for item in assoc_items {
329329
types_count += 1;
330-
*names.entry(item.ident.name).or_insert(0) += 1;
330+
*names.entry(item.name).or_insert(0) += 1;
331331
}
332332
let mut dupes = false;
333333
for item in assoc_items {
334-
let prefix = if names[&item.ident.name] > 1 {
334+
let prefix = if names[&item.name] > 1 {
335335
let trait_def_id = item.container.id();
336336
dupes = true;
337337
format!("{}::", tcx.def_path_str(trait_def_id))
338338
} else {
339339
String::new()
340340
};
341341
if let Some(sp) = tcx.hir().span_if_local(item.def_id) {
342-
err.span_label(sp, format!("`{}{}` defined here", prefix, item.ident));
342+
err.span_label(sp, format!("`{}{}` defined here", prefix, item.name));
343343
}
344344
}
345345
if potential_assoc_types.len() == assoc_items.len() {
@@ -350,14 +350,14 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
350350
// `Iterator<Item = isize>`.
351351
for (potential, item) in iter::zip(&potential_assoc_types, assoc_items) {
352352
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(*potential) {
353-
suggestions.push((*potential, format!("{} = {}", item.ident, snippet)));
353+
suggestions.push((*potential, format!("{} = {}", item.name, snippet)));
354354
}
355355
}
356356
} else if let (Ok(snippet), false) =
357357
(tcx.sess.source_map().span_to_snippet(*span), dupes)
358358
{
359359
let types: Vec<_> =
360-
assoc_items.iter().map(|item| format!("{} = Type", item.ident)).collect();
360+
assoc_items.iter().map(|item| format!("{} = Type", item.name)).collect();
361361
let code = if snippet.ends_with('>') {
362362
// The user wrote `Trait<'a>` or similar and we don't have a type we can
363363
// suggest, but at least we can clue them to the correct syntax
@@ -388,17 +388,17 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
388388
let mut names: FxHashMap<_, usize> = FxHashMap::default();
389389
for item in assoc_items {
390390
types_count += 1;
391-
*names.entry(item.ident.name).or_insert(0) += 1;
391+
*names.entry(item.name).or_insert(0) += 1;
392392
}
393393
let mut label = vec![];
394394
for item in assoc_items {
395-
let postfix = if names[&item.ident.name] > 1 {
395+
let postfix = if names[&item.name] > 1 {
396396
let trait_def_id = item.container.id();
397397
format!(" (from trait `{}`)", tcx.def_path_str(trait_def_id))
398398
} else {
399399
String::new()
400400
};
401-
label.push(format!("`{}`{}", item.ident, postfix));
401+
label.push(format!("`{}`{}", item.name, postfix));
402402
}
403403
if !label.is_empty() {
404404
err.span_label(

0 commit comments

Comments
 (0)