Skip to content
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

Format core and std macro rules, removing needless surrounding blocks #94868

Merged
merged 2 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
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
76 changes: 50 additions & 26 deletions library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ macro_rules! panic {
#[cfg_attr(not(test), rustc_diagnostic_item = "assert_eq_macro")]
#[allow_internal_unstable(core_panic)]
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 @@ -46,8 +46,8 @@ macro_rules! assert_eq {
}
}
}
});
($left:expr, $right:expr, $($arg:tt)+) => ({
};
($left:expr, $right:expr, $($arg:tt)+) => {
match (&$left, &$right) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
Expand All @@ -59,7 +59,7 @@ macro_rules! assert_eq {
}
}
}
});
};
}

/// Asserts that two expressions are not equal to each other (using [`PartialEq`]).
Expand All @@ -84,7 +84,7 @@ macro_rules! assert_eq {
#[cfg_attr(not(test), rustc_diagnostic_item = "assert_ne_macro")]
#[allow_internal_unstable(core_panic)]
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 @@ -96,8 +96,8 @@ macro_rules! assert_ne {
}
}
}
});
($left:expr, $right:expr, $($arg:tt)+) => ({
};
($left:expr, $right:expr, $($arg:tt)+) => {
match (&($left), &($right)) {
(left_val, right_val) => {
if *left_val == *right_val {
Expand All @@ -109,7 +109,7 @@ macro_rules! assert_ne {
}
}
}
});
};
}

/// Asserts that an expression matches any of the given patterns.
Expand Down Expand Up @@ -142,7 +142,7 @@ macro_rules! assert_ne {
#[allow_internal_unstable(core_panic)]
#[rustc_macro_transparency = "semitransparent"]
pub macro assert_matches {
($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => ({
($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => {
match $left {
$( $pattern )|+ $( if $guard )? => {}
ref left_val => {
Expand All @@ -153,8 +153,8 @@ pub macro assert_matches {
);
}
}
}),
($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )?, $($arg:tt)+) => ({
},
($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )?, $($arg:tt)+) => {
match $left {
$( $pattern )|+ $( if $guard )? => {}
ref left_val => {
Expand All @@ -165,7 +165,7 @@ pub macro assert_matches {
);
}
}
}),
},
}

/// Asserts that a boolean expression is `true` at runtime.
Expand Down Expand Up @@ -214,7 +214,11 @@ pub macro assert_matches {
#[rustc_diagnostic_item = "debug_assert_macro"]
#[allow_internal_unstable(edition_panic)]
macro_rules! debug_assert {
($($arg:tt)*) => (if $crate::cfg!(debug_assertions) { $crate::assert!($($arg)*); })
($($arg:tt)*) => {
if $crate::cfg!(debug_assertions) {
$crate::assert!($($arg)*);
}
};
}

/// Asserts that two expressions are equal to each other.
Expand All @@ -240,7 +244,11 @@ macro_rules! debug_assert {
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "debug_assert_eq_macro")]
macro_rules! debug_assert_eq {
($($arg:tt)*) => (if $crate::cfg!(debug_assertions) { $crate::assert_eq!($($arg)*); })
($($arg:tt)*) => {
if $crate::cfg!(debug_assertions) {
$crate::assert_eq!($($arg)*);
}
};
}

/// Asserts that two expressions are not equal to each other.
Expand All @@ -266,7 +274,11 @@ macro_rules! debug_assert_eq {
#[stable(feature = "assert_ne", since = "1.13.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "debug_assert_ne_macro")]
macro_rules! debug_assert_ne {
($($arg:tt)*) => (if $crate::cfg!(debug_assertions) { $crate::assert_ne!($($arg)*); })
($($arg:tt)*) => {
if $crate::cfg!(debug_assertions) {
$crate::assert_ne!($($arg)*);
}
};
}

/// Asserts that an expression matches any of the given patterns.
Expand Down Expand Up @@ -305,7 +317,9 @@ macro_rules! debug_assert_ne {
#[allow_internal_unstable(assert_matches)]
#[rustc_macro_transparency = "semitransparent"]
pub macro debug_assert_matches($($arg:tt)*) {
if $crate::cfg!(debug_assertions) { $crate::assert_matches::assert_matches!($($arg)*); }
if $crate::cfg!(debug_assertions) {
$crate::assert_matches::assert_matches!($($arg)*);
}
}

/// Returns whether the given expression matches any of the given patterns.
Expand All @@ -331,7 +345,7 @@ macro_rules! matches {
$( $pattern )|+ $( if $guard )? => true,
_ => false
}
}
};
}

/// Unwraps a result or propagates its error.
Expand Down Expand Up @@ -482,7 +496,9 @@ macro_rules! r#try {
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "write_macro")]
macro_rules! write {
($dst:expr, $($arg:tt)*) => ($dst.write_fmt($crate::format_args!($($arg)*)))
($dst:expr, $($arg:tt)*) => {
$dst.write_fmt($crate::format_args!($($arg)*))
};
}

