Skip to content

Tweak DefPathData #139662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions compiler/rustc_hir/src/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,16 @@ impl DefKind {
| DefKind::ForeignTy
| DefKind::TraitAlias
| DefKind::TyParam
| DefKind::ExternCrate => DefPathData::TypeNs(Some(name.unwrap())),

// An associated type names will be missing for an RPITIT. It will
// later be given a name with `synthetic` in it, if necessary.
DefKind::AssocTy => DefPathData::TypeNs(name),
| DefKind::ExternCrate => DefPathData::TypeNs(name.unwrap()),

// An associated type name will be missing for an RPITIT.
DefKind::AssocTy => {
if let Some(name) = name {
DefPathData::TypeNs(name)
} else {
DefPathData::AnonAssocTy
}
}
Comment on lines +273 to +279
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's a matter of taste, but I find the following more compact and as readable,feel free to ignore:

Suggested change
DefKind::AssocTy => {
if let Some(name) = name {
DefPathData::TypeNs(name)
} else {
DefPathData::AnonAssocTy
}
}
DefKind::AssocTy => name.map_or(DefPathData::AnonAssocTy, DefPathData::TypeNs),

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the if statement


// It's not exactly an anon const, but wrt DefPathData, there
// is no difference.
Expand Down
22 changes: 9 additions & 13 deletions compiler/rustc_hir/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,8 @@ pub enum DefPathData {
Use,
/// A global asm item.
GlobalAsm,
/// Something in the type namespace. Will be empty for RPITIT associated
/// types, which are given a synthetic name later, if necessary.
TypeNs(Option<Symbol>),
/// Something in the type namespace.
TypeNs(Symbol),
/// Something in the value namespace.
ValueNs(Symbol),
/// Something in the macro namespace.
Expand All @@ -291,6 +290,8 @@ pub enum DefPathData {
/// An existential `impl Trait` type node.
/// Argument position `impl Trait` have a `TypeNs` with their pretty-printed name.
OpaqueTy,
/// An anonymous associated type from an RPITIT.
AnonAssocTy,
/// A synthetic body for a coroutine's by-move body.
SyntheticCoroutineBody,
}
Expand Down Expand Up @@ -413,9 +414,7 @@ impl DefPathData {
pub fn get_opt_name(&self) -> Option<Symbol> {
use self::DefPathData::*;
match *self {
TypeNs(name) => name,

ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name),
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name),

Impl
| ForeignMod
Expand All @@ -426,21 +425,17 @@ impl DefPathData {
| Ctor
| AnonConst
| OpaqueTy
| AnonAssocTy
| SyntheticCoroutineBody => None,
}
}

pub fn name(&self) -> DefPathDataName {
use self::DefPathData::*;
match *self {
TypeNs(name) => {
if let Some(name) = name {
DefPathDataName::Named(name)
} else {
DefPathDataName::Anon { namespace: sym::synthetic }
}
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => {
DefPathDataName::Named(name)
}
ValueNs(name) | MacroNs(name) | LifetimeNs(name) => DefPathDataName::Named(name),
// Note that this does not show up in user print-outs.
CrateRoot => DefPathDataName::Anon { namespace: kw::Crate },
Impl => DefPathDataName::Anon { namespace: kw::Impl },
Expand All @@ -451,6 +446,7 @@ impl DefPathData {
Ctor => DefPathDataName::Anon { namespace: sym::constructor },
AnonConst => DefPathDataName::Anon { namespace: sym::constant },
OpaqueTy => DefPathDataName::Anon { namespace: sym::opaque },
AnonAssocTy => DefPathDataName::Anon { namespace: sym::anon_assoc },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% certain about the name anon_assoc, happy to hear alternative suggestions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a huge fan of the name, but not enough to try to think of a new name. Ideally we wouldn't render this at all in diagnostics, but special casing all diagnostics that render paths would be difficult b/c we'd need to work out an alternative to rendering the path of the anonymous GAT.

SyntheticCoroutineBody => DefPathDataName::Anon { namespace: sym::synthetic },
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
// the children of the visible parent (as was done when computing
// `visible_parent_map`), looking for the specific child we currently have and then
// have access to the re-exported name.
DefPathData::TypeNs(Some(ref mut name)) if Some(visible_parent) != actual_parent => {
DefPathData::TypeNs(ref mut name) if Some(visible_parent) != actual_parent => {
// Item might be re-exported several times, but filter for the one
// that's public and whose identifier isn't `_`.
let reexport = self
Expand All @@ -641,7 +641,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
}
// Re-exported `extern crate` (#43189).
DefPathData::CrateRoot => {
data = DefPathData::TypeNs(Some(self.tcx().crate_name(def_id.krate)));
data = DefPathData::TypeNs(self.tcx().crate_name(def_id.krate));
}
_ => {}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/significant_drop_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn true_significant_drop_ty<'tcx>(
name_rev.push(tcx.crate_name(did.krate));
}
rustc_hir::definitions::DefPathData::TypeNs(symbol) => {
name_rev.push(symbol.unwrap());
name_rev.push(symbol);
}
_ => return None,
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1339,12 +1339,12 @@ impl<'tcx> Resolver<'_, 'tcx> {
expn_id: ExpnId,
span: Span,
) -> TyCtxtFeed<'tcx, LocalDefId> {
let data = def_kind.def_path_data(name);
assert!(
!self.node_id_to_def_id.contains_key(&node_id),
"adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}",
"adding a def for node-id {:?}, name {:?}, data {:?} but a previous def exists: {:?}",
node_id,
data,
name,
def_kind,
self.tcx.definitions_untracked().def_key(self.node_id_to_def_id[&node_id].key()),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,8 @@ fn encode_ty_name(tcx: TyCtxt<'_>, def_id: DefId) -> String {
| hir::definitions::DefPathData::Use
| hir::definitions::DefPathData::GlobalAsm
| hir::definitions::DefPathData::MacroNs(..)
| hir::definitions::DefPathData::LifetimeNs(..) => {
| hir::definitions::DefPathData::LifetimeNs(..)
| hir::definitions::DefPathData::AnonAssocTy => {
bug!("encode_ty_name: unexpected `{:?}`", disambiguated_data.data);
}
});
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ symbols! {
and_then,
anon,
anon_adt,
anon_assoc,
anonymous_lifetime_in_impl_trait,
any,
append_const_msg,
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_symbol_mangling/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,8 @@ impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
| DefPathData::GlobalAsm
| DefPathData::Impl
| DefPathData::MacroNs(_)
| DefPathData::LifetimeNs(_) => {
| DefPathData::LifetimeNs(_)
| DefPathData::AnonAssocTy => {
bug!("symbol_names: unexpected DefPathData: {:?}", disambiguated_data.data)
}
};
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ty_utils/src/assoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ fn associated_type_for_impl_trait_in_trait(
assert_eq!(tcx.def_kind(trait_def_id), DefKind::Trait);

let span = tcx.def_span(opaque_ty_def_id);
// No name because this is a synthetic associated type.
// No name because this is an anonymous associated type.
let trait_assoc_ty = tcx.at(span).create_def(trait_def_id, None, DefKind::AssocTy);

let local_def_id = trait_assoc_ty.def_id();
Expand Down Expand Up @@ -305,7 +305,7 @@ fn associated_type_for_impl_trait_in_impl(
hir::FnRetTy::DefaultReturn(_) => tcx.def_span(impl_fn_def_id),
hir::FnRetTy::Return(ty) => ty.span,
};
// No name because this is a synthetic associated type.
// No name because this is an anonymous associated type.
let impl_assoc_ty = tcx.at(span).create_def(impl_local_def_id, None, DefKind::AssocTy);

let local_def_id = impl_assoc_ty.def_id();
Expand Down
6 changes: 3 additions & 3 deletions src/tools/clippy/clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3502,7 +3502,7 @@ fn maybe_get_relative_path(from: &DefPath, to: &DefPath, max_super: usize) -> St
// a::b::c ::d::sym refers to
// e::f::sym:: ::
// result should be super::super::super::super::e::f
if let DefPathData::TypeNs(Some(s)) = l {
if let DefPathData::TypeNs(s) = l {
path.push(s.to_string());
}
if let DefPathData::TypeNs(_) = r {
Expand All @@ -3513,7 +3513,7 @@ fn maybe_get_relative_path(from: &DefPath, to: &DefPath, max_super: usize) -> St
// a::b::sym:: :: refers to
// c::d::e ::f::sym
// when looking at `f`
Left(DefPathData::TypeNs(Some(sym))) => path.push(sym.to_string()),
Left(DefPathData::TypeNs(sym)) => path.push(sym.to_string()),
// consider:
// a::b::c ::d::sym refers to
// e::f::sym:: ::
Expand All @@ -3527,7 +3527,7 @@ fn maybe_get_relative_path(from: &DefPath, to: &DefPath, max_super: usize) -> St
// `super` chain would be too long, just use the absolute path instead
once(String::from("crate"))
.chain(to.data.iter().filter_map(|el| {
if let DefPathData::TypeNs(Some(sym)) = el.data {
if let DefPathData::TypeNs(sym) = el.data {
Some(sym.to_string())
} else {
None
Expand Down
4 changes: 2 additions & 2 deletions tests/ui-fulldeps/stable-mir/check_assoc_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn test_assoc_items() -> ControlFlow<()> {
check_items(
&trait_assoc_item_defs,
&[
"ATrait::{synthetic#0}",
"ATrait::{anon_assoc#0}",
"ATrait::rpitit",
"ATrait::Assoc",
"ATrait::assoc_fn_no_self",
Expand All @@ -64,7 +64,7 @@ fn test_assoc_items() -> ControlFlow<()> {
check_items(
&impl_assoc_item_defs,
&[
"<AStruct as ATrait>::{synthetic#0}",
"<AStruct as ATrait>::{anon_assoc#0}",
"<AStruct as ATrait>::rpitit",
"<AStruct as ATrait>::Assoc",
"<AStruct as ATrait>::assoc_fn_no_self",
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/delegation/unsupported.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0391]: cycle detected when computing type of `opaque::<impl at $DIR/unsupported.rs:21:5: 21:24>::{synthetic#0}`
error[E0391]: cycle detected when computing type of `opaque::<impl at $DIR/unsupported.rs:21:5: 21:24>::{anon_assoc#0}`
--> $DIR/unsupported.rs:22:25
|
LL | reuse to_reuse::opaque_ret;
Expand All @@ -9,15 +9,15 @@ note: ...which requires comparing an impl and trait method signature, inferring
|
LL | reuse to_reuse::opaque_ret;
| ^^^^^^^^^^
= note: ...which again requires computing type of `opaque::<impl at $DIR/unsupported.rs:21:5: 21:24>::{synthetic#0}`, completing the cycle
= note: ...which again requires computing type of `opaque::<impl at $DIR/unsupported.rs:21:5: 21:24>::{anon_assoc#0}`, completing the cycle
note: cycle used when checking that `opaque::<impl at $DIR/unsupported.rs:21:5: 21:24>` is well-formed
--> $DIR/unsupported.rs:21:5
|
LL | impl ToReuse for u8 {
| ^^^^^^^^^^^^^^^^^^^
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information

error[E0391]: cycle detected when computing type of `opaque::<impl at $DIR/unsupported.rs:24:5: 24:25>::{synthetic#0}`
error[E0391]: cycle detected when computing type of `opaque::<impl at $DIR/unsupported.rs:24:5: 24:25>::{anon_assoc#0}`
--> $DIR/unsupported.rs:25:24
|
LL | reuse ToReuse::opaque_ret;
Expand All @@ -28,7 +28,7 @@ note: ...which requires comparing an impl and trait method signature, inferring
|
LL | reuse ToReuse::opaque_ret;
| ^^^^^^^^^^
= note: ...which again requires computing type of `opaque::<impl at $DIR/unsupported.rs:24:5: 24:25>::{synthetic#0}`, completing the cycle
= note: ...which again requires computing type of `opaque::<impl at $DIR/unsupported.rs:24:5: 24:25>::{anon_assoc#0}`, completing the cycle
note: cycle used when checking that `opaque::<impl at $DIR/unsupported.rs:24:5: 24:25>` is well-formed
--> $DIR/unsupported.rs:24:5
|
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/impl-trait/in-trait/doesnt-satisfy.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ LL | fn bar() -> () {}
|
= help: the trait `std::fmt::Display` is not implemented for `()`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
note: required by a bound in `Foo::{synthetic#0}`
note: required by a bound in `Foo::{anon_assoc#0}`
--> $DIR/doesnt-satisfy.rs:2:22
|
LL | fn bar() -> impl std::fmt::Display;
| ^^^^^^^^^^^^^^^^^ required by this bound in `Foo::{synthetic#0}`
| ^^^^^^^^^^^^^^^^^ required by this bound in `Foo::{anon_assoc#0}`

error: aborting due to 1 previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/impl-trait/in-trait/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait Foo {
}

fn hello<'s, T: Foo>(x: &'s T) -> impl Sized + use<'s, T> {
//~^ ERROR <T as Foo>::{synthetic#0}<'s/#1>
//~^ ERROR <T as Foo>::{anon_assoc#0}<'s/#1>
x.hello()
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/impl-trait/in-trait/dump.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: <T as Foo>::{synthetic#0}<'s/#1>
error: <T as Foo>::{anon_assoc#0}<'s/#1>
--> $DIR/dump.rs:10:35
|
LL | fn hello<'s, T: Foo>(x: &'s T) -> impl Sized + use<'s, T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ LL | fn foo<F2: Foo<u8>>(self) -> impl Foo<u8> {
| ^^^^^^^^^^^^ the trait `Foo<char>` is not implemented for `impl Foo<u8>`
|
= help: the trait `Foo<char>` is implemented for `Bar`
note: required by a bound in `Foo::{synthetic#0}`
note: required by a bound in `Foo::{anon_assoc#0}`
--> $DIR/return-dont-satisfy-bounds.rs:2:30
|
LL | fn foo<F2>(self) -> impl Foo<T>;
| ^^^^^^ required by this bound in `Foo::{synthetic#0}`
| ^^^^^^ required by this bound in `Foo::{anon_assoc#0}`

error[E0277]: the trait bound `Bar: Foo<u8>` is not satisfied
--> $DIR/return-dont-satisfy-bounds.rs:8:34
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ error[E0277]: the trait bound `Something: Termination` is not satisfied
LL | fn main() -> Something {
| ^^^^^^^^^ the trait `Termination` is not implemented for `Something`
|
note: required by a bound in `Main::{synthetic#0}`
note: required by a bound in `Main::{anon_assoc#0}`
--> $DIR/issue-103052-2.rs:3:27
|
LL | fn main() -> impl std::process::Termination;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Main::{synthetic#0}`
| ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Main::{anon_assoc#0}`

error: aborting due to 1 previous error

Expand Down
Loading