Skip to content

Commit 06e4ff4

Browse files
committedSep 27, 2019
Scope format! temporaries
This places the temporaries that `format!` generates to refer to its arguments (through `&dyn Trait`) in a short-lived scope surrounding just the invocation of `format!`. This enables `format!` to be used in generators without the temporaries preventing the generator from being `Send` (due to `dyn Trait` not being `Sync`). See #64477 for details.
1 parent a37fe2d commit 06e4ff4

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed
 

‎src/liballoc/macros.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,8 @@ macro_rules! vec {
9898
#[macro_export]
9999
#[stable(feature = "rust1", since = "1.0.0")]
100100
macro_rules! format {
101-
($($arg:tt)*) => ($crate::fmt::format($crate::__export::format_args!($($arg)*)))
101+
($($arg:tt)*) => {{
102+
let res = $crate::fmt::format($crate::__export::format_args!($($arg)*));
103+
res
104+
}}
102105
}

‎src/test/ui/fmt/issue-64477.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// In the past, the code generated by `format!` produced temporaries in the surrounding scope that
2+
// borrowed the arguments through `&dyn Trait`. These temporaries do not implement `Send`, which
3+
// meant that when `format!` was used in an async block, the resulting generator was not `Send`.
4+
// See https://github.com/rust-lang/rust/issues/64477#issuecomment-534669068 for details
5+
// and https://github.com/rust-lang/rust/issues/64477#issuecomment-531882958 for an example.
6+
async fn foo(_: String) {}
7+
8+
fn bar() -> impl Send {
9+
async move {
10+
foo(format!("{}:{}", 1, 2)).await;
11+
}
12+
}
13+
14+
fn main() {
15+
let _ = bar();
16+
}

0 commit comments

Comments
 (0)
Please sign in to comment.