1
Fork 0

Use ? in core/std macros

This commit is contained in:
Taiki Endo 2020-10-26 07:15:37 +09:00
parent 430feb24a4
commit 04c0018d1b
3 changed files with 19 additions and 49 deletions

View file

@ -6,15 +6,12 @@ macro_rules! panic {
() => ( () => (
$crate::panic!("explicit panic") $crate::panic!("explicit panic")
); );
($msg:literal) => ( ($msg:literal $(,)?) => (
$crate::panicking::panic($msg) $crate::panicking::panic($msg)
); );
($msg:expr) => ( ($msg:expr $(,)?) => (
$crate::panicking::panic_str($msg) $crate::panicking::panic_str($msg)
); );
($msg:expr,) => (
$crate::panic!($msg)
);
($fmt:expr, $($arg:tt)+) => ( ($fmt:expr, $($arg:tt)+) => (
$crate::panicking::panic_fmt($crate::format_args!($fmt, $($arg)+)) $crate::panicking::panic_fmt($crate::format_args!($fmt, $($arg)+))
); );
@ -40,7 +37,7 @@ macro_rules! panic {
#[macro_export] #[macro_export]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
macro_rules! assert_eq { macro_rules! assert_eq {
($left:expr, $right:expr) => ({ ($left:expr, $right:expr $(,)?) => ({
match (&$left, &$right) { match (&$left, &$right) {
(left_val, right_val) => { (left_val, right_val) => {
if !(*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)+) => ({ ($left:expr, $right:expr, $($arg:tt)+) => ({
match (&($left), &($right)) { match (&($left), &($right)) {
(left_val, right_val) => { (left_val, right_val) => {
@ -94,7 +88,7 @@ macro_rules! assert_eq {
#[macro_export] #[macro_export]
#[stable(feature = "assert_ne", since = "1.13.0")] #[stable(feature = "assert_ne", since = "1.13.0")]
macro_rules! assert_ne { macro_rules! assert_ne {
($left:expr, $right:expr) => ({ ($left:expr, $right:expr $(,)?) => ({
match (&$left, &$right) { match (&$left, &$right) {
(left_val, right_val) => { (left_val, right_val) => {
if *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)+) => ({ ($left:expr, $right:expr, $($arg:tt)+) => ({
match (&($left), &($right)) { match (&($left), &($right)) {
(left_val, right_val) => { (left_val, right_val) => {
@ -315,7 +306,7 @@ macro_rules! matches {
#[rustc_deprecated(since = "1.39.0", reason = "use the `?` operator instead")] #[rustc_deprecated(since = "1.39.0", reason = "use the `?` operator instead")]
#[doc(alias = "?")] #[doc(alias = "?")]
macro_rules! r#try { macro_rules! r#try {
($expr:expr) => { ($expr:expr $(,)?) => {
match $expr { match $expr {
$crate::result::Result::Ok(val) => val, $crate::result::Result::Ok(val) => val,
$crate::result::Result::Err(err) => { $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. /// Writes formatted data into a buffer.
@ -451,12 +439,9 @@ macro_rules! write {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[allow_internal_unstable(format_args_nl)] #[allow_internal_unstable(format_args_nl)]
macro_rules! writeln { macro_rules! writeln {
($dst:expr) => ( ($dst:expr $(,)?) => (
$crate::write!($dst, "\n") $crate::write!($dst, "\n")
); );
($dst:expr,) => (
$crate::writeln!($dst)
);
($dst:expr, $($arg:tt)*) => ( ($dst:expr, $($arg:tt)*) => (
$dst.write_fmt($crate::format_args_nl!($($arg)*)) $dst.write_fmt($crate::format_args_nl!($($arg)*))
); );
@ -517,12 +502,9 @@ macro_rules! unreachable {
() => ({ () => ({
panic!("internal error: entered unreachable code") panic!("internal error: entered unreachable code")
}); });
($msg:expr) => ({ ($msg:expr $(,)?) => ({
$crate::unreachable!("{}", $msg) $crate::unreachable!("{}", $msg)
}); });
($msg:expr,) => ({
$crate::unreachable!($msg)
});
($fmt:expr, $($arg:tt)*) => ({ ($fmt:expr, $($arg:tt)*) => ({
panic!($crate::concat!("internal error: entered unreachable code: ", $fmt), $($arg)*) panic!($crate::concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
}); });
@ -711,8 +693,7 @@ pub(crate) mod builtin {
#[rustc_builtin_macro] #[rustc_builtin_macro]
#[macro_export] #[macro_export]
macro_rules! compile_error { 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. /// Constructs parameters for the other string-formatting macros.
@ -816,8 +797,7 @@ pub(crate) mod builtin {
#[rustc_builtin_macro] #[rustc_builtin_macro]
#[macro_export] #[macro_export]
macro_rules! env { 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. /// Optionally inspects an environment variable at compile time.
@ -841,8 +821,7 @@ pub(crate) mod builtin {
#[rustc_builtin_macro] #[rustc_builtin_macro]
#[macro_export] #[macro_export]
macro_rules! option_env { macro_rules! option_env {
($name:expr) => {{ /* compiler built-in */ }}; ($name:expr $(,)?) => {{ /* compiler built-in */ }};
($name:expr,) => {{ /* compiler built-in */ }};
} }
/// Concatenates identifiers into one identifier. /// Concatenates identifiers into one identifier.
@ -877,8 +856,7 @@ pub(crate) mod builtin {
#[rustc_builtin_macro] #[rustc_builtin_macro]
#[macro_export] #[macro_export]
macro_rules! concat_idents { 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. /// Concatenates literals into a static string slice.
@ -900,8 +878,7 @@ pub(crate) mod builtin {
#[rustc_builtin_macro] #[rustc_builtin_macro]
#[macro_export] #[macro_export]
macro_rules! concat { 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. /// Expands to the line number on which it was invoked.
@ -1043,8 +1020,7 @@ pub(crate) mod builtin {
#[rustc_builtin_macro] #[rustc_builtin_macro]
#[macro_export] #[macro_export]
macro_rules! include_str { 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. /// Includes a file as a reference to a byte array.
@ -1083,8 +1059,7 @@ pub(crate) mod builtin {
#[rustc_builtin_macro] #[rustc_builtin_macro]
#[macro_export] #[macro_export]
macro_rules! include_bytes { 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. /// Expands to a string that represents the current module path.
@ -1191,8 +1166,7 @@ pub(crate) mod builtin {
#[rustc_builtin_macro] #[rustc_builtin_macro]
#[macro_export] #[macro_export]
macro_rules! include { 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. /// Asserts that a boolean expression is `true` at runtime.
@ -1242,8 +1216,7 @@ pub(crate) mod builtin {
#[rustc_builtin_macro] #[rustc_builtin_macro]
#[macro_export] #[macro_export]
macro_rules! assert { 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 */ }}; ($cond:expr, $($arg:tt)+) => {{ /* compiler built-in */ }};
} }

View file

@ -10,8 +10,7 @@
#[allow_internal_unstable(libstd_sys_internals)] #[allow_internal_unstable(libstd_sys_internals)]
macro_rules! panic { macro_rules! panic {
() => ({ $crate::panic!("explicit panic") }); () => ({ $crate::panic!("explicit panic") });
($msg:expr) => ({ $crate::rt::begin_panic($msg) }); ($msg:expr $(,)?) => ({ $crate::rt::begin_panic($msg) });
($msg:expr,) => ({ $crate::panic!($msg) });
($fmt:expr, $($arg:tt)+) => ({ ($fmt:expr, $($arg:tt)+) => ({
$crate::rt::begin_panic_fmt(&$crate::format_args!($fmt, $($arg)+)) $crate::rt::begin_panic_fmt(&$crate::format_args!($fmt, $($arg)+))
}); });
@ -285,7 +284,7 @@ macro_rules! dbg {
() => { () => {
$crate::eprintln!("[{}:{}]", $crate::file!(), $crate::line!()); $crate::eprintln!("[{}:{}]", $crate::file!(), $crate::line!());
}; };
($val:expr) => { ($val:expr $(,)?) => {
// Use of `match` here is intentional because it affects the lifetimes // Use of `match` here is intentional because it affects the lifetimes
// of temporaries - https://stackoverflow.com/a/48732525/1063961 // of temporaries - https://stackoverflow.com/a/48732525/1063961
match $val { match $val {
@ -296,8 +295,6 @@ macro_rules! dbg {
} }
} }
}; };
// Trailing comma with single argument is ignored
($val:expr,) => { $crate::dbg!($val) };
($($val:expr),+ $(,)?) => { ($($val:expr),+ $(,)?) => {
($($crate::dbg!($val)),+,) ($($crate::dbg!($val)),+,)
}; };

View file

@ -45,7 +45,7 @@ LL | fn main() {}
| |
::: $SRC_DIR/core/src/macros/mod.rs:LL:COL ::: $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 | ---------- while parsing argument for this `expr` macro fragment
error: aborting due to 4 previous errors error: aborting due to 4 previous errors