Skip to content

Commit

Permalink
Scope format! temporaries
Browse files Browse the repository at this point in the history
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 rust-lang#64477 for details.
  • Loading branch information
jonhoo committed Sep 27, 2019
1 parent a37fe2d commit 06e4ff4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/liballoc/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,8 @@ macro_rules! vec {
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
macro_rules! format {
($($arg:tt)*) => ($crate::fmt::format($crate::__export::format_args!($($arg)*)))
($($arg:tt)*) => {{
let res = $crate::fmt::format($crate::__export::format_args!($($arg)*));
res
}}
}
16 changes: 16 additions & 0 deletions src/test/ui/fmt/issue-64477.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// In the past, the code generated by `format!` produced temporaries in the surrounding scope that
// borrowed the arguments through `&dyn Trait`. These temporaries do not implement `Send`, which
// meant that when `format!` was used in an async block, the resulting generator was not `Send`.
// See https://github.com/rust-lang/rust/issues/64477#issuecomment-534669068 for details
// and https://github.com/rust-lang/rust/issues/64477#issuecomment-531882958 for an example.
async fn foo(_: String) {}

fn bar() -> impl Send {
async move {
foo(format!("{}:{}", 1, 2)).await;
}
}

fn main() {
let _ = bar();
}

0 comments on commit 06e4ff4

Please sign in to comment.