Skip to content

Commit

Permalink
Store a Symbol instead of an Ident in AssocItem
Browse files Browse the repository at this point in the history
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(...))]`
  • Loading branch information
Aaron1011 committed Jan 19, 2022
1 parent 5e57faa commit c8941d3
Show file tree
Hide file tree
Showing 28 changed files with 100 additions and 92 deletions.
Expand Up @@ -798,7 +798,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
.map(|assoc_items| {
assoc_items
.in_definition_order()
.map(|assoc_item_def| assoc_item_def.ident)
.map(|assoc_item_def| assoc_item_def.ident(self.infcx.tcx))
.filter(|&ident| {
let original_method_ident = path_segment.ident;
original_method_ident != ident
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/infer/error_reporting/mod.rs
Expand Up @@ -2643,7 +2643,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
infer::LateBoundRegion(_, br, infer::AssocTypeProjection(def_id)) => format!(
" for lifetime parameter {}in trait containing associated type `{}`",
br_string(br),
self.tcx.associated_item(def_id).ident
self.tcx.associated_item(def_id).name
),
infer::EarlyBoundRegion(_, name) => format!(" for lifetime parameter `{}`", name),
infer::UpvarRegion(ref upvar_id, _) => {
Expand Down
Expand Up @@ -70,7 +70,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
.map(|s| format!("`{}`", s))
.unwrap_or_else(|| "`fn` parameter".to_string()),
lifetime,
ctxt.assoc_item.ident,
ctxt.assoc_item.name,
);
err.span_label(param.param_ty_span, &format!("this data with {}...", lifetime));
err.span_label(
Expand Down Expand Up @@ -231,7 +231,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
// Handle case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a
// `'static` lifetime when called as a method on a binding: `bar.qux()`.
if self.find_impl_on_dyn_trait(&mut err, param.param_ty, &ctxt) {
override_error_code = Some(ctxt.assoc_item.ident);
override_error_code = Some(ctxt.assoc_item.name);
}
}
}
Expand All @@ -252,7 +252,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
self.get_impl_ident_and_self_ty_from_trait(*item_def_id, &v.0)
{
if self.suggest_constrain_dyn_trait_in_impl(&mut err, &v.0, ident, self_ty) {
override_error_code = Some(ident);
override_error_code = Some(ident.name);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/decoder.rs
Expand Up @@ -1286,7 +1286,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
};

ty::AssocItem {
ident,
name: ident.name,
kind,
vis: self.get_visibility(id),
defaultness: container.defaultness(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/encoder.rs
Expand Up @@ -1291,7 +1291,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
record!(self.tables.kind[def_id] <- EntryKind::AssocType(container));
}
}
self.encode_ident_span(def_id, impl_item.ident);
self.encode_ident_span(def_id, impl_item.ident(self.tcx));
self.encode_item_type(def_id);
if let Some(trait_item_def_id) = impl_item.trait_item_def_id {
record!(self.tables.trait_item_def_id[def_id] <- trait_item_def_id);
Expand Down
19 changes: 11 additions & 8 deletions compiler/rustc_middle/src/ty/assoc.rs
Expand Up @@ -44,8 +44,7 @@ impl AssocItemContainer {
#[derive(Copy, Clone, Debug, PartialEq, HashStable, Eq, Hash)]
pub struct AssocItem {
pub def_id: DefId,
#[stable_hasher(project(name))]
pub ident: Ident,
pub name: Symbol,
pub kind: AssocKind,
pub vis: Visibility,
pub defaultness: hir::Defaultness,
Expand All @@ -61,6 +60,10 @@ pub struct AssocItem {
}

impl AssocItem {
pub fn ident(&self, tcx: TyCtxt<'_>) -> Ident {
Ident::new(self.name, tcx.def_ident_span(self.def_id).unwrap())
}

pub fn signature(&self, tcx: TyCtxt<'_>) -> String {
match self.kind {
ty::AssocKind::Fn => {
Expand All @@ -70,9 +73,9 @@ impl AssocItem {
// regions just fine, showing `fn(&MyType)`.
tcx.fn_sig(self.def_id).skip_binder().to_string()
}
ty::AssocKind::Type => format!("type {};", self.ident),
ty::AssocKind::Type => format!("type {};", self.name),
ty::AssocKind::Const => {
format!("const {}: {:?};", self.ident, tcx.type_of(self.def_id))
format!("const {}: {:?};", self.name, tcx.type_of(self.def_id))
}
}
}
Expand Down Expand Up @@ -115,7 +118,7 @@ pub struct AssocItems<'tcx> {
impl<'tcx> AssocItems<'tcx> {
/// Constructs an `AssociatedItems` map from a series of `ty::AssocItem`s in definition order.
pub fn new(items_in_def_order: impl IntoIterator<Item = &'tcx ty::AssocItem>) -> Self {
let items = items_in_def_order.into_iter().map(|item| (item.ident.name, item)).collect();
let items = items_in_def_order.into_iter().map(|item| (item.name, item)).collect();
AssocItems { items }
}

Expand Down Expand Up @@ -149,7 +152,7 @@ impl<'tcx> AssocItems<'tcx> {
) -> Option<&ty::AssocItem> {
self.filter_by_name_unhygienic(ident.name)
.filter(|item| item.kind == kind)
.find(|item| tcx.hygienic_eq(ident, item.ident, parent_def_id))
.find(|item| tcx.hygienic_eq(ident, item.ident(tcx), parent_def_id))
}

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

/// Returns the associated item with the given name in the given `Namespace`, if one exists.
Expand All @@ -175,6 +178,6 @@ impl<'tcx> AssocItems<'tcx> {
) -> Option<&ty::AssocItem> {
self.filter_by_name_unhygienic(ident.name)
.filter(|item| item.kind.namespace() == ns)
.find(|item| tcx.hygienic_eq(ident, item.ident, parent_def_id))
.find(|item| tcx.hygienic_eq(ident, item.ident(tcx), parent_def_id))
}
}
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/error.rs
Expand Up @@ -972,10 +972,10 @@ fn foo(&self) -> Self::T { String::new() }
let (span, sugg) = if has_params {
let pos = span.hi() - BytePos(1);
let span = Span::new(pos, pos, span.ctxt(), span.parent());
(span, format!(", {} = {}", assoc.ident, ty))
(span, format!(", {} = {}", assoc.ident(self), ty))
} else {
let item_args = self.format_generic_args(assoc_substs);
(span.shrink_to_hi(), format!("<{}{} = {}>", assoc.ident, item_args, ty))
(span.shrink_to_hi(), format!("<{}{} = {}>", assoc.ident(self), item_args, ty))
};
db.span_suggestion_verbose(span, msg, sugg, MaybeIncorrect);
return true;
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Expand Up @@ -908,7 +908,7 @@ pub trait PrettyPrinter<'tcx>:
if !first {
p!(", ");
}
p!(write("{} = ", self.tcx().associated_item(assoc_item_def_id).ident));
p!(write("{} = ", self.tcx().associated_item(assoc_item_def_id).name));

match term.skip_binder() {
Term::Ty(ty) => {
Expand Down Expand Up @@ -2455,7 +2455,7 @@ define_print_and_forward_display! {
}

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

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_symbol_mangling/src/v0.rs
Expand Up @@ -556,7 +556,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
cx = cx.print_def_path(trait_ref.def_id, trait_ref.substs)?;
}
ty::ExistentialPredicate::Projection(projection) => {
let name = cx.tcx.associated_item(projection.item_def_id).ident;
let name = cx.tcx.associated_item(projection.item_def_id).name;
cx.push("p");
cx.push_ident(name.as_str());
cx = match projection.term {
Expand Down
Expand Up @@ -1353,14 +1353,15 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
.map(|id| (trait_assoc_item, id))
})
.and_then(|(trait_assoc_item, id)| {
let trait_assoc_ident = trait_assoc_item.ident(self.tcx);
self.tcx.find_map_relevant_impl(
id,
proj.projection_ty.self_ty(),
|did| {
self.tcx
.associated_items(did)
.in_definition_order()
.filter(|assoc| assoc.ident == trait_assoc_item.ident)
.filter(|assoc| assoc.ident(self.tcx) == trait_assoc_ident)
.next()
},
)
Expand Down
Expand Up @@ -1368,7 +1368,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
err.span_suggestion(
span,
"use the fully qualified path to an implementation",
format!("<Type as {}>::{}", self.tcx.def_path_str(trait_ref), assoc_item.ident),
format!("<Type as {}>::{}", self.tcx.def_path_str(trait_ref), assoc_item.name),
Applicability::HasPlaceholders,
);
}
Expand Down
24 changes: 15 additions & 9 deletions compiler/rustc_trait_selection/src/traits/object_safety.rs
Expand Up @@ -89,7 +89,7 @@ fn object_safety_violations_for_trait(
.filter(|item| item.kind == ty::AssocKind::Fn)
.filter_map(|item| {
object_safety_violation_for_method(tcx, trait_def_id, &item)
.map(|(code, span)| ObjectSafetyViolation::Method(item.ident.name, code, span))
.map(|(code, span)| ObjectSafetyViolation::Method(item.name, code, span))
})
.filter(|violation| {
if let ObjectSafetyViolation::Method(
Expand Down Expand Up @@ -125,15 +125,21 @@ fn object_safety_violations_for_trait(
tcx.associated_items(trait_def_id)
.in_definition_order()
.filter(|item| item.kind == ty::AssocKind::Const)
.map(|item| ObjectSafetyViolation::AssocConst(item.ident.name, item.ident.span)),
.map(|item| {
let ident = item.ident(tcx);
ObjectSafetyViolation::AssocConst(ident.name, ident.span)
}),
);

violations.extend(
tcx.associated_items(trait_def_id)
.in_definition_order()
.filter(|item| item.kind == ty::AssocKind::Type)
.filter(|item| !tcx.generics_of(item.def_id).params.is_empty())
.map(|item| ObjectSafetyViolation::GAT(item.ident.name, item.ident.span)),
.map(|item| {
let ident = item.ident(tcx);
ObjectSafetyViolation::GAT(ident.name, ident.span)
}),
);

debug!(
Expand Down Expand Up @@ -367,15 +373,15 @@ fn object_safety_violation_for_method(
(MethodViolationCode::ReferencesSelfInput(arg), Some(node)) => node
.fn_decl()
.and_then(|decl| decl.inputs.get(arg + 1))
.map_or(method.ident.span, |arg| arg.span),
.map_or(method.ident(tcx).span, |arg| arg.span),
(MethodViolationCode::UndispatchableReceiver, Some(node)) => node
.fn_decl()
.and_then(|decl| decl.inputs.get(0))
.map_or(method.ident.span, |arg| arg.span),
.map_or(method.ident(tcx).span, |arg| arg.span),
(MethodViolationCode::ReferencesSelfOutput, Some(node)) => {
node.fn_decl().map_or(method.ident.span, |decl| decl.output.span())
node.fn_decl().map_or(method.ident(tcx).span, |decl| decl.output.span())
}
_ => method.ident.span,
_ => method.ident(tcx).span,
};
(v, span)
})
Expand Down Expand Up @@ -404,10 +410,10 @@ fn virtual_call_violation_for_method<'tcx>(
);
// Get the span pointing at where the `self` receiver should be.
let sm = tcx.sess.source_map();
let self_span = method.ident.span.to(tcx
let self_span = method.ident(tcx).span.to(tcx
.hir()
.span_if_local(method.def_id)
.unwrap_or_else(|| sm.next_point(method.ident.span))
.unwrap_or_else(|| sm.next_point(method.ident(tcx).span))
.shrink_to_hi());
let self_span = sm.span_through_char(self_span, '(').shrink_to_hi();
return Some(MethodViolationCode::StaticMethod(
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_trait_selection/src/traits/project.rs
Expand Up @@ -1600,7 +1600,7 @@ fn confirm_generator_candidate<'cx, 'tcx>(
gen_sig,
)
.map_bound(|(trait_ref, yield_ty, return_ty)| {
let name = tcx.associated_item(obligation.predicate.item_def_id).ident.name;
let name = tcx.associated_item(obligation.predicate.item_def_id).name;
let ty = if name == sym::Return {
return_ty
} else if name == sym::Yield {
Expand Down Expand Up @@ -1842,7 +1842,7 @@ fn confirm_impl_candidate<'cx, 'tcx>(
// just return Error.
debug!(
"confirm_impl_candidate: no associated type {:?} for {:?}",
assoc_ty.item.ident, obligation.predicate
assoc_ty.item.name, obligation.predicate
);
return Progress { ty: tcx.ty_error(), obligations: nested };
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ty_utils/src/assoc.rs
Expand Up @@ -100,7 +100,7 @@ fn associated_item_from_trait_item_ref(
};

ty::AssocItem {
ident: trait_item_ref.ident,
name: trait_item_ref.ident.name,
kind,
vis: tcx.visibility(def_id),
defaultness: trait_item_ref.defaultness,
Expand All @@ -124,7 +124,7 @@ fn associated_item_from_impl_item_ref(
};

ty::AssocItem {
ident: impl_item_ref.ident,
name: impl_item_ref.ident.name,
kind,
vis: tcx.visibility(def_id),
defaultness: impl_item_ref.defaultness,
Expand Down
20 changes: 10 additions & 10 deletions compiler/rustc_typeck/src/astconv/errors.rs
Expand Up @@ -214,7 +214,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
.map(|r| self.tcx().associated_items(r.def_id()).in_definition_order())
.flatten()
.filter_map(
|item| if item.kind == ty::AssocKind::Type { Some(item.ident.name) } else { None },
|item| if item.kind == ty::AssocKind::Type { Some(item.name) } else { None },
)
.collect();

Expand Down Expand Up @@ -270,7 +270,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let trait_def_id = assoc_item.container.id();
names.push(format!(
"`{}` (from trait `{}`)",
assoc_item.ident,
assoc_item.name,
tcx.def_path_str(trait_def_id),
));
}
Expand Down Expand Up @@ -327,19 +327,19 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let mut names: FxHashMap<_, usize> = FxHashMap::default();
for item in assoc_items {
types_count += 1;
*names.entry(item.ident.name).or_insert(0) += 1;
*names.entry(item.name).or_insert(0) += 1;
}
let mut dupes = false;
for item in assoc_items {
let prefix = if names[&item.ident.name] > 1 {
let prefix = if names[&item.name] > 1 {
let trait_def_id = item.container.id();
dupes = true;
format!("{}::", tcx.def_path_str(trait_def_id))
} else {
String::new()
};
if let Some(sp) = tcx.hir().span_if_local(item.def_id) {
err.span_label(sp, format!("`{}{}` defined here", prefix, item.ident));
err.span_label(sp, format!("`{}{}` defined here", prefix, item.name));
}
}
if potential_assoc_types.len() == assoc_items.len() {
Expand All @@ -350,14 +350,14 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
// `Iterator<Item = isize>`.
for (potential, item) in iter::zip(&potential_assoc_types, assoc_items) {
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(*potential) {
suggestions.push((*potential, format!("{} = {}", item.ident, snippet)));
suggestions.push((*potential, format!("{} = {}", item.name, snippet)));
}
}
} else if let (Ok(snippet), false) =
(tcx.sess.source_map().span_to_snippet(*span), dupes)
{
let types: Vec<_> =
assoc_items.iter().map(|item| format!("{} = Type", item.ident)).collect();
assoc_items.iter().map(|item| format!("{} = Type", item.name)).collect();
let code = if snippet.ends_with('>') {
// The user wrote `Trait<'a>` or similar and we don't have a type we can
// suggest, but at least we can clue them to the correct syntax
Expand Down Expand Up @@ -388,17 +388,17 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let mut names: FxHashMap<_, usize> = FxHashMap::default();
for item in assoc_items {
types_count += 1;
*names.entry(item.ident.name).or_insert(0) += 1;
*names.entry(item.name).or_insert(0) += 1;
}
let mut label = vec![];
for item in assoc_items {
let postfix = if names[&item.ident.name] > 1 {
let postfix = if names[&item.name] > 1 {
let trait_def_id = item.container.id();
format!(" (from trait `{}`)", tcx.def_path_str(trait_def_id))
} else {
String::new()
};
label.push(format!("`{}`{}", item.ident, postfix));
label.push(format!("`{}`{}", item.name, postfix));
}
if !label.is_empty() {
err.span_label(
Expand Down

0 comments on commit c8941d3

Please sign in to comment.