Skip to content

Allow customising ty::TraitRef's printing behavior. #59188

Closed

Description

@eddyb

(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.

Activity

  1. added
    C-cleanupCategory: PRs that clean code up or issues documenting cleanup.
    E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
    E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.
    on Mar 15, 2019
  2. eddyb commented on Mar 15, 2019

    @eddyb
    ContributorAuthor

    Blocked on #58140.

    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).

    cc @nikomatsakis

  3. stepnivlk commented on Apr 10, 2019

    @stepnivlk

    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.

  4. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Apr 10, 2019
  5. nathankleyn commented on Apr 18, 2019

    @nathankleyn
    Contributor

    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?

  6. eddyp commented on Apr 18, 2019

    @eddyp
    Contributor

    @stepnivlk you probably meant @eddyb

  7. eddyb commented on Apr 19, 2019

    @eddyb
    ContributorAuthor

    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:

    ty::TraitRef<'tcx> {
    p!(print_def_path(self.def_id, self.substs));
    }

    and also here:
    ty::Binder<ty::TraitRef<'tcx>>,

    (since that is the behavior for printing with {}, i.e. fmt::Display, it's the one we want to force to go through print_only_trait_path)

  8. nathankleyn commented on Apr 23, 2019

    @nathankleyn
    Contributor

    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! 🤞

  9. added a commit that references this issue on Apr 25, 2019
    f5ec9d8
  10. horse-princess commented on Nov 20, 2019

    @horse-princess
    Contributor

    @nathankleyn would you mind me snatching this?

  11. nathankleyn commented on Nov 20, 2019

    @nathankleyn
    Contributor

    @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.

  12. added a commit that references this issue on Nov 29, 2019
    9933b80
  13. added a commit that references this issue on Nov 29, 2019
    b16b339
  14. added a commit that references this issue on Nov 29, 2019
    64efc45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-cleanupCategory: PRs that clean code up or issues documenting cleanup.E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions