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

Parenthesize break values containing leading label #126883

Merged
merged 2 commits into from
Jul 2, 2024

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Jun 24, 2024

The AST pretty printer previously produced invalid syntax in the case of break expressions with a value that begins with a loop or block label.

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

fn main() {
    loop {
        break expr!('a: loop { break 'a 1; } + 1);
    };
}

rustc -Zunpretty=expanded main.rs :

#![feature(prelude_import)]
#![no_std]
#[prelude_import]
use ::std::prelude::rust_2015::*;
#[macro_use]
extern crate std;
macro_rules! expr { ($e:expr) => { $e }; }

fn main() { loop { break 'a: loop { break 'a 1; } + 1; }; }

The expanded code is not valid Rust syntax. Printing invalid syntax is bad because it blocks cargo expand from being able to format the output as Rust syntax using rustfmt.

error: parentheses are required around this expression to avoid confusion with a labeled break expression
 --> <anon>:9:26
  |
9 | fn main() { loop { break 'a: loop { break 'a 1; } + 1; }; }
  |                          ^^^^^^^^^^^^^^^^^^^^^^^^
  |
help: wrap the expression in parentheses
  |
9 | fn main() { loop { break ('a: loop { break 'a 1; }) + 1; }; }
  |                          +                        +

This PR updates the AST pretty-printer to insert parentheses around the value of a break expression as required to avoid this edge case.

@rustbot
Copy link
Collaborator

rustbot commented Jun 24, 2024

r? @fmease

rustbot has assigned @fmease.
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 Jun 24, 2024
Copy link
Member

@fmease fmease left a comment

Choose a reason for hiding this comment

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

Thanks! Sorry for the delay.
r=me with nit addressed

compiler/rustc_ast/src/util/classify.rs Outdated Show resolved Hide resolved
@fmease
Copy link
Member

fmease commented Jul 1, 2024

@bors rollup

@fmease fmease added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 1, 2024
@dtolnay
Copy link
Member Author

dtolnay commented Jul 2, 2024

@bors r=fmease

@bors
Copy link
Contributor

bors commented Jul 2, 2024

📌 Commit 0698223 has been approved by fmease

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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 2, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 2, 2024
…iaskrgr

Rollup of 10 pull requests

Successful merges:

 - rust-lang#126883 (Parenthesize break values containing leading label)
 - rust-lang#127136 (Fix `FnMut::call_mut`/`Fn::call` shim for async closures that capture references)
 - rust-lang#127146 (Uplift fast rejection to new solver)
 - rust-lang#127152 (Bootstrap: Try renaming the file if removing fails)
 - rust-lang#127168 (Use the aligned size for alloca at args/ret when the pass mode is cast)
 - rust-lang#127203 (Fix import suggestion error when path segment failed not from starting)
 - rust-lang#127212 (Update books)
 - rust-lang#127224 (Make `FloatTy` checks exhaustive in pretty print)
 - rust-lang#127230 (chore: remove duplicate words)
 - rust-lang#127243 (Add test for adt_const_params)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 2, 2024
…iaskrgr

Rollup of 10 pull requests

Successful merges:

 - rust-lang#126883 (Parenthesize break values containing leading label)
 - rust-lang#127136 (Fix `FnMut::call_mut`/`Fn::call` shim for async closures that capture references)
 - rust-lang#127146 (Uplift fast rejection to new solver)
 - rust-lang#127152 (Bootstrap: Try renaming the file if removing fails)
 - rust-lang#127168 (Use the aligned size for alloca at args/ret when the pass mode is cast)
 - rust-lang#127203 (Fix import suggestion error when path segment failed not from starting)
 - rust-lang#127212 (Update books)
 - rust-lang#127224 (Make `FloatTy` checks exhaustive in pretty print)
 - rust-lang#127230 (chore: remove duplicate words)
 - rust-lang#127243 (Add test for adt_const_params)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit f8f67b2 into rust-lang:master Jul 2, 2024
6 checks passed
@rustbot rustbot added this to the 1.81.0 milestone Jul 2, 2024
@dtolnay dtolnay deleted the breakvalue branch July 3, 2024 00:09
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.

4 participants