Skip to content

Commit cb109a6

Browse files
committed
Shorten lifetime of panic temporaries in panic_fmt case
1 parent cbee2a1 commit cb109a6

File tree

5 files changed

+24
-45
lines changed

5 files changed

+24
-45
lines changed

library/core/src/panic.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ pub macro panic_2015 {
3535
("{}", $arg:expr $(,)?) => (
3636
$crate::panicking::panic_display(&$arg)
3737
),
38-
($fmt:expr, $($arg:tt)+) => (
39-
$crate::panicking::panic_fmt($crate::const_format_args!($fmt, $($arg)+))
40-
),
38+
($fmt:expr, $($arg:tt)+) => ({
39+
// Semicolon to prevent temporaries inside the formatting machinery from
40+
// being considered alive in the caller after the panic_fmt call.
41+
$crate::panicking::panic_fmt($crate::const_format_args!($fmt, $($arg)+));
42+
}),
4143
}
4244

4345
#[doc(hidden)]
@@ -53,9 +55,11 @@ pub macro panic_2021 {
5355
("{}", $arg:expr $(,)?) => (
5456
$crate::panicking::panic_display(&$arg)
5557
),
56-
($($t:tt)+) => (
57-
$crate::panicking::panic_fmt($crate::const_format_args!($($t)+))
58-
),
58+
($($t:tt)+) => ({
59+
// Semicolon to prevent temporaries inside the formatting machinery from
60+
// being considered alive in the caller after the panic_fmt call.
61+
$crate::panicking::panic_fmt($crate::const_format_args!($($t)+));
62+
}),
5963
}
6064

6165
#[doc(hidden)]

library/std/src/panic.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ pub macro panic_2015 {
2626
$crate::rt::panic_display(&$arg)
2727
}),
2828
($fmt:expr, $($arg:tt)+) => ({
29-
$crate::rt::panic_fmt($crate::const_format_args!($fmt, $($arg)+))
29+
// Semicolon to prevent temporaries inside the formatting machinery from
30+
// being considered alive in the caller after the panic_fmt call.
31+
$crate::rt::panic_fmt($crate::const_format_args!($fmt, $($arg)+));
3032
}),
3133
}
3234

tests/ui/macros/panic-temporaries.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// check-fail
1+
// check-pass
22
// edition:2021
33

44
#![allow(unreachable_code)]
@@ -15,5 +15,5 @@ async fn g() {
1515
fn require_send(_: impl Send) {}
1616

1717
fn main() {
18-
require_send(g()); //~ future cannot be sent between threads safely
18+
require_send(g());
1919
}

tests/ui/macros/panic-temporaries.stderr

-27
This file was deleted.

tests/ui/macros/rfc-2011-nicer-assert-messages/non-consuming-methods-have-optimized-codegen.stdout

+9-9
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn arbitrary_consuming_method_for_demonstration_purposes() {
2626

2727
{
2828
::std::rt::panic_fmt(format_args!("Assertion failed: elem as usize\nWith captures:\n elem = {0:?}\n",
29-
__capture0))
29+
__capture0));
3030
}
3131
}
3232
};
@@ -42,7 +42,7 @@ fn addr_of() {
4242
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
4343
{
4444
::std::rt::panic_fmt(format_args!("Assertion failed: &elem\nWith captures:\n elem = {0:?}\n",
45-
__capture0))
45+
__capture0));
4646
}
4747
}
4848
};
@@ -58,7 +58,7 @@ fn binary() {
5858
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
5959
{
6060
::std::rt::panic_fmt(format_args!("Assertion failed: elem == 1\nWith captures:\n elem = {0:?}\n",
61-
__capture0))
61+
__capture0));
6262
}
6363
}
6464
};
@@ -71,7 +71,7 @@ fn binary() {
7171
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
7272
{
7373
::std::rt::panic_fmt(format_args!("Assertion failed: elem >= 1\nWith captures:\n elem = {0:?}\n",
74-
__capture0))
74+
__capture0));
7575
}
7676
}
7777
};
@@ -84,7 +84,7 @@ fn binary() {
8484
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
8585
{
8686
::std::rt::panic_fmt(format_args!("Assertion failed: elem > 0\nWith captures:\n elem = {0:?}\n",
87-
__capture0))
87+
__capture0));
8888
}
8989
}
9090
};
@@ -97,7 +97,7 @@ fn binary() {
9797
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
9898
{
9999
::std::rt::panic_fmt(format_args!("Assertion failed: elem < 3\nWith captures:\n elem = {0:?}\n",
100-
__capture0))
100+
__capture0));
101101
}
102102
}
103103
};
@@ -110,7 +110,7 @@ fn binary() {
110110
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
111111
{
112112
::std::rt::panic_fmt(format_args!("Assertion failed: elem <= 3\nWith captures:\n elem = {0:?}\n",
113-
__capture0))
113+
__capture0));
114114
}
115115
}
116116
};
@@ -123,7 +123,7 @@ fn binary() {
123123
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
124124
{
125125
::std::rt::panic_fmt(format_args!("Assertion failed: elem != 3\nWith captures:\n elem = {0:?}\n",
126-
__capture0))
126+
__capture0));
127127
}
128128
}
129129
};
@@ -139,7 +139,7 @@ fn unary() {
139139
(&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
140140
{
141141
::std::rt::panic_fmt(format_args!("Assertion failed: *elem\nWith captures:\n elem = {0:?}\n",
142-
__capture0))
142+
__capture0));
143143
}
144144
}
145145
};

0 commit comments

Comments
 (0)