Rollup merge of #111486 - fmease:pp-inh-proj, r=petrochenkov

Pretty-print inherent projections correctly

Previously, we were trying to pretty-print inherent projections with `Printer::print_def_path` which is incorrect since
it expects the substitutions to be of a certain format (parents substs followed by own substs) which doesn't hold for
inherent projections (self type subst followed by own substs).
Now we print inherent projections manually.

Fixes #111390.
Fixes #111397.

Lacking tests! Is there a test suite / compiletest flags for the pretty-printer? In most if not all cases,
inherent projections are normalized away before they get the chance to appear in diagnostics.

If I were to create regression tests for linked issues, they would need to be `mir-opt` tests to exercise
`-Zdump-mir=all` (right?) which doesn't feel quite adequate to me.

`@rustbot` label F-inherent_associated_types
This commit is contained in:
Matthias Krüger 2023-05-23 19:53:41 +02:00 committed by GitHub
commit 221039b416
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 4 deletions

View File

@ -58,11 +58,12 @@ fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
// Types with identity (print the module path).
ty::Adt(ty::AdtDef(Interned(&ty::AdtDefData { did: def_id, .. }, _)), substs)
| ty::FnDef(def_id, substs)
| ty::Alias(_, ty::AliasTy { def_id, substs, .. })
| ty::Alias(ty::Projection | ty::Opaque, ty::AliasTy { def_id, substs, .. })
| ty::Closure(def_id, substs)
| ty::Generator(def_id, substs, _) => self.print_def_path(def_id, substs),
ty::Foreign(def_id) => self.print_def_path(def_id, &[]),
ty::Alias(ty::Inherent, _) => bug!("type_name: unexpected inherent projection"),
ty::GeneratorWitness(_) => bug!("type_name: unexpected `GeneratorWitness`"),
ty::GeneratorWitnessMIR(..) => bug!("type_name: unexpected `GeneratorWitnessMIR`"),
}

View File

@ -1164,6 +1164,22 @@ fn insert_trait_and_projection(
traits.entry(trait_ref).or_default().extend(proj_ty);
}
fn pretty_print_inherent_projection(
self,
alias_ty: &ty::AliasTy<'tcx>,
) -> Result<Self::Path, Self::Error> {
let def_key = self.tcx().def_key(alias_ty.def_id);
self.path_generic_args(
|cx| {
cx.path_append(
|cx| cx.path_qualified(alias_ty.self_ty(), None),
&def_key.disambiguated_data,
)
},
&alias_ty.substs[1..],
)
}
fn ty_infer_name(&self, _: ty::TyVid) -> Option<Symbol> {
None
}
@ -2821,7 +2837,11 @@ pub struct PrintClosureAsImpl<'tcx> {
}
ty::AliasTy<'tcx> {
p!(print_def_path(self.def_id, self.substs));
if let DefKind::Impl { of_trait: false } = cx.tcx().def_kind(cx.tcx().parent(self.def_id)) {
p!(pretty_print_inherent_projection(self))
} else {
p!(print_def_path(self.def_id, self.substs));
}
}
ty::ClosureKind {

View File

@ -220,7 +220,7 @@ fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
match *ty.kind() {
// Print all nominal types as paths (unlike `pretty_print_type`).
ty::FnDef(def_id, substs)
| ty::Alias(_, ty::AliasTy { def_id, substs, .. })
| ty::Alias(ty::Projection | ty::Opaque, ty::AliasTy { def_id, substs, .. })
| ty::Closure(def_id, substs)
| ty::Generator(def_id, substs, _) => self.print_def_path(def_id, substs),
@ -241,6 +241,8 @@ fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
Ok(self)
}
ty::Alias(ty::Inherent, _) => panic!("unexpected inherent projection"),
_ => self.pretty_print_type(ty),
}
}

View File

@ -433,7 +433,7 @@ fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
// Mangle all nominal types as paths.
ty::Adt(ty::AdtDef(Interned(&ty::AdtDefData { did: def_id, .. }, _)), substs)
| ty::FnDef(def_id, substs)
| ty::Alias(_, ty::AliasTy { def_id, substs, .. })
| ty::Alias(ty::Projection | ty::Opaque, ty::AliasTy { def_id, substs, .. })
| ty::Closure(def_id, substs)
| ty::Generator(def_id, substs, _) => {
self = self.print_def_path(def_id, substs)?;
@ -482,6 +482,7 @@ fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
self = r.print(self)?;
}
ty::Alias(ty::Inherent, _) => bug!("symbol_names: unexpected inherent projection"),
ty::GeneratorWitness(_) => bug!("symbol_names: unexpected `GeneratorWitness`"),
ty::GeneratorWitnessMIR(..) => bug!("symbol_names: unexpected `GeneratorWitnessMIR`"),
}