You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(Originally discussed in #58140 (comment))
Right now a TraitRef prints as <T as Trait<U>> via "debug" ({:?}) and Trait<U> via "display" ({}) - this is used all over rustc to indicate what printed form of the trait is desired.
However, it would be overall cleaner for "display" and "debug" printing to match (or even for fmt::Display to not be implemented at all, perhaps?), and provide all the information that exists (i.e. including the Self type).
We would then need a replacement for printing "just the trait path with its parameters", and I think we can have a method, .print_only_trait_path(), that returns a wrapper which has the different formatting behavior.
Also, in order to do this thoroughly, fmt::Display would need to be removed from TraitRef (comment-out its section in src/librustc/ty/print/pretty.rs) and all uses of formatting it with {} would have to be put through the new .print_only_trait_path() method instead.
Only after that is done and everything compiles again, can fmt::Display be re-enabled for TraitRef (and it can be given the same behavior as {:?} today).
Some simplification of ty::print may be possible afterwards (right now associated items of a trait have to print <T as Trait<U>> themselves, without the special-casing Trait<U> would be printed).
Hey @eddyb! I would love to pick this one up and got a bit of the way into exploring this, but wondering — what did you have in mind for where .print_only_trait_path would be implemented?
As in — is this method added to the existing Print / PrettyPrinter traits somehow? Using a new wrapper trait with that method which is impld only for ty::TraitRef? A fn at the top level somewhere? Somehow else?
TraitRef::print_only_trait_path would only construct a trivial wrapper around self (and that wrapper would be e.g. pub struct TraitRefPrintOnlyTraitPath<'tcx>(ty::TraitRef<'tcx>);).
We can also have the same method on PolyTraitRef (i.e. Binder<TraitRef>) that returns Binder<TraitRefPrintOnlyTraitPath> (using map_bound).
Then, in ty::print::pretty, we'd have change this to be on TraitRefPrintOnlyTraitPath:
Thanks @eddyb — that's really helpful! I'll try to make a start on this and see how far I get, it's a really great way to explore my way around the compiler internals some more! 🤞
Activity
eddyb commentedon Mar 15, 2019
Blocked on #58140.
Also, in order to do this thoroughly,
fmt::Displaywould need to be removed fromTraitRef(comment-out its section insrc/librustc/ty/print/pretty.rs) and all uses of formatting it with{}would have to be put through the new.print_only_trait_path()method instead.Only after that is done and everything compiles again, can
fmt::Displaybe re-enabled forTraitRef(and it can be given the same behavior as{:?}today).Some simplification of
ty::printmay be possible afterwards (right now associated items of a trait have to print<T as Trait<U>>themselves, without the special-casingTrait<U>would be printed).cc @nikomatsakis
stepnivlk commentedon Apr 10, 2019
hey @eddyp #58140 got merged does it mean you're happy with proceeding forward with this one? If so I think I can tackle it.
nathankleyn commentedon Apr 18, 2019
Hey @eddyb! I would love to pick this one up and got a bit of the way into exploring this, but wondering — what did you have in mind for where
.print_only_trait_pathwould be implemented?As in — is this method added to the existing
Print/PrettyPrintertraits somehow? Using a new wrapper trait with that method which isimpld only forty::TraitRef? Afnat the top level somewhere? Somehow else?eddyp commentedon Apr 18, 2019
@stepnivlk you probably meant @eddyb
eddyb commentedon Apr 19, 2019
TraitRef::print_only_trait_pathwould only construct a trivial wrapper aroundself(and that wrapper would be e.g.pub struct TraitRefPrintOnlyTraitPath<'tcx>(ty::TraitRef<'tcx>);).We can also have the same method on
PolyTraitRef(i.e.Binder<TraitRef>) that returnsBinder<TraitRefPrintOnlyTraitPath>(usingmap_bound).Then, in
ty::print::pretty, we'd have change this to be onTraitRefPrintOnlyTraitPath:rust/src/librustc/ty/print/pretty.rs
Lines 1530 to 1532 in 316a391
and also here:
rust/src/librustc/ty/print/pretty.rs
Line 1449 in 316a391
(since that is the behavior for printing with
{}, i.e.fmt::Display, it's the one we want to force to go throughprint_only_trait_path)nathankleyn commentedon Apr 23, 2019
Thanks @eddyb — that's really helpful! I'll try to make a start on this and see how far I get, it's a really great way to explore my way around the compiler internals some more! 🤞
Add wrapper to allow customising `ty::TraitRef`'s printing behaviour.
horse-princess commentedon Nov 20, 2019
@nathankleyn would you mind me snatching this?
nathankleyn commentedon Nov 20, 2019
@Areredify Feel free! Some partial work here if it helps: nathankleyn@f5ec9d8
However it was, when I was last working on it, giving me some confusing issues and I couldn't untangle it.
Rollup merge of rust-lang#66613 - Areredify:trait-ref-print, r=eddyb
Auto merge of #66613 - Areredify:trait-ref-print, r=eddyb
Rollup merge of rust-lang#66613 - Areredify:trait-ref-print, r=eddyb