Skip to content

Printer cleanups #144776

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Conversation

nnethercote
Copy link
Contributor

The trait Printer is implemented by six types, and the sub-trait PrettyPrinter is implemented by three of those types. The traits and the impls are complex and a bit of a mess. This PR starts to clean them up.

r? @davidtwco

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 1, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 1, 2025

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred in need_type_info.rs

cc @lcnr

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

@rust-log-analyzer

This comment has been minimized.

@nnethercote nnethercote force-pushed the Printer-cleanups branch 2 times, most recently from 968070d to 8941c3c Compare August 1, 2025 14:58
@bors
Copy link
Collaborator

bors commented Aug 2, 2025

☔ The latest upstream changes (presumably #144458) made this pull request unmergeable. Please resolve the merge conflicts.

This helps me understand the structure of the code a lot.

If any of these are actually reachable, we can put the old code back,
add a new test case, and we will have improved our test coverage.
They each have a single call site.
It's equivalent to the default `PrettyPrinter::comma_sep`.
Currently they are mostly named `cx`, which is a terrible name for a
type that impls `Printer`/`PrettyPrinter`, and is easy to confuse with
other types like `TyCtxt`. This commit changes them to `p`. A couple of
existing `p` variables had to be renamed to make way.
`Bound`/`Placeholder`/`Infer`/`Error` shouldn't occur, so we can handle
them in the second exhaustive `match`, and ignore them in the first
non-exhaustive `match`.
It's a cryptic macro that makes some things slightly more concise in
`PrettyPrinter`. E.g. if you declare `define_scope_printer!(p)` in a
scope you can then call `p! to get these transformations:
```
p!("foo");         -->  write!(p, "foo")?;
p!(print(ty));     -->  ty.print(p)?;
p!(method(args));  -->  p.method(args)?;
```
You can also chain calls, e.g.:
```
p!("foo", print(ty));  -->  write!(p, "foo")?; ty.print(p)?;
```
Ultimately this doesn't seem worth it. The macro definition is hard to
read, the call sites are hard to read, `define_scope_printer!` is pretty
gross, and the code size reductions are small. Tellingly, many normal
`write!` and `print` calls are sprinkled throughout the code, probably
because people have made modifications and didn't want to use or
understand how to use `p!`.

This commit removes it.
None of the impls use it.
@nnethercote
Copy link
Contributor Author

I rebased.

`print_binder` can call `wrap_binder`.
@cjgillot
Copy link
Contributor

cjgillot commented Aug 4, 2025

@bors r+

@bors
Copy link
Collaborator

bors commented Aug 4, 2025

📌 Commit cc62d55 has been approved by cjgillot

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants