Add print_trait_sugared
This commit is contained in:
parent
8a7b2035f8
commit
b97ff8eb16
17 changed files with 91 additions and 56 deletions
|
@ -2640,6 +2640,23 @@ impl<'tcx> fmt::Debug for TraitRefPrintOnlyTraitPath<'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Wrapper type for `ty::TraitRef` which opts-in to pretty printing only
|
||||
/// the trait path, and additionally tries to "sugar" `Fn(...)` trait bounds.
|
||||
#[derive(Copy, Clone, TypeFoldable, TypeVisitable, Lift)]
|
||||
pub struct TraitRefPrintSugared<'tcx>(ty::TraitRef<'tcx>);
|
||||
|
||||
impl<'tcx> rustc_errors::IntoDiagnosticArg for TraitRefPrintSugared<'tcx> {
|
||||
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
|
||||
self.to_string().into_diagnostic_arg()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> fmt::Debug for TraitRefPrintSugared<'tcx> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt::Display::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
/// Wrapper type for `ty::TraitRef` which opts-in to pretty printing only
|
||||
/// the trait name. That is, it will print `Trait` instead of
|
||||
/// `<T as Trait<U>>`.
|
||||
|
@ -2657,6 +2674,10 @@ impl<'tcx> ty::TraitRef<'tcx> {
|
|||
TraitRefPrintOnlyTraitPath(self)
|
||||
}
|
||||
|
||||
pub fn print_trait_sugared(self) -> TraitRefPrintSugared<'tcx> {
|
||||
TraitRefPrintSugared(self)
|
||||
}
|
||||
|
||||
pub fn print_only_trait_name(self) -> TraitRefPrintOnlyTraitName<'tcx> {
|
||||
TraitRefPrintOnlyTraitName(self)
|
||||
}
|
||||
|
@ -2666,6 +2687,10 @@ impl<'tcx> ty::Binder<'tcx, ty::TraitRef<'tcx>> {
|
|||
pub fn print_only_trait_path(self) -> ty::Binder<'tcx, TraitRefPrintOnlyTraitPath<'tcx>> {
|
||||
self.map_bound(|tr| tr.print_only_trait_path())
|
||||
}
|
||||
|
||||
pub fn print_trait_sugared(self) -> ty::Binder<'tcx, TraitRefPrintSugared<'tcx>> {
|
||||
self.map_bound(|tr| tr.print_trait_sugared())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, TypeFoldable, TypeVisitable, Lift)]
|
||||
|
@ -2745,6 +2770,7 @@ forward_display_to_print! {
|
|||
ty::PolyExistentialTraitRef<'tcx>,
|
||||
ty::Binder<'tcx, ty::TraitRef<'tcx>>,
|
||||
ty::Binder<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
|
||||
ty::Binder<'tcx, TraitRefPrintSugared<'tcx>>,
|
||||
ty::Binder<'tcx, ty::FnSig<'tcx>>,
|
||||
ty::Binder<'tcx, ty::TraitPredicate<'tcx>>,
|
||||
ty::Binder<'tcx, TraitPredPrintModifiersAndPath<'tcx>>,
|
||||
|
@ -2844,6 +2870,24 @@ define_print_and_forward_display! {
|
|||
p!(print_def_path(self.0.def_id, self.0.args));
|
||||
}
|
||||
|
||||
TraitRefPrintSugared<'tcx> {
|
||||
if !with_no_queries()
|
||||
&& let Some(kind) = cx.tcx().fn_trait_kind_from_def_id(self.0.def_id)
|
||||
&& let ty::Tuple(args) = self.0.args.type_at(1).kind()
|
||||
{
|
||||
p!(write("{}", kind.as_str()), "(");
|
||||
for (i, arg) in args.iter().enumerate() {
|
||||
if i > 0 {
|
||||
p!(", ");
|
||||
}
|
||||
p!(print(arg));
|
||||
}
|
||||
p!(")");
|
||||
} else {
|
||||
p!(print_def_path(self.0.def_id, self.0.args));
|
||||
}
|
||||
}
|
||||
|
||||
TraitRefPrintOnlyTraitName<'tcx> {
|
||||
p!(print_def_path(self.0.def_id, &[]));
|
||||
}
|
||||
|
@ -2892,7 +2936,7 @@ define_print_and_forward_display! {
|
|||
if let ty::ImplPolarity::Negative = self.polarity {
|
||||
p!("!");
|
||||
}
|
||||
p!(print(self.trait_ref.print_only_trait_path()))
|
||||
p!(print(self.trait_ref.print_trait_sugared()))
|
||||
}
|
||||
|
||||
ty::ProjectionPredicate<'tcx> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue