Skip to content

Pretty-print let-else with added parenthesization when needed #125051

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

Merged
merged 2 commits into from
May 13, 2024

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented May 12, 2024

Rustc used to produce invalid syntax for the following code, which is problematic because it means we cannot apply rustfmt to the output of -Zunpretty=expanded.

macro_rules! expr {
    ($e:expr) => { $e };
}

fn main() {
    let _ = expr!(loop {}) else { return; };
}
$ rustc repro.rs -Zunpretty=expanded | rustfmt
error: `loop...else` loops are not supported
 --> <stdin>:9:29
  |
9 | fn main() { let _ = loop {} else { return; }; }
  |                     ----    ^^^^^^^^^^^^^^^^
  |                     |
  |                     `else` is attached to this loop
  |
  = note: consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run

@rustbot
Copy link
Collaborator

rustbot commented May 12, 2024

r? @nnethercote

rustbot has assigned @nnethercote.
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 May 12, 2024
@dtolnay dtolnay added the A-pretty Area: Pretty printing (including `-Z unpretty`) label May 12, 2024
@compiler-errors
Copy link
Member

r=me when CI is green

[ f() ],
"let _ = f() else { return; };",
"let _ = f() else { return; }",
);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially tried testing this with:

    c2_let_expr_else_return!(
        [ loop {} ],
        "let _ = (loop {}) else { return; };",
        "let _ = loop {} else { return; }",
    );

but the curly brace diagnostic actually looks inside of the $expr. I am not sure whether that is intentional. (In my opinion it shouldn't do that.) I added a separate test in tests/ui/unpretty instead.

error: right curly brace `}` before `else` in a `let...else` statement not allowed
  --> tests/ui/macros/stringify.rs:704:17
   |
LL |         [ loop {} ],
   |                 ^
   |
help: wrap the expression in parentheses
   |
LL |         [ (loop {}) ],
   |           +       +

@dtolnay
Copy link
Member Author

dtolnay commented May 12, 2024

@bors r=compiler-errors

@bors
Copy link
Collaborator

bors commented May 12, 2024

📌 Commit 94cc82c has been approved by compiler-errors

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 May 12, 2024
@bors
Copy link
Collaborator

bors commented May 12, 2024

⌛ Testing commit 94cc82c with merge ecbe3fd...

@bors
Copy link
Collaborator

bors commented May 13, 2024

☀️ Test successful - checks-actions
Approved by: compiler-errors
Pushing ecbe3fd to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label May 13, 2024
@bors bors merged commit ecbe3fd into rust-lang:master May 13, 2024
7 checks passed
@rustbot rustbot added this to the 1.80.0 milestone May 13, 2024
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (ecbe3fd): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
2.4% [2.4%, 2.4%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.1% [-2.1%, -2.1%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.2% [-2.1%, 2.4%] 2

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.4% [2.4%, 2.4%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 677.096s -> 676.356s (-0.11%)
Artifact size: 316.08 MiB -> 316.06 MiB (-0.01%)

@dtolnay dtolnay deleted the printletelse branch May 19, 2024 17:41
@dtolnay dtolnay added F-let_else Issues related to let-else statements (RFC 3137) and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-pretty Area: Pretty printing (including `-Z unpretty`) F-let_else Issues related to let-else statements (RFC 3137) merged-by-bors This PR was explicitly merged by bors. 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.

6 participants