Skip to content

Commit

Permalink
Rollup merge of #86947 - m-ou-se:assert-matches-to-submodule, r=yaahc
Browse files Browse the repository at this point in the history
Move assert_matches to an inner module

Fixes #82913
  • Loading branch information
JohnTitor authored Jul 15, 2021
2 parents 9813013 + e304443 commit a5acb7b
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 15 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::ich::StableHashingContext;
use rustc_ast as ast;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_span::{BytePos, NormalizedPos, SourceFile};
use std::assert::assert_matches;

use smallvec::SmallVec;

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_mir/src/interpret/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! integer. It is crucial that these operations call `check_align` *before*
//! short-circuiting the empty case!

use std::assert::assert_matches;
use std::borrow::Cow;
use std::collections::VecDeque;
use std::convert::{TryFrom, TryInto};
Expand Down
10 changes: 10 additions & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ use prelude::v1::*;
#[macro_use]
mod macros;

// We don't export this through #[macro_export] for now, to avoid breakage.
// See https://github.com/rust-lang/rust/issues/82913
#[cfg(not(test))]
#[unstable(feature = "assert_matches", issue = "82775")]
/// Unstable module containing the unstable `assert_matches` macro.
pub mod assert {
#[unstable(feature = "assert_matches", issue = "82775")]
pub use crate::macros::{assert_matches, debug_assert_matches};
}

#[macro_use]
mod internal_macros;

Expand Down
17 changes: 11 additions & 6 deletions library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ macro_rules! assert_ne {
/// ```
/// #![feature(assert_matches)]
///
/// use std::assert::assert_matches;
///
/// let a = 1u32.checked_add(2);
/// let b = 1u32.checked_sub(2);
/// assert_matches!(a, Some(_));
Expand All @@ -135,10 +137,10 @@ macro_rules! assert_ne {
/// let c = Ok("abc".to_string());
/// assert_matches!(c, Ok(x) | Err(x) if x.len() < 100);
/// ```
#[macro_export]
#[unstable(feature = "assert_matches", issue = "82775")]
#[allow_internal_unstable(core_panic)]
macro_rules! assert_matches {
#[rustc_macro_transparency = "semitransparent"]
pub macro assert_matches {
($left:expr, $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => ({
match $left {
$( $pattern )|+ $( if $guard )? => {}
Expand All @@ -150,7 +152,7 @@ macro_rules! assert_matches {
);
}
}
});
}),
($left:expr, $( $pattern:pat_param )|+ $( if $guard: expr )?, $($arg:tt)+) => ({
match $left {
$( $pattern )|+ $( if $guard )? => {}
Expand All @@ -162,7 +164,7 @@ macro_rules! assert_matches {
);
}
}
});
}),
}

/// Asserts that a boolean expression is `true` at runtime.
Expand Down Expand Up @@ -284,6 +286,8 @@ macro_rules! debug_assert_ne {
/// ```
/// #![feature(assert_matches)]
///
/// use std::assert::debug_assert_matches;
///
/// let a = 1u32.checked_add(2);
/// let b = 1u32.checked_sub(2);
/// debug_assert_matches!(a, Some(_));
Expand All @@ -295,8 +299,9 @@ macro_rules! debug_assert_ne {
#[macro_export]
#[unstable(feature = "assert_matches", issue = "82775")]
#[allow_internal_unstable(assert_matches)]
macro_rules! debug_assert_matches {
($($arg:tt)*) => (if $crate::cfg!(debug_assertions) { $crate::assert_matches!($($arg)*); })
#[rustc_macro_transparency = "semitransparent"]
pub macro debug_assert_matches($($arg:tt)*) {
if $crate::cfg!(debug_assertions) { $crate::assert::assert_matches!($($arg)*); }
}

/// Returns whether the given expression matches any of the given patterns.
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,8 @@ pub use std_detect::{
#[stable(feature = "rust1", since = "1.0.0")]
#[allow(deprecated, deprecated_in_future)]
pub use core::{
assert_eq, assert_matches, assert_ne, debug_assert, debug_assert_eq, debug_assert_matches,
debug_assert_ne, matches, r#try, todo, unimplemented, unreachable, write, writeln,
assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, matches, r#try, todo,
unimplemented, unreachable, write, writeln,
};

// Re-export built-in macros defined through libcore.
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/macros/assert-matches-macro-msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#![feature(assert_matches)]

use std::assert::assert_matches;

fn main() {
assert_matches!(1 + 1, 3, "1 + 1 definitely should be 3");
}
2 changes: 2 additions & 0 deletions src/test/ui/matches2021.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#![feature(assert_matches)]

use std::assert::assert_matches;

fn main() {
assert!(matches!((), ()));
assert_matches!((), ());
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/resolve/resolve-hint-macro.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn main() {
assert(true);
//~^ ERROR expected function, found macro `assert`
assert_eq(1, 1);
//~^ ERROR expected function, found macro `assert_eq`
}
10 changes: 5 additions & 5 deletions src/test/ui/resolve/resolve-hint-macro.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error[E0423]: expected function, found macro `assert`
error[E0423]: expected function, found macro `assert_eq`
--> $DIR/resolve-hint-macro.rs:2:5
|
LL | assert(true);
| ^^^^^^ not a function
LL | assert_eq(1, 1);
| ^^^^^^^^^ not a function
|
help: use `!` to invoke the macro
|
LL | assert!(true);
| ^
LL | assert_eq!(1, 1);
| ^

error: aborting due to previous error

Expand Down

0 comments on commit a5acb7b

Please sign in to comment.