Move the `matches!` macro to the prelude
This commit is contained in:
parent
f69293ae80
commit
7472cd46aa
|
@ -585,7 +585,6 @@ checksum = "8ff012e225ce166d4422e0e78419d901719760f62ae2b7969ca6b564d1b54a9e"
|
||||||
name = "core"
|
name = "core"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"matches_macro",
|
|
||||||
"rand 0.7.0",
|
"rand 0.7.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -1901,10 +1900,6 @@ version = "0.1.8"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
|
checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "matches_macro"
|
|
||||||
version = "0.0.0"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mdbook"
|
name = "mdbook"
|
||||||
version = "0.3.1"
|
version = "0.3.1"
|
||||||
|
|
|
@ -20,9 +20,6 @@ path = "../libcore/tests/lib.rs"
|
||||||
name = "corebenches"
|
name = "corebenches"
|
||||||
path = "../libcore/benches/lib.rs"
|
path = "../libcore/benches/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
matches_macro = { path = "../libmatches_macro" }
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
rand = "0.7"
|
rand = "0.7"
|
||||||
|
|
||||||
|
|
|
@ -85,7 +85,6 @@
|
||||||
#![feature(iter_once_with)]
|
#![feature(iter_once_with)]
|
||||||
#![feature(lang_items)]
|
#![feature(lang_items)]
|
||||||
#![feature(link_llvm_intrinsics)]
|
#![feature(link_llvm_intrinsics)]
|
||||||
#![feature(matches_macro)]
|
|
||||||
#![feature(never_type)]
|
#![feature(never_type)]
|
||||||
#![feature(nll)]
|
#![feature(nll)]
|
||||||
#![feature(exhaustive_patterns)]
|
#![feature(exhaustive_patterns)]
|
||||||
|
@ -135,16 +134,7 @@
|
||||||
use prelude::v1::*;
|
use prelude::v1::*;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
#[path = "macros.rs"]
|
mod macros;
|
||||||
mod prelude_macros;
|
|
||||||
|
|
||||||
/// Macros that are not in the prelude and need to be imported explicitly
|
|
||||||
#[unstable(feature = "matches_macro", issue = "0")]
|
|
||||||
pub mod macros {
|
|
||||||
#[unstable(feature = "matches_macro", issue = "0")]
|
|
||||||
#[doc(inline)]
|
|
||||||
pub use matches_macro::matches;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod internal_macros;
|
mod internal_macros;
|
||||||
|
|
|
@ -238,6 +238,30 @@ 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)*); })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns whether the given expression matches (any of) the given pattern(s).
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// #![feature(matches_macro)]
|
||||||
|
///
|
||||||
|
/// let foo = 'f';
|
||||||
|
/// assert!(matches!(foo, 'A'..='Z' | 'a'..='z'));
|
||||||
|
///
|
||||||
|
/// let bar = Some(4);
|
||||||
|
/// assert!(matches!(bar, Some(x) if x > 2));
|
||||||
|
/// ```
|
||||||
|
#[macro_export]
|
||||||
|
#[unstable(feature = "matches_macro", issue = "0")]
|
||||||
|
macro_rules! matches {
|
||||||
|
($expression:expr, $( $pattern:pat )|+ $( if $guard: expr )?) => {
|
||||||
|
match $expression {
|
||||||
|
$( $pattern )|+ $( if $guard )? => true,
|
||||||
|
_ => false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Unwraps a result or propagates its error.
|
/// Unwraps a result or propagates its error.
|
||||||
///
|
///
|
||||||
/// The `?` operator was added to replace `try!` and should be used instead.
|
/// The `?` operator was added to replace `try!` and should be used instead.
|
||||||
|
|
|
@ -82,7 +82,7 @@ pub use crate::{
|
||||||
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
|
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use crate::prelude_macros::builtin::{
|
pub use crate::macros::builtin::{
|
||||||
RustcDecodable,
|
RustcDecodable,
|
||||||
RustcEncodable,
|
RustcEncodable,
|
||||||
bench,
|
bench,
|
||||||
|
|
|
@ -1,10 +0,0 @@
|
||||||
[package]
|
|
||||||
authors = ["The Rust Project Developers"]
|
|
||||||
name = "matches_macro"
|
|
||||||
version = "0.0.0"
|
|
||||||
autotests = false
|
|
||||||
autobenches = false
|
|
||||||
edition = "2018"
|
|
||||||
|
|
||||||
[lib]
|
|
||||||
path = "lib.rs"
|
|
|
@ -1,29 +0,0 @@
|
||||||
#![no_core]
|
|
||||||
#![feature(no_core)]
|
|
||||||
#![feature(staged_api)]
|
|
||||||
#![doc(test(no_crate_inject))]
|
|
||||||
|
|
||||||
/// Returns whether the given expression matches (any of) the given pattern(s).
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// #![feature(matches_macro)]
|
|
||||||
/// use std::macros::matches;
|
|
||||||
///
|
|
||||||
/// let foo = 'f';
|
|
||||||
/// assert!(matches!(foo, 'A'..='Z' | 'a'..='z'));
|
|
||||||
///
|
|
||||||
/// let bar = Some(4);
|
|
||||||
/// assert!(matches!(bar, Some(x) if x > 2));
|
|
||||||
/// ```
|
|
||||||
#[macro_export]
|
|
||||||
#[unstable(feature = "matches_macro", issue = "0")]
|
|
||||||
macro_rules! matches {
|
|
||||||
($expression:expr, $( $pattern:pat )|+ $( if $guard: expr )?) => {
|
|
||||||
match $expression {
|
|
||||||
$( $pattern )|+ $( if $guard )? => true,
|
|
||||||
_ => false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -354,16 +354,7 @@ extern crate cfg_if;
|
||||||
|
|
||||||
// The standard macros that are not built-in to the compiler.
|
// The standard macros that are not built-in to the compiler.
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
#[path = "macros.rs"]
|
mod macros;
|
||||||
mod prelude_macros;
|
|
||||||
|
|
||||||
/// Macros that are not in the prelude and need to be imported explicitly
|
|
||||||
#[unstable(feature = "matches_macro", issue = "0")]
|
|
||||||
pub mod macros {
|
|
||||||
#[unstable(feature = "matches_macro", issue = "0")]
|
|
||||||
#[doc(inline)]
|
|
||||||
pub use core::macros::matches;
|
|
||||||
}
|
|
||||||
|
|
||||||
// The Rust prelude
|
// The Rust prelude
|
||||||
pub mod prelude;
|
pub mod prelude;
|
||||||
|
@ -537,6 +528,7 @@ pub use core::{
|
||||||
writeln,
|
writeln,
|
||||||
// Unstable
|
// Unstable
|
||||||
todo,
|
todo,
|
||||||
|
matches,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Re-export built-in macros defined through libcore.
|
// Re-export built-in macros defined through libcore.
|
||||||
|
|
|
@ -5,7 +5,7 @@ LL | macro_rules! unknown { () => () }
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: cannot find a built-in macro with name `line`
|
error: cannot find a built-in macro with name `line`
|
||||||
--> <::core::prelude_macros::builtin::line macros>:1:1
|
--> <::core::macros::builtin::line macros>:1:1
|
||||||
|
|
|
|
||||||
LL | () => { }
|
LL | () => { }
|
||||||
| ^^^^^^^^^
|
| ^^^^^^^^^
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
// run-pass
|
|
||||||
|
|
||||||
#![feature(matches_macro)]
|
|
||||||
|
|
||||||
use std::macros::matches;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let foo = 'f';
|
|
||||||
assert!(matches!(foo, 'A'..='Z' | 'a'..='z'));
|
|
||||||
|
|
||||||
let foo = '_';
|
|
||||||
assert!(!matches!(foo, 'A'..='Z' | 'a'..='z'));
|
|
||||||
}
|
|
|
@ -1,7 +0,0 @@
|
||||||
#![feature(matches_macro)]
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let foo = 'f';
|
|
||||||
assert!(matches!(foo, 'A'..='Z' | 'a'..='z'));
|
|
||||||
//~^ Error: cannot find macro `matches` in this scope
|
|
||||||
}
|
|
|
@ -1,8 +0,0 @@
|
||||||
error: cannot find macro `matches` in this scope
|
|
||||||
--> $DIR/matches_macro_not_in_the_prelude.rs:5:13
|
|
||||||
|
|
|
||||||
LL | assert!(matches!(foo, 'A'..='Z' | 'a'..='z'));
|
|
||||||
| ^^^^^^^
|
|
||||||
|
|
||||||
error: aborting due to previous error
|
|
||||||
|
|
Loading…
Reference in New Issue