Skip to content

Commit 96eed86

Browse files
committed
libcore/libstd: fix commas in macro_rules! macros
BREAKING CHANGE: (or perhaps, *bugfix*) In #![no_std] applications, the following calls to `panic!` used to behave differently; they now behave the same. Old behavior: panic!("{{"); // panics with "{{" panic!("{{",); // panics with "{" New behavior: panic!("{{"); // panics with "{{" panic!("{{",); // panics with "{{" This only affects calls to `panic!` (and by proxy `assert` and `debug_assert`) with a single string literal followed by a trailing comma, and only in `#![no_std]` applications.
1 parent 5fa97c3 commit 96eed86

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/libcore/macros.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ macro_rules! panic {
1919
($msg:expr) => ({
2020
$crate::panicking::panic(&($msg, file!(), line!(), __rust_unstable_column!()))
2121
});
22-
($fmt:expr, $($arg:tt)*) => ({
22+
($msg:expr,) => (
23+
panic!($msg)
24+
);
25+
($fmt:expr, $($arg:tt)+) => ({
2326
$crate::panicking::panic_fmt(format_args!($fmt, $($arg)*),
2427
&(file!(), line!(), __rust_unstable_column!()))
2528
});
@@ -79,6 +82,9 @@ macro_rules! assert {
7982
panic!(concat!("assertion failed: ", stringify!($cond)))
8083
}
8184
);
85+
($cond:expr,) => (
86+
assert!($cond)
87+
);
8288
($cond:expr, $($arg:tt)+) => (
8389
if !$cond {
8490
panic!($($arg)+)
@@ -359,7 +365,8 @@ macro_rules! try {
359365
$crate::result::Result::Err(err) => {
360366
return $crate::result::Result::Err($crate::convert::From::from(err))
361367
}
362-
})
368+
});
369+
($expr:expr,) => (try!($expr));
363370
}
364371

365372
/// Write formatted data into a buffer.
@@ -456,6 +463,9 @@ macro_rules! writeln {
456463
($dst:expr) => (
457464
write!($dst, "\n")
458465
);
466+
($dst:expr,) => (
467+
writeln!($dst)
468+
);
459469
($dst:expr, $fmt:expr) => (
460470
write!($dst, concat!($fmt, "\n"))
461471
);
@@ -524,6 +534,9 @@ macro_rules! unreachable {
524534
($msg:expr) => ({
525535
unreachable!("{}", $msg)
526536
});
537+
($msg:expr,) => ({
538+
unreachable!($msg)
539+
});
527540
($fmt:expr, $($arg:tt)*) => ({
528541
panic!(concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
529542
});

src/libstd/macros.rs

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ macro_rules! panic {
6868
($msg:expr) => ({
6969
$crate::rt::begin_panic($msg, &(file!(), line!(), __rust_unstable_column!()))
7070
});
71+
($msg:expr,) => ({
72+
panic!($msg)
73+
});
7174
($fmt:expr, $($arg:tt)+) => ({
7275
$crate::rt::begin_panic_fmt(&format_args!($fmt, $($arg)+),
7376
&(file!(), line!(), __rust_unstable_column!()))

0 commit comments

Comments
 (0)