Skip to content

Pretty-print inherent projections correctly #111486

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 1 commit into from
May 23, 2023
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
3 changes: 2 additions & 1 deletion compiler/rustc_const_eval/src/util/type_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
// 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`"),
}
Expand Down
22 changes: 21 additions & 1 deletion compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,22 @@ pub trait PrettyPrinter<'tcx>:
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
}
Expand Down Expand Up @@ -2821,7 +2837,11 @@ define_print_and_forward_display! {
}

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)) {
Copy link
Member Author

@fmease fmease May 17, 2023

Choose a reason for hiding this comment

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

I'm no longer using AliasTy::kind() here since it ICEs on AssocConsts (see the rust-long-analyzer above). AliasTys can actually be AssocConsts to represent associated const projections even though it's not really documented anywhere as I assume it to be a temporary hack to make #![feature(associated_const_equality)] work until AliasTerm is introduced.

p!(pretty_print_inherent_projection(self))
} else {
p!(print_def_path(self.def_id, self.substs));
}
}

ty::ClosureKind {
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_symbol_mangling/src/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
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),

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

ty::Alias(ty::Inherent, _) => panic!("unexpected inherent projection"),

_ => self.pretty_print_type(ty),
}
}
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 @@ -433,7 +433,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
// 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)?;
Expand Down Expand Up @@ -482,6 +482,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
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`"),
}
Expand Down