mirror of
https://github.com/rust-lang/rust
synced 2024-10-02 23:04:50 +00:00
Add pattern types to parser
This commit is contained in:
parent
fc27a91880
commit
c340e67dec
|
@ -332,6 +332,9 @@ fn visit_ty(&mut self, ty: &'a ast::Ty) {
|
|||
ast::TyKind::Never => {
|
||||
gate!(&self, never_type, ty.span, "the `!` type is experimental");
|
||||
}
|
||||
ast::TyKind::Pat(..) => {
|
||||
gate!(&self, pattern_types, ty.span, "pattern types are unstable");
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
visit::walk_ty(self, ty)
|
||||
|
|
|
@ -46,6 +46,7 @@
|
|||
mod format_foreign;
|
||||
mod global_allocator;
|
||||
mod log_syntax;
|
||||
mod pattern_type;
|
||||
mod source_util;
|
||||
mod test;
|
||||
mod trace_macros;
|
||||
|
@ -95,6 +96,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
|
|||
log_syntax: log_syntax::expand_log_syntax,
|
||||
module_path: source_util::expand_mod,
|
||||
option_env: env::expand_option_env,
|
||||
pattern_type: pattern_type::expand,
|
||||
std_panic: edition_panic::expand_panic,
|
||||
stringify: source_util::expand_stringify,
|
||||
trace_macros: trace_macros::expand_trace_macros,
|
||||
|
|
29
compiler/rustc_builtin_macros/src/pattern_type.rs
Normal file
29
compiler/rustc_builtin_macros/src/pattern_type.rs
Normal file
|
@ -0,0 +1,29 @@
|
|||
use rustc_ast::{ast, ptr::P, tokenstream::TokenStream, Pat, Ty};
|
||||
use rustc_errors::PResult;
|
||||
use rustc_expand::base::{self, DummyResult, ExpandResult, ExtCtxt, MacroExpanderResult};
|
||||
use rustc_span::{sym, Span};
|
||||
|
||||
pub fn expand<'cx>(
|
||||
cx: &'cx mut ExtCtxt<'_>,
|
||||
sp: Span,
|
||||
tts: TokenStream,
|
||||
) -> MacroExpanderResult<'cx> {
|
||||
let (ty, pat) = match parse_pat_ty(cx, tts) {
|
||||
Ok(parsed) => parsed,
|
||||
Err(err) => {
|
||||
return ExpandResult::Ready(DummyResult::any(sp, err.emit()));
|
||||
}
|
||||
};
|
||||
|
||||
ExpandResult::Ready(base::MacEager::ty(cx.ty(sp, ast::TyKind::Pat(ty, pat))))
|
||||
}
|
||||
|
||||
fn parse_pat_ty<'a>(cx: &mut ExtCtxt<'a>, stream: TokenStream) -> PResult<'a, (P<Ty>, P<Pat>)> {
|
||||
let mut parser = cx.new_parser_from_tts(stream);
|
||||
|
||||
let ty = parser.parse_ty()?;
|
||||
parser.eat_keyword(sym::is);
|
||||
let pat = parser.parse_pat_no_top_alt(None, None)?;
|
||||
|
||||
Ok((ty, pat))
|
||||
}
|
|
@ -215,6 +215,8 @@ pub fn internal(&self, feature: Symbol) -> bool {
|
|||
(internal, omit_gdb_pretty_printer_section, "1.5.0", None),
|
||||
/// Set the maximum pattern complexity allowed (not limited by default).
|
||||
(internal, pattern_complexity, "1.78.0", None),
|
||||
/// Allows using pattern types.
|
||||
(internal, pattern_types, "CURRENT_RUSTC_VERSION", Some(54882)),
|
||||
/// Allows using `#[prelude_import]` on glob `use` items.
|
||||
(internal, prelude_import, "1.2.0", None),
|
||||
/// Used to identify crates that contain the profiler runtime.
|
||||
|
|
|
@ -1009,6 +1009,7 @@
|
|||
io_stderr,
|
||||
io_stdout,
|
||||
irrefutable_let_patterns,
|
||||
is,
|
||||
is_val_statically_known,
|
||||
isa_attribute,
|
||||
isize,
|
||||
|
@ -1347,6 +1348,8 @@
|
|||
path,
|
||||
pattern_complexity,
|
||||
pattern_parentheses,
|
||||
pattern_type,
|
||||
pattern_types,
|
||||
phantom_data,
|
||||
pic,
|
||||
pie,
|
||||
|
|
|
@ -396,6 +396,9 @@ pub mod assert_matches {
|
|||
pub mod option;
|
||||
pub mod panic;
|
||||
pub mod panicking;
|
||||
#[cfg(not(bootstrap))]
|
||||
#[unstable(feature = "core_pattern_types", issue = "none")]
|
||||
pub mod pat;
|
||||
pub mod pin;
|
||||
pub mod result;
|
||||
pub mod sync;
|
||||
|
|
14
library/core/src/pat.rs
Normal file
14
library/core/src/pat.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
//! Helper module for exporting the `pattern_type` macro
|
||||
|
||||
/// Creates a pattern type.
|
||||
/// ```ignore (cannot test this from within core yet)
|
||||
/// type Positive = std::pat::pattern_type!(i32 is 1..);
|
||||
/// ```
|
||||
#[macro_export]
|
||||
#[rustc_builtin_macro(pattern_type)]
|
||||
#[unstable(feature = "core_pattern_type", issue = "none")]
|
||||
macro_rules! pattern_type {
|
||||
($($arg:tt)*) => {
|
||||
/* compiler built-in */
|
||||
};
|
||||
}
|
|
@ -576,6 +576,9 @@
|
|||
pub mod num;
|
||||
pub mod os;
|
||||
pub mod panic;
|
||||
#[cfg(not(bootstrap))]
|
||||
#[unstable(feature = "core_pattern_types", issue = "none")]
|
||||
pub mod pat;
|
||||
pub mod path;
|
||||
pub mod process;
|
||||
pub mod sync;
|
||||
|
|
3
library/std/src/pat.rs
Normal file
3
library/std/src/pat.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
//! Helper module for exporting the `pattern_type` macro
|
||||
|
||||
pub use core::pattern_type;
|
12
tests/ui/type/pattern_types/bad_pat.rs
Normal file
12
tests/ui/type/pattern_types/bad_pat.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
//@ compile-flags: -Zno-analysis
|
||||
|
||||
#![feature(pattern_types)]
|
||||
#![feature(core_pattern_types)]
|
||||
#![feature(core_pattern_type)]
|
||||
|
||||
use std::pat::pattern_type;
|
||||
|
||||
type NonNullU32_2 = pattern_type!(u32 is 1..=);
|
||||
//~^ ERROR: inclusive range with no end
|
||||
type Positive2 = pattern_type!(i32 is 0..=);
|
||||
//~^ ERROR: inclusive range with no end
|
19
tests/ui/type/pattern_types/bad_pat.stderr
Normal file
19
tests/ui/type/pattern_types/bad_pat.stderr
Normal file
|
@ -0,0 +1,19 @@
|
|||
error[E0586]: inclusive range with no end
|
||||
--> $DIR/bad_pat.rs:9:43
|
||||
|
|
||||
LL | type NonNullU32_2 = pattern_type!(u32 is 1..=);
|
||||
| ^^^ help: use `..` instead
|
||||
|
|
||||
= note: inclusive ranges must be bounded at the end (`..=b` or `a..=b`)
|
||||
|
||||
error[E0586]: inclusive range with no end
|
||||
--> $DIR/bad_pat.rs:11:40
|
||||
|
|
||||
LL | type Positive2 = pattern_type!(i32 is 0..=);
|
||||
| ^^^ help: use `..` instead
|
||||
|
|
||||
= note: inclusive ranges must be bounded at the end (`..=b` or `a..=b`)
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0586`.
|
14
tests/ui/type/pattern_types/feature-gate-pattern_types.rs
Normal file
14
tests/ui/type/pattern_types/feature-gate-pattern_types.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
//@ compile-flags: -Zno-analysis
|
||||
|
||||
use std::pat::pattern_type;
|
||||
|
||||
type NonNullU32 = pattern_type!(u32 is 1..);
|
||||
//~^ use of unstable library feature 'core_pattern_type'
|
||||
type Percent = pattern_type!(u32 is 0..=100);
|
||||
//~^ use of unstable library feature 'core_pattern_type'
|
||||
type Negative = pattern_type!(i32 is ..=0);
|
||||
//~^ use of unstable library feature 'core_pattern_type'
|
||||
type Positive = pattern_type!(i32 is 0..);
|
||||
//~^ use of unstable library feature 'core_pattern_type'
|
||||
type Always = pattern_type!(Option<u32> is Some(_));
|
||||
//~^ use of unstable library feature 'core_pattern_type'
|
|
@ -0,0 +1,48 @@
|
|||
error[E0658]: use of unstable library feature 'core_pattern_type'
|
||||
--> $DIR/feature-gate-pattern_types.rs:5:19
|
||||
|
|
||||
LL | type NonNullU32 = pattern_type!(u32 is 1..);
|
||||
| ^^^^^^^^^^^^
|
||||
|
|
||||
= help: add `#![feature(core_pattern_type)]` to the crate attributes to enable
|
||||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
||||
|
||||
error[E0658]: use of unstable library feature 'core_pattern_type'
|
||||
--> $DIR/feature-gate-pattern_types.rs:7:16
|
||||
|
|
||||
LL | type Percent = pattern_type!(u32 is 0..=100);
|
||||
| ^^^^^^^^^^^^
|
||||
|
|
||||
= help: add `#![feature(core_pattern_type)]` to the crate attributes to enable
|
||||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
||||
|
||||
error[E0658]: use of unstable library feature 'core_pattern_type'
|
||||
--> $DIR/feature-gate-pattern_types.rs:9:17
|
||||
|
|
||||
LL | type Negative = pattern_type!(i32 is ..=0);
|
||||
| ^^^^^^^^^^^^
|
||||
|
|
||||
= help: add `#![feature(core_pattern_type)]` to the crate attributes to enable
|
||||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
||||
|
||||
error[E0658]: use of unstable library feature 'core_pattern_type'
|
||||
--> $DIR/feature-gate-pattern_types.rs:11:17
|
||||
|
|
||||
LL | type Positive = pattern_type!(i32 is 0..);
|
||||
| ^^^^^^^^^^^^
|
||||
|
|
||||
= help: add `#![feature(core_pattern_type)]` to the crate attributes to enable
|
||||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
||||
|
||||
error[E0658]: use of unstable library feature 'core_pattern_type'
|
||||
--> $DIR/feature-gate-pattern_types.rs:13:15
|
||||
|
|
||||
LL | type Always = pattern_type!(Option<u32> is Some(_));
|
||||
| ^^^^^^^^^^^^
|
||||
|
|
||||
= help: add `#![feature(core_pattern_type)]` to the crate attributes to enable
|
||||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
||||
|
||||
error: aborting due to 5 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0658`.
|
12
tests/ui/type/pattern_types/feature-gate-pattern_types2.rs
Normal file
12
tests/ui/type/pattern_types/feature-gate-pattern_types2.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
//@ compile-flags: -Zno-analysis
|
||||
//@ check-pass
|
||||
|
||||
#![feature(core_pattern_type)]
|
||||
|
||||
use std::pat::pattern_type;
|
||||
|
||||
type NonNullU32 = pattern_type!(u32 is 1..);
|
||||
type Percent = pattern_type!(u32 is 0..=100);
|
||||
type Negative = pattern_type!(i32 is ..=0);
|
||||
type Positive = pattern_type!(i32 is 0..);
|
||||
type Always = pattern_type!(Option<u32> is Some(_));
|
Loading…
Reference in a new issue