Skip to content

Commit

Permalink
Use ? in core/std macros
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Oct 25, 2020
1 parent 430feb2 commit 04c0018
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 49 deletions.
59 changes: 16 additions & 43 deletions library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)+))
);
Expand All @@ -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) {
Expand All @@ -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) => {
Expand Down Expand Up @@ -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 {
Expand All @@ -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) => {
Expand Down Expand Up @@ -315,17 +306,14 @@ 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) => {
return $crate::result::Result::Err($crate::convert::From::from(err));
}
}
};
($expr:expr,) => {
$crate::r#try!($expr)
};
}

/// Writes formatted data into a buffer.
Expand Down Expand Up @@ -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)*))
);
Expand Down Expand Up @@ -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)*)
});
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -816,8 +797,7 @@ pub(crate) mod builtin {
#[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.
Expand All @@ -841,8 +821,7 @@ pub(crate) mod builtin {
#[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.
Expand Down Expand Up @@ -877,8 +856,7 @@ pub(crate) mod builtin {
#[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.
Expand All @@ -900,8 +878,7 @@ pub(crate) mod builtin {
#[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.
Expand Down Expand Up @@ -1043,8 +1020,7 @@ pub(crate) mod builtin {
#[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.
Expand Down Expand Up @@ -1083,8 +1059,7 @@ pub(crate) mod builtin {
#[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.
Expand Down Expand Up @@ -1191,8 +1166,7 @@ pub(crate) mod builtin {
#[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.
Expand Down Expand Up @@ -1242,8 +1216,7 @@ pub(crate) mod builtin {
#[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 */ }};
}

Expand Down
7 changes: 2 additions & 5 deletions library/std/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)+))
});
Expand Down Expand Up @@ -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 {
Expand All @@ -296,8 +295,6 @@ macro_rules! dbg {
}
}
};
// Trailing comma with single argument is ignored
($val:expr,) => { $crate::dbg!($val) };
($($val:expr),+ $(,)?) => {
($($crate::dbg!($val)),+,)
};
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/parser/issue-62894.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 04c0018

Please sign in to comment.