Skip to content
/ rust Public
forked from rust-lang/rust

Commit 9eca59a

Browse files
committed
Introduce DefPathData::AnonAssocTy.
PR rust-lang#137977 changed `DefPathData::TypeNs` to contain `Option<Symbol>` to account for RPITIT assoc types being anonymous. This commit changes it back to `Symbol` and gives anonymous assoc types their own variant. It makes things a bit nicer overall.
1 parent fad2535 commit 9eca59a

File tree

8 files changed

+31
-28
lines changed

8 files changed

+31
-28
lines changed

compiler/rustc_hir/src/def.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,16 @@ impl DefKind {
267267
| DefKind::ForeignTy
268268
| DefKind::TraitAlias
269269
| DefKind::TyParam
270-
| DefKind::ExternCrate => DefPathData::TypeNs(Some(name.unwrap())),
271-
272-
// An associated type names will be missing for an RPITIT. It will
273-
// later be given a name with `synthetic` in it, if necessary.
274-
DefKind::AssocTy => DefPathData::TypeNs(name),
270+
| DefKind::ExternCrate => DefPathData::TypeNs(name.unwrap()),
271+
272+
// An associated type name will be missing for an RPITIT.
273+
DefKind::AssocTy => {
274+
if let Some(name) = name {
275+
DefPathData::TypeNs(name)
276+
} else {
277+
DefPathData::AnonAssocTy
278+
}
279+
}
275280

276281
// It's not exactly an anon const, but wrt DefPathData, there
277282
// is no difference.

compiler/rustc_hir/src/definitions.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,8 @@ pub enum DefPathData {
271271
Use,
272272
/// A global asm item.
273273
GlobalAsm,
274-
/// Something in the type namespace. Will be empty for RPITIT associated
275-
/// types, which are given a synthetic name later, if necessary.
276-
TypeNs(Option<Symbol>),
274+
/// Something in the type namespace.
275+
TypeNs(Symbol),
277276
/// Something in the value namespace.
278277
ValueNs(Symbol),
279278
/// Something in the macro namespace.
@@ -291,6 +290,8 @@ pub enum DefPathData {
291290
/// An existential `impl Trait` type node.
292291
/// Argument position `impl Trait` have a `TypeNs` with their pretty-printed name.
293292
OpaqueTy,
293+
/// An anonymous associated type from an RPITIT.
294+
AnonAssocTy,
294295
/// A synthetic body for a coroutine's by-move body.
295296
SyntheticCoroutineBody,
296297
}
@@ -413,9 +414,7 @@ impl DefPathData {
413414
pub fn get_opt_name(&self) -> Option<Symbol> {
414415
use self::DefPathData::*;
415416
match *self {
416-
TypeNs(name) => name,
417-
418-
ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name),
417+
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name),
419418

420419
Impl
421420
| ForeignMod
@@ -426,21 +425,17 @@ impl DefPathData {
426425
| Ctor
427426
| AnonConst
428427
| OpaqueTy
428+
| AnonAssocTy
429429
| SyntheticCoroutineBody => None,
430430
}
431431
}
432432

433433
pub fn name(&self) -> DefPathDataName {
434434
use self::DefPathData::*;
435435
match *self {
436-
TypeNs(name) => {
437-
if let Some(name) = name {
438-
DefPathDataName::Named(name)
439-
} else {
440-
DefPathDataName::Anon { namespace: sym::synthetic }
441-
}
436+
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => {
437+
DefPathDataName::Named(name)
442438
}
443-
ValueNs(name) | MacroNs(name) | LifetimeNs(name) => DefPathDataName::Named(name),
444439
// Note that this does not show up in user print-outs.
445440
CrateRoot => DefPathDataName::Anon { namespace: kw::Crate },
446441
Impl => DefPathDataName::Anon { namespace: kw::Impl },
@@ -451,6 +446,7 @@ impl DefPathData {
451446
Ctor => DefPathDataName::Anon { namespace: sym::constructor },
452447
AnonConst => DefPathDataName::Anon { namespace: sym::constant },
453448
OpaqueTy => DefPathDataName::Anon { namespace: sym::opaque },
449+
AnonAssocTy => DefPathDataName::Anon { namespace: sym::synthetic },
454450
SyntheticCoroutineBody => DefPathDataName::Anon { namespace: sym::synthetic },
455451
}
456452
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
620620
// the children of the visible parent (as was done when computing
621621
// `visible_parent_map`), looking for the specific child we currently have and then
622622
// have access to the re-exported name.
623-
DefPathData::TypeNs(Some(ref mut name)) if Some(visible_parent) != actual_parent => {
623+
DefPathData::TypeNs(ref mut name) if Some(visible_parent) != actual_parent => {
624624
// Item might be re-exported several times, but filter for the one
625625
// that's public and whose identifier isn't `_`.
626626
let reexport = self
@@ -641,7 +641,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
641641
}
642642
// Re-exported `extern crate` (#43189).
643643
DefPathData::CrateRoot => {
644-
data = DefPathData::TypeNs(Some(self.tcx().crate_name(def_id.krate)));
644+
data = DefPathData::TypeNs(self.tcx().crate_name(def_id.krate));
645645
}
646646
_ => {}
647647
}

compiler/rustc_middle/src/ty/significant_drop_order.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn true_significant_drop_ty<'tcx>(
2626
name_rev.push(tcx.crate_name(did.krate));
2727
}
2828
rustc_hir::definitions::DefPathData::TypeNs(symbol) => {
29-
name_rev.push(symbol.unwrap());
29+
name_rev.push(symbol);
3030
}
3131
_ => return None,
3232
}

compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/encode.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,8 @@ fn encode_ty_name(tcx: TyCtxt<'_>, def_id: DefId) -> String {
721721
| hir::definitions::DefPathData::Use
722722
| hir::definitions::DefPathData::GlobalAsm
723723
| hir::definitions::DefPathData::MacroNs(..)
724-
| hir::definitions::DefPathData::LifetimeNs(..) => {
724+
| hir::definitions::DefPathData::LifetimeNs(..)
725+
| hir::definitions::DefPathData::AnonAssocTy => {
725726
bug!("encode_ty_name: unexpected `{:?}`", disambiguated_data.data);
726727
}
727728
});

compiler/rustc_symbol_mangling/src/v0.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,8 @@ impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
858858
| DefPathData::GlobalAsm
859859
| DefPathData::Impl
860860
| DefPathData::MacroNs(_)
861-
| DefPathData::LifetimeNs(_) => {
861+
| DefPathData::LifetimeNs(_)
862+
| DefPathData::AnonAssocTy => {
862863
bug!("symbol_names: unexpected DefPathData: {:?}", disambiguated_data.data)
863864
}
864865
};

compiler/rustc_ty_utils/src/assoc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ fn associated_type_for_impl_trait_in_trait(
252252
assert_eq!(tcx.def_kind(trait_def_id), DefKind::Trait);
253253

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

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

311311
let local_def_id = impl_assoc_ty.def_id();

src/tools/clippy/clippy_utils/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3502,7 +3502,7 @@ fn maybe_get_relative_path(from: &DefPath, to: &DefPath, max_super: usize) -> St
35023502
// a::b::c ::d::sym refers to
35033503
// e::f::sym:: ::
35043504
// result should be super::super::super::super::e::f
3505-
if let DefPathData::TypeNs(Some(s)) = l {
3505+
if let DefPathData::TypeNs(s) = l {
35063506
path.push(s.to_string());
35073507
}
35083508
if let DefPathData::TypeNs(_) = r {
@@ -3513,7 +3513,7 @@ fn maybe_get_relative_path(from: &DefPath, to: &DefPath, max_super: usize) -> St
35133513
// a::b::sym:: :: refers to
35143514
// c::d::e ::f::sym
35153515
// when looking at `f`
3516-
Left(DefPathData::TypeNs(Some(sym))) => path.push(sym.to_string()),
3516+
Left(DefPathData::TypeNs(sym)) => path.push(sym.to_string()),
35173517
// consider:
35183518
// a::b::c ::d::sym refers to
35193519
// e::f::sym:: ::
@@ -3527,7 +3527,7 @@ fn maybe_get_relative_path(from: &DefPath, to: &DefPath, max_super: usize) -> St
35273527
// `super` chain would be too long, just use the absolute path instead
35283528
once(String::from("crate"))
35293529
.chain(to.data.iter().filter_map(|el| {
3530-
if let DefPathData::TypeNs(Some(sym)) = el.data {
3530+
if let DefPathData::TypeNs(sym) = el.data {
35313531
Some(sym.to_string())
35323532
} else {
35333533
None

0 commit comments

Comments
 (0)