Skip to content

Commit 221039b

Browse files
authored
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
2 parents d1e9910 + 778abc7 commit 221039b

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

compiler/rustc_const_eval/src/util/type_name.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
5858
// Types with identity (print the module path).
5959
ty::Adt(ty::AdtDef(Interned(&ty::AdtDefData { did: def_id, .. }, _)), substs)
6060
| ty::FnDef(def_id, substs)
61-
| ty::Alias(_, ty::AliasTy { def_id, substs, .. })
61+
| ty::Alias(ty::Projection | ty::Opaque, ty::AliasTy { def_id, substs, .. })
6262
| ty::Closure(def_id, substs)
6363
| ty::Generator(def_id, substs, _) => self.print_def_path(def_id, substs),
6464
ty::Foreign(def_id) => self.print_def_path(def_id, &[]),
6565

66+
ty::Alias(ty::Inherent, _) => bug!("type_name: unexpected inherent projection"),
6667
ty::GeneratorWitness(_) => bug!("type_name: unexpected `GeneratorWitness`"),
6768
ty::GeneratorWitnessMIR(..) => bug!("type_name: unexpected `GeneratorWitnessMIR`"),
6869
}

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,22 @@ pub trait PrettyPrinter<'tcx>:
11641164
traits.entry(trait_ref).or_default().extend(proj_ty);
11651165
}
11661166

1167+
fn pretty_print_inherent_projection(
1168+
self,
1169+
alias_ty: &ty::AliasTy<'tcx>,
1170+
) -> Result<Self::Path, Self::Error> {
1171+
let def_key = self.tcx().def_key(alias_ty.def_id);
1172+
self.path_generic_args(
1173+
|cx| {
1174+
cx.path_append(
1175+
|cx| cx.path_qualified(alias_ty.self_ty(), None),
1176+
&def_key.disambiguated_data,
1177+
)
1178+
},
1179+
&alias_ty.substs[1..],
1180+
)
1181+
}
1182+
11671183
fn ty_infer_name(&self, _: ty::TyVid) -> Option<Symbol> {
11681184
None
11691185
}
@@ -2821,7 +2837,11 @@ define_print_and_forward_display! {
28212837
}
28222838

28232839
ty::AliasTy<'tcx> {
2824-
p!(print_def_path(self.def_id, self.substs));
2840+
if let DefKind::Impl { of_trait: false } = cx.tcx().def_kind(cx.tcx().parent(self.def_id)) {
2841+
p!(pretty_print_inherent_projection(self))
2842+
} else {
2843+
p!(print_def_path(self.def_id, self.substs));
2844+
}
28252845
}
28262846

28272847
ty::ClosureKind {

compiler/rustc_symbol_mangling/src/legacy.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
220220
match *ty.kind() {
221221
// Print all nominal types as paths (unlike `pretty_print_type`).
222222
ty::FnDef(def_id, substs)
223-
| ty::Alias(_, ty::AliasTy { def_id, substs, .. })
223+
| ty::Alias(ty::Projection | ty::Opaque, ty::AliasTy { def_id, substs, .. })
224224
| ty::Closure(def_id, substs)
225225
| ty::Generator(def_id, substs, _) => self.print_def_path(def_id, substs),
226226

@@ -241,6 +241,8 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
241241
Ok(self)
242242
}
243243

244+
ty::Alias(ty::Inherent, _) => panic!("unexpected inherent projection"),
245+
244246
_ => self.pretty_print_type(ty),
245247
}
246248
}

compiler/rustc_symbol_mangling/src/v0.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
433433
// Mangle all nominal types as paths.
434434
ty::Adt(ty::AdtDef(Interned(&ty::AdtDefData { did: def_id, .. }, _)), substs)
435435
| ty::FnDef(def_id, substs)
436-
| ty::Alias(_, ty::AliasTy { def_id, substs, .. })
436+
| ty::Alias(ty::Projection | ty::Opaque, ty::AliasTy { def_id, substs, .. })
437437
| ty::Closure(def_id, substs)
438438
| ty::Generator(def_id, substs, _) => {
439439
self = self.print_def_path(def_id, substs)?;
@@ -482,6 +482,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
482482
self = r.print(self)?;
483483
}
484484

485+
ty::Alias(ty::Inherent, _) => bug!("symbol_names: unexpected inherent projection"),
485486
ty::GeneratorWitness(_) => bug!("symbol_names: unexpected `GeneratorWitness`"),
486487
ty::GeneratorWitnessMIR(..) => bug!("symbol_names: unexpected `GeneratorWitnessMIR`"),
487488
}

0 commit comments

Comments
 (0)