Skip to content

Use ? in core/std macros #78375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 16 additions & 43 deletions library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
@@ -6,15 +6,12 @@ macro_rules! panic {
() => (
$crate::panic!("explicit panic")
);
($msg:literal) => (
($msg:literal $(,)?) => (
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no ($msg:literal,) => ($panic!($msg)) in the previous code, but this seems previously covered by ($msg:expr,) => ($panic!($msg)).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's what confused me then.

$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,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.
@@ -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 @@ 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.
@@ -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.
@@ -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.
@@ -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.
@@ -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.
@@ -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.
@@ -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.
@@ -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 */ }};
}

7 changes: 2 additions & 5 deletions library/std/src/macros.rs
Original file line number Diff line number Diff line change
@@ -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)),+,)
};
2 changes: 1 addition & 1 deletion src/test/ui/parser/issue-62894.stderr
Original file line number Diff line number Diff line change
@@ -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