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

Format core and std macro rules, removing needless surrounding blocks #94868

Merged
merged 2 commits into from
Mar 16, 2022

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Mar 11, 2022

Many of the asserting and printing macros in core and std are written with prehistoric-looking formatting, like this:

macro_rules! println {
() => ($crate::print!("\n"));
($($arg:tt)*) => ({
$crate::io::_print($crate::format_args_nl!($($arg)*));
})
}

In modern Rust style this would conventionally be written as follows instead, always using braces and a trailing semicolon on the macro arms:

macro_rules! println {
() => {
$crate::print!("\n")
};
($($arg:tt)*) => {
$crate::io::_print($crate::format_args_nl!($($arg)*))
};
}

Getting rid of the unneeded braces inside the expansion reduces extraneous indentation in macro-expanded code. For example:

println!("repro {}", true);
// before:

{
    ::std::io::_print(
        ::core::fmt::Arguments::new_v1(
            &["repro ", "\n"],
            &[::core::fmt::ArgumentV1::new_display(&true)],
        ),
    );
};
// after:

::std::io::_print(
    ::core::fmt::Arguments::new_v1(
        &["repro ", "\n"],
        &[::core::fmt::ArgumentV1::new_display(&true)],
    ),
);

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 11, 2022
@rust-highfive
Copy link
Collaborator

r? @yaahc

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 11, 2022
Comment on lines -15 to 16
LL ~ Foo(2, b) => println!("{}", b)
LL ~ Foo(2, b) => println!("{}", b),
LL + Foo(_, _) => todo!()
Copy link
Member Author

Choose a reason for hiding this comment

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

This now avoids #94866

@Dylan-DPC
Copy link
Member

r? @Dylan-DPC

@rust-highfive rust-highfive assigned Dylan-DPC and unassigned yaahc Mar 13, 2022
@Dylan-DPC
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Mar 13, 2022

📌 Commit af53809 has been approved by Dylan-DPC

@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 Mar 13, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 13, 2022
Format core and std macro rules, removing needless surrounding blocks

Many of the asserting and printing macros in `core` and `std` are written with prehistoric-looking formatting, like this:

https://github.com/rust-lang/rust/blob/335ffbfa547df94ac236f5c56130cecf99c8d82b/library/std/src/macros.rs#L96-L101

In modern Rust style this would conventionally be written as follows instead, always using braces and a trailing semicolon on the macro arms:

https://github.com/rust-lang/rust/blob/af53809c874e0afb7be966df4d3cfcaa05277c53/library/std/src/macros.rs#L98-L105

Getting rid of the unneeded braces inside the expansion reduces extraneous indentation in macro-expanded code. For example:

```rust
println!("repro {}", true);
```

```rust
// before:

{
    ::std::io::_print(
        ::core::fmt::Arguments::new_v1(
            &["repro ", "\n"],
            &[::core::fmt::ArgumentV1::new_display(&true)],
        ),
    );
};
```

```rust
// after:

::std::io::_print(
    ::core::fmt::Arguments::new_v1(
        &["repro ", "\n"],
        &[::core::fmt::ArgumentV1::new_display(&true)],
    ),
);
```
@bors
Copy link
Contributor

bors commented Mar 13, 2022

⌛ Testing commit af53809 with merge 28838f68c28e8f45725d715555a73c55dbd9323d...

@bors
Copy link
Contributor

bors commented Mar 13, 2022

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Mar 13, 2022
@rust-log-analyzer

This comment has been minimized.

Copy link
Member Author

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

I don't know how to reproduce the run-make-fulldeps/coverage-reports failure on aarch64-gnu, but I noticed this snippet in the logs which looks relevant, so I will try to make the change that it shows.

--- expected_show_coverage.closure.txt	2022-03-13 10:25:55.192734369 +0000
+++ /checkout/obj/build/aarch64-unknown-linux-gnu/test/run-make-fulldeps/coverage-reports/coverage-reports/actual_show_coverage.closure.txt	2022-03-13 11:10:50.435023772 +0000
@@ -116,8 +116,8 @@
   116|      1|
   117|      1|    let
   118|      1|        _unused_closure