/// Write formatted data into a buffer, with a newline appended.
Expand Down Expand Up @@ -534,12 +550,12 @@ macro_rules! write {
#[cfg_attr(not(test), rustc_diagnostic_item = "writeln_macro")]
#[allow_internal_unstable(format_args_nl)]
macro_rules! writeln {
($dst:expr $(,)?) => (
($dst:expr $(,)?) => {
$crate::write!($dst, "\n")
);
($dst:expr, $($arg:tt)*) => (
};
($dst:expr, $($arg:tt)*) => {
$dst.write_fmt($crate::format_args_nl!($($arg)*))
);
};
}

/// Indicates unreachable code.
Expand Down Expand Up @@ -683,8 +699,12 @@ macro_rules! unreachable {
#[cfg_attr(not(test), rustc_diagnostic_item = "unimplemented_macro")]
#[allow_internal_unstable(core_panic)]
macro_rules! unimplemented {
() => ($crate::panicking::panic("not implemented"));
($($arg:tt)+) => ($crate::panic!("not implemented: {}", $crate::format_args!($($arg)+)));
() => {
$crate::panicking::panic("not implemented")
};
($($arg:tt)+) => {
$crate::panic!("not implemented: {}", $crate::format_args!($($arg)+))
};
}

/// Indicates unfinished code.
Expand Down Expand Up @@ -746,8 +766,12 @@ macro_rules! unimplemented {
#[cfg_attr(not(test), rustc_diagnostic_item = "todo_macro")]
#[allow_internal_unstable(core_panic)]
macro_rules! todo {
() => ($crate::panicking::panic("not yet implemented"));
($($arg:tt)+) => ($crate::panic!("not yet implemented: {}", $crate::format_args!($($arg)+)));
() => {
$crate::panicking::panic("not yet implemented")
};
($($arg:tt)+) => {
$crate::panic!("not yet implemented: {}", $crate::format_args!($($arg)+))
};
}

/// Definitions of built-in macros.
Expand Down
28 changes: 18 additions & 10 deletions library/std/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ macro_rules! panic {
#[cfg_attr(not(test), rustc_diagnostic_item = "print_macro")]
#[allow_internal_unstable(print_internals)]
macro_rules! print {
($($arg:tt)*) => ($crate::io::_print($crate::format_args!($($arg)*)));
($($arg:tt)*) => {
$crate::io::_print($crate::format_args!($($arg)*))
};
}

/// Prints to the standard output, with a newline.
Expand Down Expand Up @@ -94,10 +96,12 @@ macro_rules! print {
#[cfg_attr(not(test), rustc_diagnostic_item = "println_macro")]
#[allow_internal_unstable(print_internals, format_args_nl)]
macro_rules! println {
() => ($crate::print!("\n"));
($($arg:tt)*) => ({
$crate::io::_print($crate::format_args_nl!($($arg)*));
})
() => {
$crate::print!("\n")
};
($($arg:tt)*) => {
$crate::io::_print($crate::format_args_nl!($($arg)*))
};
}

/// Prints to the standard error.
Expand Down Expand Up @@ -126,7 +130,9 @@ macro_rules! println {
#[cfg_attr(not(test), rustc_diagnostic_item = "eprint_macro")]
#[allow_internal_unstable(print_internals)]
macro_rules! eprint {
($($arg:tt)*) => ($crate::io::_eprint($crate::format_args!($($arg)*)));
($($arg:tt)*) => {
$crate::io::_eprint($crate::format_args!($($arg)*))
};
}

/// Prints to the standard error, with a newline.
Expand Down Expand Up @@ -155,10 +161,12 @@ macro_rules! eprint {
#[cfg_attr(not(test), rustc_diagnostic_item = "eprintln_macro")]
#[allow_internal_unstable(print_internals, format_args_nl)]
macro_rules! eprintln {
() => ($crate::eprint!("\n"));
($($arg:tt)*) => ({
$crate::io::_eprint($crate::format_args_nl!($($arg)*));
})
() => {
$crate::eprint!("\n")
};
($($arg:tt)*) => {
$crate::io::_eprint($crate::format_args_nl!($($arg)*))
};
}

/// Prints and returns the value of a given expression for quick and dirty
Expand Down
2 changes: 1 addition & 1 deletion src/test/pretty/dollar-crate.pp
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
// pp-exact:dollar-crate.pp

fn main() {
{ ::std::io::_print(::core::fmt::Arguments::new_v1(&["rust\n"], &[])); };
::std::io::_print(::core::fmt::Arguments::new_v1(&["rust\n"], &[]));
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@
116| 1|
117| 1| let
118| 1| _unused_closure
119| | =
120| | |
119| 1| =
120| 1| |
121| | mut countdown
122| | |
123| 0| {
Expand Down Expand Up @@ -173,7 +173,7 @@
169| | ;
170| |
171| 1| let short_used_not_covered_closure_line_break_no_block_embedded_branch =
172| | | _unused_arg: u8 |
172| 1| | _unused_arg: u8 |
173| 0| println!(
174| 0| "not called: {}",
175| 0| if is_true { "check" } else { "me" }
Expand All @@ -191,7 +191,7 @@
187| | ;
188| |
189| 1| let short_used_covered_closure_line_break_no_block_embedded_branch =
190| 1| | _unused_arg: u8 |
190| | | _unused_arg: u8 |
191| 1| println!(
192| 1| "not called: {}",
193| 1| if is_true { "check" } else { "me" }
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/macros/trace-macro.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ LL | println!("Hello, World!");
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: expanding `println! { "Hello, World!" }`
= note: to `{ $crate :: io :: _print($crate :: format_args_nl! ("Hello, World!")) ; }`
= note: to `$crate :: io :: _print($crate :: format_args_nl! ("Hello, World!"))`

2 changes: 1 addition & 1 deletion src/test/ui/parser/issues/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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LL | struct Foo(isize, isize);
= note: the matched value is of type `Foo`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
|
LL ~ Foo(2, b) => println!("{}", b)
LL ~ Foo(2, b) => println!("{}", b),
LL + Foo(_, _) => todo!()
Comment on lines -15 to 16
Copy link
Member Author

Choose a reason for hiding this comment

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

This now avoids #94866

|

Expand Down