From 96eed862a08f0ee1d234f4f83419dd46fe58ccef Mon Sep 17 00:00:00 2001 From: Michael Lamparski Date: Wed, 7 Feb 2018 09:31:22 -0500 Subject: [PATCH] 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. --- src/libcore/macros.rs | 17 +++++++++++++++-- src/libstd/macros.rs | 3 +++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index f00128a8147d..c12edaf0b29a 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -19,7 +19,10 @@ macro_rules! panic { ($msg:expr) => ({ $crate::panicking::panic(&($msg, file!(), line!(), __rust_unstable_column!())) }); - ($fmt:expr, $($arg:tt)*) => ({ + ($msg:expr,) => ( + panic!($msg) + ); + ($fmt:expr, $($arg:tt)+) => ({ $crate::panicking::panic_fmt(format_args!($fmt, $($arg)*), &(file!(), line!(), __rust_unstable_column!())) }); @@ -79,6 +82,9 @@ macro_rules! assert { panic!(concat!("assertion failed: ", stringify!($cond))) } ); + ($cond:expr,) => ( + assert!($cond) + ); ($cond:expr, $($arg:tt)+) => ( if !$cond { panic!($($arg)+) @@ -359,7 +365,8 @@ macro_rules! try { $crate::result::Result::Err(err) => { return $crate::result::Result::Err($crate::convert::From::from(err)) } - }) + }); + ($expr:expr,) => (try!($expr)); } /// Write formatted data into a buffer. @@ -456,6 +463,9 @@ macro_rules! writeln { ($dst:expr) => ( write!($dst, "\n") ); + ($dst:expr,) => ( + writeln!($dst) + ); ($dst:expr, $fmt:expr) => ( write!($dst, concat!($fmt, "\n")) ); @@ -524,6 +534,9 @@ macro_rules! unreachable { ($msg:expr) => ({ unreachable!("{}", $msg) }); + ($msg:expr,) => ({ + unreachable!($msg) + }); ($fmt:expr, $($arg:tt)*) => ({ panic!(concat!("internal error: entered unreachable code: ", $fmt), $($arg)*) }); diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index f058b1caef50..5a01674a3d0a 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -68,6 +68,9 @@ macro_rules! panic { ($msg:expr) => ({ $crate::rt::begin_panic($msg, &(file!(), line!(), __rust_unstable_column!())) }); + ($msg:expr,) => ({ + panic!($msg) + }); ($fmt:expr, $($arg:tt)+) => ({ $crate::rt::begin_panic_fmt(&format_args!($fmt, $($arg)+), &(file!(), line!(), __rust_unstable_column!()))