-  119|       |    =
-  120|       |        |
+  119|      1|    =
+  120|      1|        |
   121|       |            mut countdown
   122|       |        |
   123|      0|    {
@@ -173,7 +173,7 @@
   169|       |    ;
   170|       |
   171|      1|    let short_used_not_covered_closure_line_break_no_block_embedded_branch =
-  172|       |        | _unused_arg: u8 |
+  172|      1|        | _unused_arg: u8 |
   173|      0|            println!(
   174|      0|                "not called: {}",
   175|      0|                if is_true { "check" } else { "me" }
@@ -191,7 +191,7 @@
   187|       |    ;
   188|       |
   189|      1|    let short_used_covered_closure_line_break_no_block_embedded_branch =
-  190|      1|        | _unused_arg: u8 |
+  190|       |        | _unused_arg: u8 |
   191|      1|            println!(
   192|      1|                "not called: {}",
   193|      1|                if is_true { "check" } else { "me" }

@dtolnay
Copy link
Member Author

dtolnay commented Mar 16, 2022

@bors r=Dylan-DPC

@bors
Copy link
Contributor

bors commented Mar 16, 2022

📌 Commit ac5c657 has been approved by Dylan-DPC

@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 Mar 16, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 16, 2022
Rollup of 5 pull requests

Successful merges:

 - rust-lang#94868 (Format core and std macro rules, removing needless surrounding blocks)
 - rust-lang#94951 (Extend the irrefutable_let_patterns lint to let chains)
 - rust-lang#94955 (Refactor: Use `format_args_capture` in some parts of `rustc_parse`)
 - rust-lang#94957 (Improve the explanation about the behaviour of read_line)
 - rust-lang#94974 (Ensure that `let_else` does not interact with `let_chains`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit f986c74 into rust-lang:master Mar 16, 2022
@rustbot rustbot added this to the 1.61.0 milestone Mar 16, 2022
@dtolnay dtolnay deleted the noblock branch March 22, 2022 20:51
bors added a commit to rust-lang-ci/rust that referenced this pull request May 1, 2022
…lacrum

Make [e]println macros eagerly drop temporaries (for backport)

This PR extracts the subset of rust-lang#96455 which is only the parts necessary for fixing the 1.61-beta regressions in rust-lang#96434.

My larger PR rust-lang#96455 contains a few other changes relative to the pre-rust-lang#94868 behavior; those are not necessary to backport into 1.61.

argument position | before rust-lang#94868 | after rust-lang#94868 | after this PR
--- |:---:|:---:|:---:
`write!($tmp, "…", …)` | :rage: | :rage: | :rage:
`write!(…, "…", $tmp)` | :rage: | :rage: | :rage:
`writeln!($tmp, "…", …)` | :rage: | :rage: | :rage:
`writeln!(…, "…", $tmp)` | :rage: | :rage: | :rage:
`print!("…", $tmp)` | :rage: | :rage: | :rage:
`println!("…", $tmp)` | :smiley_cat: | :rage: | :smiley_cat:
`eprint!("…", $tmp)` | :rage: | :rage: | :rage:
`eprintln!("…", $tmp)` | :smiley_cat: | :rage: | :smiley_cat:
`panic!("…", $tmp)` | :smiley_cat: | :smiley_cat: | :smiley_cat:
ehuss pushed a commit to ehuss/rust that referenced this pull request May 4, 2022
…lacrum

Make [e]println macros eagerly drop temporaries (for backport)

This PR extracts the subset of rust-lang#96455 which is only the parts necessary for fixing the 1.61-beta regressions in rust-lang#96434.

My larger PR rust-lang#96455 contains a few other changes relative to the pre-rust-lang#94868 behavior; those are not necessary to backport into 1.61.

argument position | before rust-lang#94868 | after rust-lang#94868 | after this PR
--- |:---:|:---:|:---:
`write!($tmp, "…", …)` | :rage: | :rage: | :rage:
`write!(…, "…", $tmp)` | :rage: | :rage: | :rage:
`writeln!($tmp, "…", …)` | :rage: | :rage: | :rage:
`writeln!(…, "…", $tmp)` | :rage: | :rage: | :rage:
`print!("…", $tmp)` | :rage: | :rage: | :rage:
`println!("…", $tmp)` | :smiley_cat: | :rage: | :smiley_cat:
`eprint!("…", $tmp)` | :rage: | :rage: | :rage:
`eprintln!("…", $tmp)` | :smiley_cat: | :rage: | :smiley_cat:
`panic!("…", $tmp)` | :smiley_cat: | :smiley_cat: | :smiley_cat:
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request May 12, 2022
Make write/print macros eagerly drop temporaries

This PR fixes the 2 regressions in rust-lang#96434 (`println` and `eprintln`) and changes all the other similar macros (`write`, `writeln`, `print`, `eprint`) to match the old pre-rust-lang#94868 behavior of `println` and `eprintln`.

argument position | before rust-lang#94868 | after rust-lang#94868 | after this PR
--- |:---:|:---:|:---:
`write!($tmp, "…", …)` | :rage: | :rage: | :smiley_cat:
`write!(…, "…", $tmp)` | :rage: | :rage: | :smiley_cat:
`writeln!($tmp, "…", …)` | :rage: | :rage: | :smiley_cat:
`writeln!(…, "…", $tmp)` | :rage: | :rage: | :smiley_cat:
`print!("…", $tmp)` | :rage: | :rage: | :smiley_cat:
`println!("…", $tmp)` | :smiley_cat: | :rage: | :smiley_cat:
`eprint!("…", $tmp)` | :rage: | :rage: | :smiley_cat:
`eprintln!("…", $tmp)` | :smiley_cat: | :rage: | :smiley_cat:
`panic!("…", $tmp)` | :smiley_cat: | :smiley_cat: | :smiley_cat:

Example of code that is affected by this change:

```rust
use std::sync::Mutex;

fn main() {
    let mutex = Mutex::new(0);
    print!("{}", mutex.lock().unwrap()) /* no semicolon */
}
```

You can see several real-world examples like this in the Crater links at the top of rust-lang#96434. This code failed to compile prior to this PR as follows, but works after this PR.

```console
error[E0597]: `mutex` does not live long enough
 --> src/main.rs:5:18
  |
5 |     print!("{}", mutex.lock().unwrap()) /* no semicolon */
  |                  ^^^^^^^^^^^^---------
  |                  |
  |                  borrowed value does not live long enough
  |                  a temporary with access to the borrow is created here ...
6 | }
  | -
  | |
  | `mutex` dropped here while still borrowed
  | ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `MutexGuard`
```
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request May 14, 2022
Make write/print macros eagerly drop temporaries

This PR fixes the 2 regressions in rust-lang#96434 (`println` and `eprintln`) and changes all the other similar macros (`write`, `writeln`, `print`, `eprint`) to match the old pre-rust-lang#94868 behavior of `println` and `eprintln`.

argument position | before rust-lang#94868 | after rust-lang#94868 | after this PR
--- |:---:|:---:|:---:
`write!($tmp, "…", …)` | :rage: | :rage: | :smiley_cat:
`write!(…, "…", $tmp)` | :rage: | :rage: | :smiley_cat:
`writeln!($tmp, "…", …)` | :rage: | :rage: | :smiley_cat:
`writeln!(…, "…", $tmp)` | :rage: | :rage: | :smiley_cat:
`print!("…", $tmp)` | :rage: | :rage: | :smiley_cat:
`println!("…", $tmp)` | :smiley_cat: | :rage: | :smiley_cat:
`eprint!("…", $tmp)` | :rage: | :rage: | :smiley_cat:
`eprintln!("…", $tmp)` | :smiley_cat: | :rage: | :smiley_cat:
`panic!("…", $tmp)` | :smiley_cat: | :smiley_cat: | :smiley_cat:

Example of code that is affected by this change:

```rust
use std::sync::Mutex;

fn main() {
    let mutex = Mutex::new(0);
    print!("{}", mutex.lock().unwrap()) /* no semicolon */
}
```

You can see several real-world examples like this in the Crater links at the top of rust-lang#96434. This code failed to compile prior to this PR as follows, but works after this PR.

```console
error[E0597]: `mutex` does not live long enough
 --> src/main.rs:5:18
  |
5 |     print!("{}", mutex.lock().unwrap()) /* no semicolon */
  |                  ^^^^^^^^^^^^---------
  |                  |
  |                  borrowed value does not live long enough
  |                  a temporary with access to the borrow is created here ...
6 | }
  | -
  | |
  | `mutex` dropped here while still borrowed
  | ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `MutexGuard`
```
MabezDev pushed a commit to esp-rs/rust that referenced this pull request May 17, 2022
…lacrum

Make [e]println macros eagerly drop temporaries (for backport)

This PR extracts the subset of rust-lang#96455 which is only the parts necessary for fixing the 1.61-beta regressions in rust-lang#96434.

My larger PR rust-lang#96455 contains a few other changes relative to the pre-rust-lang#94868 behavior; those are not necessary to backport into 1.61.

argument position | before rust-lang#94868 | after rust-lang#94868 | after this PR
--- |:---:|:---:|:---:
`write!($tmp, "…", …)` | :rage: | :rage: | :rage:
`write!(…, "…", $tmp)` | :rage: | :rage: | :rage:
`writeln!($tmp, "…", …)` | :rage: | :rage: | :rage:
`writeln!(…, "…", $tmp)` | :rage: | :rage: | :rage:
`print!("…", $tmp)` | :rage: | :rage: | :rage:
`println!("…", $tmp)` | :smiley_cat: | :rage: | :smiley_cat:
`eprint!("…", $tmp)` | :rage: | :rage: | :rage:
`eprintln!("…", $tmp)` | :smiley_cat: | :rage: | :smiley_cat:
`panic!("…", $tmp)` | :smiley_cat: | :smiley_cat: | :smiley_cat:
bors added a commit to rust-lang-ci/rust that referenced this pull request May 23, 2022
Make write/print macros eagerly drop temporaries

This PR fixes the 2 regressions in rust-lang#96434 (`println` and `eprintln`) and changes all the other similar macros (`write`, `writeln`, `print`, `eprint`) to match the old pre-rust-lang#94868 behavior of `println` and `eprintln`.

argument position | before rust-lang#94868 | after rust-lang#94868 | after this PR
--- |:---:|:---:|:---:
`write!($tmp, "…", …)` | :rage: | :rage: | :smiley_cat:
`write!(…, "…", $tmp)` | :rage: | :rage: | :smiley_cat:
`writeln!($tmp, "…", …)` | :rage: | :rage: | :smiley_cat:
`writeln!(…, "…", $tmp)` | :rage: | :rage: | :smiley_cat:
`print!("…", $tmp)` | :rage: | :rage: | :smiley_cat:
`println!("…", $tmp)` | :smiley_cat: | :rage: | :smiley_cat:
`eprint!("…", $tmp)` | :rage: | :rage: | :smiley_cat:
`eprintln!("…", $tmp)` | :smiley_cat: | :rage: | :smiley_cat:
`panic!("…", $tmp)` | :smiley_cat: | :smiley_cat: | :smiley_cat:

Example of code that is affected by this change:

```rust
use std::sync::Mutex;

fn main() {
    let mutex = Mutex::new(0);
    print!("{}", mutex.lock().unwrap()) /* no semicolon */
}
```

You can see several real-world examples like this in the Crater links at the top of rust-lang#96434. This code failed to compile prior to this PR as follows, but works after this PR.

```console
error[E0597]: `mutex` does not live long enough
 --> src/main.rs:5:18
  |
5 |     print!("{}", mutex.lock().unwrap()) /* no semicolon */
  |                  ^^^^^^^^^^^^---------
  |                  |
  |                  borrowed value does not live long enough
  |                  a temporary with access to the borrow is created here ...
6 | }
  | -
  | |
  | `mutex` dropped here while still borrowed
  | ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `MutexGuard`
```
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Jul 28, 2022
Revert `write!` and `writeln!` to late drop temporaries

Closes (on master, but not on beta) rust-lang#99684 by reverting the `write!` and `writeln!` parts of rust-lang#96455.

argument position | before<br>rust-lang#94868 | after<br>rust-lang#94868 | after<br>rust-lang#96455 | after<br>this PR | desired<br>(unimplementable)
--- |:---:|:---:|:---:|:---:|:---:
`write!($tmp, "…", …)` | **⸺late** | **⸺late** | *early⸺* | **⸺late** | **⸺late**
`write!(…, "…", $tmp)` | **⸺late** | **⸺late** | *early⸺* | **⸺late** | *early⸺*
`writeln!($tmp, "…", …)` | **⸺late** | **⸺late** | *early⸺* | **⸺late** | **⸺late**
`writeln!(…, "…", $tmp)` | **⸺late** | **⸺late** | *early⸺* | **⸺late** | *early⸺*
`print!("…", $tmp)` | **⸺late** | **⸺late** | *early⸺* | *early⸺* | *early⸺*
`println!("…", $tmp)` | *early⸺* | **⸺late** | *early⸺* | *early⸺* | *early⸺*
`eprint!("…", $tmp)` | **⸺late** | **⸺late** | *early⸺* | *early⸺* | *early⸺*
`eprintln!("…", $tmp)` | *early⸺* | **⸺late**| *early⸺* | *early⸺* | *early⸺*
`panic!("…", $tmp)` | *early⸺* | *early⸺* | *early⸺* | *early⸺* | *early⸺*

"Late drop" refers to dropping temporaries at the nearest semicolon **outside** of the macro invocation.

"Early drop" refers to dropping temporaries inside of the macro invocation.
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