Skip to content
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

Isolate the diagnostic code that expects thir::Pat to be printable #128304

Merged
merged 3 commits into from
Jul 29, 2024

Conversation

Zalathar
Copy link
Contributor

Currently, thir::Pat implements fmt::Display (and IntoDiagArg) directly, for use by a few diagnostics.

That makes it tricky to experiment with alternate representations for THIR patterns, because the patterns currently need to be printable on their own. That immediately rules out possibilities like storing subpatterns as a PatId index into a central list (instead of the current directly-owned Box<Pat>).

This PR therefore takes an incremental step away from that obstacle, by removing thir::Pat from diagnostic structs in rustc_pattern_analysis, and hiding the pattern-printing process behind a single public Pat::to_string method. Doing so makes it easier to identify and update the code that wants to print patterns, and gives a place to pass in additional context in the future if necessary.


I'm currently not sure whether switching over to PatId is actually desirable or not, but I think this change makes sense on its own merits, by reducing the coupling between thir::Pat and the pattern-analysis error types.

In several cases this avoids the need to clone the underlying pattern, and then
print the clone later.
@rustbot
Copy link
Collaborator

rustbot commented Jul 28, 2024

r? @fee1-dead

rustbot has assigned @fee1-dead.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@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 Jul 28, 2024
@rustbot
Copy link
Collaborator

rustbot commented Jul 28, 2024

Some changes occurred in exhaustiveness checking

cc @Nadrieril

Some changes occurred in match checking

cc @Nadrieril

@Zalathar
Copy link
Contributor Author

Noting mostly for my own reference, another mention of obstacles to switching over to PatId: #101139 (comment)

@Zalathar
Copy link
Contributor Author

It looks like thir::Pat in diagnostic structs was first introduced by #106097.

This hides the fact that we print `WitnessPat` by converting it to `thir::Pat`
and then printing that.
This gives a clearer view of the (diagnostic) code that expects to be able to
print THIR patterns, and makes it possible to experiment with requiring some
kind of context (for ID lookup) when printing patterns.
@Nadrieril
Copy link
Member

Looks good, thank you!

r? Nadrieril

@bors r+

@bors
Copy link
Contributor

bors commented Jul 29, 2024

📌 Commit ae0ec73 has been approved by Nadrieril

It is now in the queue for this repository.

@rustbot rustbot assigned Nadrieril and unassigned fee1-dead Jul 29, 2024
@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 Jul 29, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 29, 2024
…eril

Isolate the diagnostic code that expects `thir::Pat` to be printable

Currently, `thir::Pat` implements `fmt::Display` (and `IntoDiagArg`) directly, for use by a few diagnostics.

That makes it tricky to experiment with alternate representations for THIR patterns, because the patterns currently need to be printable on their own. That immediately rules out possibilities like storing subpatterns as a `PatId` index into a central list (instead of the current directly-owned `Box<Pat>`).

This PR therefore takes an incremental step away from that obstacle, by removing `thir::Pat` from diagnostic structs in `rustc_pattern_analysis`, and hiding the pattern-printing process behind a single public `Pat::to_string` method. Doing so makes it easier to identify and update the code that wants to print patterns, and gives a place to pass in additional context in the future if necessary.

---

I'm currently not sure whether switching over to `PatId` is actually desirable or not, but I think this change makes sense on its own merits, by reducing the coupling between `thir::Pat` and the pattern-analysis error types.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 29, 2024
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#128182 (handle no_std targets on std builds)
 - rust-lang#128277 (miri: fix offset_from behavior on wildcard pointers)
 - rust-lang#128304 (Isolate the diagnostic code that expects `thir::Pat` to be printable)
 - rust-lang#128307 (Clean and enable `rustdoc::unescaped_backticks` for `core/alloc/std/test/proc_macro`)
 - rust-lang#128322 (CI: move RFL job forward to v6.11-rc1)
 - rust-lang#128333 (Miri subtree update)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit d73decd into rust-lang:master Jul 29, 2024
6 checks passed
@rustbot rustbot added this to the 1.82.0 milestone Jul 29, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jul 29, 2024
Rollup merge of rust-lang#128304 - Zalathar:thir-pat-display, r=Nadrieril

Isolate the diagnostic code that expects `thir::Pat` to be printable

Currently, `thir::Pat` implements `fmt::Display` (and `IntoDiagArg`) directly, for use by a few diagnostics.

That makes it tricky to experiment with alternate representations for THIR patterns, because the patterns currently need to be printable on their own. That immediately rules out possibilities like storing subpatterns as a `PatId` index into a central list (instead of the current directly-owned `Box<Pat>`).

This PR therefore takes an incremental step away from that obstacle, by removing `thir::Pat` from diagnostic structs in `rustc_pattern_analysis`, and hiding the pattern-printing process behind a single public `Pat::to_string` method. Doing so makes it easier to identify and update the code that wants to print patterns, and gives a place to pass in additional context in the future if necessary.

---

I'm currently not sure whether switching over to `PatId` is actually desirable or not, but I think this change makes sense on its own merits, by reducing the coupling between `thir::Pat` and the pattern-analysis error types.
@Zalathar Zalathar deleted the thir-pat-display branch July 29, 2024 12:49
Zalathar added a commit to Zalathar/rust that referenced this pull request Jul 31, 2024
This reverts commit ae0ec73.

The original change in rust-lang#128304 was intended to be a step towards being able to
print `thir::Pat` even after switching to `PatId`.

But because the only patterns that need to be printed are the synthetic ones
created by pattern analysis (for diagnostic purposes only), it makes more sense
to completely separate the printable patterns from the real THIR patterns.
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.

5 participants