-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Pretty print Fn<(..., ...)>
trait refs with parentheses (almost) always
#118268
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()) | ||
} | ||
estebank marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
#[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()), "("); | ||
estebank marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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)); | ||
} | ||
} | ||
Comment on lines
+2873
to
+2889
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will never print the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't we hack something together for Edit: I think I was thinking of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I could eagerly look into the self type and see if it's something with a signature, but it's not always going to be possible. Let me see. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you manage it, great. If not, r=me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, when we extract this information from the self type, it's often not right. For example, when we have:
Extracting the return type from the self type will give us an error message mentioning something like |
||
|
||
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> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a lot of these :(
I ctrl+f'd for
,);
and,),
yesterday and found like >50 instances.