Skip to content

Commit 4d1e052

Browse files
committed
add test for &format!(...) in an if expression's block
This is the real-world way that the unintentional stabilization of lifetime extension for format arguments was used.
1 parent 3493250 commit 4d1e052

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0716]: temporary value dropped while borrowed
2+
--> $DIR/format-args-temporary-scopes.rs:25:41
3+
|
4+
LL | println!("{:?}{:?}", (), if true { &format!("") } else { "" });
5+
| -----------^^^^^^^^^^^--------------
6+
| | | |
7+
| | | temporary value is freed at the end of this statement
8+
| | creates a temporary value which is freed while still in use
9+
| borrow later used here
10+
|
11+
= note: consider using a `let` binding to create a longer lived value
12+
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
13+
14+
error: aborting due to 1 previous error
15+
16+
For more information about this error, try `rustc --explain E0716`.

tests/ui/borrowck/format-args-temporary-scopes.e2024.stderr

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0716]: temporary value dropped while borrowed
2-
--> $DIR/format-args-temporary-scopes.rs:13:25
2+
--> $DIR/format-args-temporary-scopes.rs:12:25
33
|
44
LL | println!("{:?}", { &temp() });
55
| ---^^^^^---
@@ -11,7 +11,7 @@ LL | println!("{:?}", { &temp() });
1111
= note: consider using a `let` binding to create a longer lived value
1212

1313
error[E0716]: temporary value dropped while borrowed
14-
--> $DIR/format-args-temporary-scopes.rs:19:29
14+
--> $DIR/format-args-temporary-scopes.rs:18:29
1515
|
1616
LL | println!("{:?}{:?}", { &temp() }, ());
1717
| ---^^^^^---
@@ -22,6 +22,19 @@ LL | println!("{:?}{:?}", { &temp() }, ());
2222
|
2323
= note: consider using a `let` binding to create a longer lived value
2424

25-
error: aborting due to 2 previous errors
25+
error[E0716]: temporary value dropped while borrowed
26+
--> $DIR/format-args-temporary-scopes.rs:25:41
27+
|
28+
LL | println!("{:?}{:?}", (), if true { &format!("") } else { "" });
29+
| -^^^^^^^^^^-
30+
| || |
31+
| || temporary value is freed at the end of this statement
32+
| |creates a temporary value which is freed while still in use
33+
| borrow later used here
34+
|
35+
= note: consider using a `let` binding to create a longer lived value
36+
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
37+
38+
error: aborting due to 3 previous errors
2639

2740
For more information about this error, try `rustc --explain E0716`.

tests/ui/borrowck/format-args-temporary-scopes.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Test for #145784 as it relates to format arguments: arguments to macros such as `println!`
22
//! should obey normal temporary scoping rules.
33
//@ revisions: e2021 e2024
4-
//@ [e2021] check-pass
54
//@ [e2021] edition: 2021
65
//@ [e2024] edition: 2024
76

@@ -18,4 +17,11 @@ fn main() {
1817
// outlive the result of the block, making this compile.
1918
println!("{:?}{:?}", { &temp() }, ());
2019
//[e2024]~^ ERROR: temporary value dropped while borrowed [E0716]
20+
21+
// In real-world projects, this typically appeared in `if` expressions with a `&str` in one
22+
// branch and a reference to a `String` temporary in the other. Since the consequent and `else`
23+
// blocks of `if` expressions are temporary scopes in all editions, this affects Rust 2021 and
24+
// earlier as well.
25+
println!("{:?}{:?}", (), if true { &format!("") } else { "" });
26+
//~^ ERROR: temporary value dropped while borrowed [E0716]
2127
}

0 commit comments

Comments
 (0)