Skip to content

Commit 602cf24

Browse files
Give RPITITs real names with -Zlower-impl-trait-in-trait-to-assoc-ty
1 parent f8aea8d commit 602cf24

File tree

5 files changed

+42
-19
lines changed

5 files changed

+42
-19
lines changed

compiler/rustc_hir/src/definitions.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ pub enum DefPathData {
281281
/// An `impl Trait` type node.
282282
ImplTrait,
283283
/// `impl Trait` generated associated type node.
284-
ImplTraitAssocTy,
284+
ImplTraitAssocTy(Symbol),
285285
}
286286

287287
impl Definitions {
@@ -402,11 +402,11 @@ impl DefPathData {
402402
pub fn get_opt_name(&self) -> Option<Symbol> {
403403
use self::DefPathData::*;
404404
match *self {
405-
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name),
406-
407-
// We use this name when collecting `ModChild`s.
408-
// FIXME this could probably be removed with some refactoring to the name resolver.
409-
ImplTraitAssocTy => Some(kw::Empty),
405+
TypeNs(name)
406+
| ValueNs(name)
407+
| MacroNs(name)
408+
| LifetimeNs(name)
409+
| ImplTraitAssocTy(name) => Some(name),
410410

411411
Impl | ForeignMod | CrateRoot | Use | GlobalAsm | ClosureExpr | Ctor | AnonConst
412412
| ImplTrait => None,
@@ -416,9 +416,11 @@ impl DefPathData {
416416
pub fn name(&self) -> DefPathDataName {
417417
use self::DefPathData::*;
418418
match *self {
419-
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => {
420-
DefPathDataName::Named(name)
421-
}
419+
TypeNs(name)
420+
| ValueNs(name)
421+
| MacroNs(name)
422+
| LifetimeNs(name)
423+
| ImplTraitAssocTy(name) => DefPathDataName::Named(name),
422424
// Note that this does not show up in user print-outs.
423425
CrateRoot => DefPathDataName::Anon { namespace: kw::Crate },
424426
Impl => DefPathDataName::Anon { namespace: kw::Impl },
@@ -428,7 +430,7 @@ impl DefPathData {
428430
ClosureExpr => DefPathDataName::Anon { namespace: sym::closure },
429431
Ctor => DefPathDataName::Anon { namespace: sym::constructor },
430432
AnonConst => DefPathDataName::Anon { namespace: sym::constant },
431-
ImplTrait | ImplTraitAssocTy => DefPathDataName::Anon { namespace: sym::opaque },
433+
ImplTrait => DefPathDataName::Anon { namespace: sym::opaque },
432434
}
433435
}
434436
}

compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ fn encode_ty_name(tcx: TyCtxt<'_>, def_id: DefId) -> String {
396396
hir::definitions::DefPathData::CrateRoot
397397
| hir::definitions::DefPathData::Use
398398
| hir::definitions::DefPathData::GlobalAsm
399-
| hir::definitions::DefPathData::ImplTraitAssocTy
399+
| hir::definitions::DefPathData::ImplTraitAssocTy(_)
400400
| hir::definitions::DefPathData::MacroNs(..)
401401
| hir::definitions::DefPathData::LifetimeNs(..) => {
402402
bug!("encode_ty_name: unexpected `{:?}`", disambiguated_data.data);

compiler/rustc_symbol_mangling/src/v0.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
792792
| DefPathData::Use
793793
| DefPathData::GlobalAsm
794794
| DefPathData::Impl
795-
| DefPathData::ImplTraitAssocTy
795+
| DefPathData::ImplTraitAssocTy(_)
796796
| DefPathData::MacroNs(_)
797797
| DefPathData::LifetimeNs(_) => {
798798
bug!("symbol_names: unexpected DefPathData: {:?}", disambiguated_data.data)

compiler/rustc_ty_utils/src/assoc.rs

+26-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId};
55
use rustc_hir::definitions::DefPathData;
66
use rustc_hir::intravisit::{self, Visitor};
77
use rustc_middle::ty::{self, ImplTraitInTraitData, InternalSubsts, TyCtxt};
8-
use rustc_span::symbol::kw;
8+
use rustc_span::Symbol;
99

1010
pub fn provide(providers: &mut ty::query::Providers) {
1111
*providers = ty::query::Providers {
@@ -266,7 +266,8 @@ fn associated_type_for_impl_trait_in_trait(
266266
assert_eq!(tcx.def_kind(trait_def_id), DefKind::Trait);
267267

268268
let span = tcx.def_span(opaque_ty_def_id);
269-
let trait_assoc_ty = tcx.at(span).create_def(trait_def_id, DefPathData::ImplTraitAssocTy);
269+
let name = name_for_impl_trait_in_trait(tcx, opaque_ty_def_id, trait_def_id);
270+
let trait_assoc_ty = tcx.at(span).create_def(trait_def_id, DefPathData::ImplTraitAssocTy(name));
270271

271272
let local_def_id = trait_assoc_ty.def_id();
272273
let def_id = local_def_id.to_def_id();
@@ -281,7 +282,7 @@ fn associated_type_for_impl_trait_in_trait(
281282
trait_assoc_ty.def_ident_span(Some(span));
282283

283284
trait_assoc_ty.associated_item(ty::AssocItem {
284-
name: kw::Empty,
285+
name,
285286
kind: ty::AssocKind::Type,
286287
def_id,
287288
trait_item_def_id: None,
@@ -351,6 +352,24 @@ fn associated_type_for_impl_trait_in_trait(
351352
local_def_id
352353
}
353354

355+
/// Create a stable path name for an associated type for an impl trait in trait
356+
/// by appending the opaque type's path segments starting from the function name.
357+
fn name_for_impl_trait_in_trait(
358+
tcx: TyCtxt<'_>,
359+
opaque_ty_def_id: LocalDefId,
360+
trait_def_id: LocalDefId,
361+
) -> Symbol {
362+
let mut name = vec![];
363+
let mut def_id = opaque_ty_def_id;
364+
while def_id != trait_def_id {
365+
name.push(tcx.def_key(def_id.to_def_id()).disambiguated_data.to_string());
366+
def_id = tcx.local_parent(def_id);
367+
}
368+
name.reverse();
369+
let name = Symbol::intern(&name.join("::"));
370+
name
371+
}
372+
354373
/// Given an `trait_assoc_def_id` corresponding to an associated item synthesized
355374
/// from an `impl Trait` in an associated function from a trait, and an
356375
/// `impl_fn_def_id` that represents an implementation of the associated function
@@ -364,9 +383,11 @@ fn associated_type_for_impl_trait_in_impl(
364383
let impl_local_def_id = tcx.local_parent(impl_fn_def_id);
365384
let impl_def_id = impl_local_def_id.to_def_id();
366385

386+
let name = tcx.item_name(trait_assoc_def_id);
367387
// FIXME fix the span, we probably want the def_id of the return type of the function
368388
let span = tcx.def_span(impl_fn_def_id);
369-
let impl_assoc_ty = tcx.at(span).create_def(impl_local_def_id, DefPathData::ImplTraitAssocTy);
389+
let impl_assoc_ty =
390+
tcx.at(span).create_def(impl_local_def_id, DefPathData::ImplTraitAssocTy(name));
370391

371392
let local_def_id = impl_assoc_ty.def_id();
372393
let def_id = local_def_id.to_def_id();
@@ -381,7 +402,7 @@ fn associated_type_for_impl_trait_in_impl(
381402
impl_assoc_ty.def_ident_span(Some(span));
382403

383404
impl_assoc_ty.associated_item(ty::AssocItem {
384-
name: kw::Empty,
405+
name,
385406
kind: ty::AssocKind::Type,
386407
def_id,
387408
trait_item_def_id: Some(trait_assoc_def_id),

tests/ui/impl-trait/in-trait/doesnt-satisfy.next.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ LL | fn bar() -> () {}
66
|
77
= help: the trait `std::fmt::Display` is not implemented for `()`
88
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
9-
note: required by a bound in `Foo::{opaque#0}`
9+
note: required by a bound in `Foo::bar::{opaque#0}`
1010
--> $DIR/doesnt-satisfy.rs:8:22
1111
|
1212
LL | fn bar() -> impl std::fmt::Display;
13-
| ^^^^^^^^^^^^^^^^^ required by this bound in `Foo::`
13+
| ^^^^^^^^^^^^^^^^^ required by this bound in `Foo::bar::{opaque#0}`
1414

1515
error: aborting due to previous error
1616

0 commit comments

Comments
 (0)