From 04c0018d1b08875b6e51205d2b62b67925b6238a Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Mon, 26 Oct 2020 07:15:37 +0900 Subject: [PATCH] Use ? in core/std macros --- library/core/src/macros/mod.rs | 59 ++++++++------------------- library/std/src/macros.rs | 7 +--- src/test/ui/parser/issue-62894.stderr | 2 +- 3 files changed, 19 insertions(+), 49 deletions(-) diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs index ac45e819cf6..bce8a70e92f 100644 --- a/library/core/src/macros/mod.rs +++ b/library/core/src/macros/mod.rs @@ -6,15 +6,12 @@ macro_rules! panic { () => ( $crate::panic!("explicit panic") ); - ($msg:literal) => ( + ($msg:literal $(,)?) => ( $crate::panicking::panic($msg) ); - ($msg:expr) => ( + ($msg:expr $(,)?) => ( $crate::panicking::panic_str($msg) ); - ($msg:expr,) => ( - $crate::panic!($msg) - ); ($fmt:expr, $($arg:tt)+) => ( $crate::panicking::panic_fmt($crate::format_args!($fmt, $($arg)+)) ); @@ -40,7 +37,7 @@ macro_rules! panic { #[macro_export] #[stable(feature = "rust1", since = "1.0.0")] macro_rules! assert_eq { - ($left:expr, $right:expr) => ({ + ($left:expr, $right:expr $(,)?) => ({ match (&$left, &$right) { (left_val, right_val) => { if !(*left_val == *right_val) { @@ -54,9 +51,6 @@ macro_rules! assert_eq { } } }); - ($left:expr, $right:expr,) => ({ - $crate::assert_eq!($left, $right) - }); ($left:expr, $right:expr, $($arg:tt)+) => ({ match (&($left), &($right)) { (left_val, right_val) => { @@ -94,7 +88,7 @@ macro_rules! assert_eq { #[macro_export] #[stable(feature = "assert_ne", since = "1.13.0")] macro_rules! assert_ne { - ($left:expr, $right:expr) => ({ + ($left:expr, $right:expr $(,)?) => ({ match (&$left, &$right) { (left_val, right_val) => { if *left_val == *right_val { @@ -108,9 +102,6 @@ macro_rules! assert_ne { } } }); - ($left:expr, $right:expr,) => { - $crate::assert_ne!($left, $right) - }; ($left:expr, $right:expr, $($arg:tt)+) => ({ match (&($left), &($right)) { (left_val, right_val) => { @@ -315,7 +306,7 @@ macro_rules! matches { #[rustc_deprecated(since = "1.39.0", reason = "use the `?` operator instead")] #[doc(alias = "?")] macro_rules! r#try { - ($expr:expr) => { + ($expr:expr $(,)?) => { match $expr { $crate::result::Result::Ok(val) => val, $crate::result::Result::Err(err) => { @@ -323,9 +314,6 @@ macro_rules! r#try { } } }; - ($expr:expr,) => { - $crate::r#try!($expr) - }; } /// Writes formatted data into a buffer. @@ -451,12 +439,9 @@ macro_rules! write { #[stable(feature = "rust1", since = "1.0.0")] #[allow_internal_unstable(format_args_nl)] macro_rules! writeln { - ($dst:expr) => ( + ($dst:expr $(,)?) => ( $crate::write!($dst, "\n") ); - ($dst:expr,) => ( - $crate::writeln!($dst) - ); ($dst:expr, $($arg:tt)*) => ( $dst.write_fmt($crate::format_args_nl!($($arg)*)) ); @@ -517,12 +502,9 @@ macro_rules! unreachable { () => ({ panic!("internal error: entered unreachable code") }); - ($msg:expr) => ({ + ($msg:expr $(,)?) => ({ $crate::unreachable!("{}", $msg) }); - ($msg:expr,) => ({ - $crate::unreachable!($msg) - }); ($fmt:expr, $($arg:tt)*) => ({ panic!($crate::concat!("internal error: entered unreachable code: ", $fmt), $($arg)*) }); @@ -711,8 +693,7 @@ pub(crate) mod builtin { #[rustc_builtin_macro] #[macro_export] macro_rules! compile_error { - ($msg:expr) => {{ /* compiler built-in */ }}; - ($msg:expr,) => {{ /* compiler built-in */ }}; + ($msg:expr $(,)?) => {{ /* compiler built-in */ }}; } /// Constructs parameters for the other string-formatting macros. @@ -816,8 +797,7 @@ macro_rules! format_args_nl { #[rustc_builtin_macro] #[macro_export] macro_rules! env { - ($name:expr) => {{ /* compiler built-in */ }}; - ($name:expr,) => {{ /* compiler built-in */ }}; + ($name:expr $(,)?) => {{ /* compiler built-in */ }}; } /// Optionally inspects an environment variable at compile time. @@ -841,8 +821,7 @@ macro_rules! env { #[rustc_builtin_macro] #[macro_export] macro_rules! option_env { - ($name:expr) => {{ /* compiler built-in */ }}; - ($name:expr,) => {{ /* compiler built-in */ }}; + ($name:expr $(,)?) => {{ /* compiler built-in */ }}; } /// Concatenates identifiers into one identifier. @@ -877,8 +856,7 @@ macro_rules! option_env { #[rustc_builtin_macro] #[macro_export] macro_rules! concat_idents { - ($($e:ident),+) => {{ /* compiler built-in */ }}; - ($($e:ident,)+) => {{ /* compiler built-in */ }}; + ($($e:ident),+ $(,)?) => {{ /* compiler built-in */ }}; } /// Concatenates literals into a static string slice. @@ -900,8 +878,7 @@ macro_rules! concat_idents { #[rustc_builtin_macro] #[macro_export] macro_rules! concat { - ($($e:expr),*) => {{ /* compiler built-in */ }}; - ($($e:expr,)*) => {{ /* compiler built-in */ }}; + ($($e:expr),* $(,)?) => {{ /* compiler built-in */ }}; } /// Expands to the line number on which it was invoked. @@ -1043,8 +1020,7 @@ macro_rules! stringify { #[rustc_builtin_macro] #[macro_export] macro_rules! include_str { - ($file:expr) => {{ /* compiler built-in */ }}; - ($file:expr,) => {{ /* compiler built-in */ }}; + ($file:expr $(,)?) => {{ /* compiler built-in */ }}; } /// Includes a file as a reference to a byte array. @@ -1083,8 +1059,7 @@ macro_rules! include_str { #[rustc_builtin_macro] #[macro_export] macro_rules! include_bytes { - ($file:expr) => {{ /* compiler built-in */ }}; - ($file:expr,) => {{ /* compiler built-in */ }}; + ($file:expr $(,)?) => {{ /* compiler built-in */ }}; } /// Expands to a string that represents the current module path. @@ -1191,8 +1166,7 @@ macro_rules! cfg { #[rustc_builtin_macro] #[macro_export] macro_rules! include { - ($file:expr) => {{ /* compiler built-in */ }}; - ($file:expr,) => {{ /* compiler built-in */ }}; + ($file:expr $(,)?) => {{ /* compiler built-in */ }}; } /// Asserts that a boolean expression is `true` at runtime. @@ -1242,8 +1216,7 @@ macro_rules! include { #[rustc_builtin_macro] #[macro_export] macro_rules! assert { - ($cond:expr) => {{ /* compiler built-in */ }}; - ($cond:expr,) => {{ /* compiler built-in */ }}; + ($cond:expr $(,)?) => {{ /* compiler built-in */ }}; ($cond:expr, $($arg:tt)+) => {{ /* compiler built-in */ }}; } diff --git a/library/std/src/macros.rs b/library/std/src/macros.rs index e8898d98ff3..57649d6f8f2 100644 --- a/library/std/src/macros.rs +++ b/library/std/src/macros.rs @@ -10,8 +10,7 @@ #[allow_internal_unstable(libstd_sys_internals)] macro_rules! panic { () => ({ $crate::panic!("explicit panic") }); - ($msg:expr) => ({ $crate::rt::begin_panic($msg) }); - ($msg:expr,) => ({ $crate::panic!($msg) }); + ($msg:expr $(,)?) => ({ $crate::rt::begin_panic($msg) }); ($fmt:expr, $($arg:tt)+) => ({ $crate::rt::begin_panic_fmt(&$crate::format_args!($fmt, $($arg)+)) }); @@ -285,7 +284,7 @@ macro_rules! dbg { () => { $crate::eprintln!("[{}:{}]", $crate::file!(), $crate::line!()); }; - ($val:expr) => { + ($val:expr $(,)?) => { // Use of `match` here is intentional because it affects the lifetimes // of temporaries - https://stackoverflow.com/a/48732525/1063961 match $val { @@ -296,8 +295,6 @@ macro_rules! dbg { } } }; - // Trailing comma with single argument is ignored - ($val:expr,) => { $crate::dbg!($val) }; ($($val:expr),+ $(,)?) => { ($($crate::dbg!($val)),+,) }; diff --git a/src/test/ui/parser/issue-62894.stderr b/src/test/ui/parser/issue-62894.stderr index 93d43bda32d..cf3727c9d57 100644 --- a/src/test/ui/parser/issue-62894.stderr +++ b/src/test/ui/parser/issue-62894.stderr @@ -45,7 +45,7 @@ LL | fn main() {} | ::: $SRC_DIR/core/src/macros/mod.rs:LL:COL | -LL | ($left:expr, $right:expr) => ({ +LL | ($left:expr, $right:expr $(,)?) => ({ | ---------- while parsing argument for this `expr` macro fragment error: aborting due to 4 previous errors -- 2.44.0