diff options
| author | Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de> | 2023-01-31 11:35:23 +0000 |
|---|---|---|
| committer | Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de> | 2024-04-08 11:57:17 +0000 |
| commit | c340e67deca58387541e33f4a597d82e07406170 (patch) | |
| tree | fb1cbf532011d316d1e50839d63c7450f860bf22 | |
| parent | fc27a91880f54bbf9c66fa545d508199e2c441bb (diff) | |
| download | rust-c340e67deca58387541e33f4a597d82e07406170.tar.gz rust-c340e67deca58387541e33f4a597d82e07406170.zip | |
Add pattern types to parser
| -rw-r--r-- | compiler/rustc_ast_passes/src/feature_gate.rs | 3 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/lib.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/pattern_type.rs | 29 | ||||
| -rw-r--r-- | compiler/rustc_feature/src/unstable.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_span/src/symbol.rs | 3 | ||||
| -rw-r--r-- | library/core/src/lib.rs | 3 | ||||
| -rw-r--r-- | library/core/src/pat.rs | 14 | ||||
| -rw-r--r-- | library/std/src/lib.rs | 3 | ||||
| -rw-r--r-- | library/std/src/pat.rs | 3 | ||||
| -rw-r--r-- | tests/ui/type/pattern_types/bad_pat.rs | 12 | ||||
| -rw-r--r-- | tests/ui/type/pattern_types/bad_pat.stderr | 19 | ||||
| -rw-r--r-- | tests/ui/type/pattern_types/feature-gate-pattern_types.rs | 14 | ||||
| -rw-r--r-- | tests/ui/type/pattern_types/feature-gate-pattern_types.stderr | 48 | ||||
| -rw-r--r-- | tests/ui/type/pattern_types/feature-gate-pattern_types2.rs | 12 |
14 files changed, 167 insertions, 0 deletions
diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index 5912dd3f931..d7cd3efe408 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -332,6 +332,9 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { 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) diff --git a/compiler/rustc_builtin_macros/src/lib.rs b/compiler/rustc_builtin_macros/src/lib.rs index 554dac0852f..1b4c6041294 100644 --- a/compiler/rustc_builtin_macros/src/lib.rs +++ b/compiler/rustc_builtin_macros/src/lib.rs @@ -46,6 +46,7 @@ mod format; 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, diff --git a/compiler/rustc_builtin_macros/src/pattern_type.rs b/compiler/rustc_builtin_macros/src/pattern_type.rs new file mode 100644 index 00000000000..54039c2c538 --- /dev/null +++ b/compiler/rustc_builtin_macros/src/pattern_type.rs @@ -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)) +} diff --git a/compiler/rustc_feature/src/unstable.rs b/compiler/rustc_feature/src/unstable.rs index 6fe51c62936..65621ba2764 100644 --- a/compiler/rustc_feature/src/unstable.rs +++ b/compiler/rustc_feature/src/unstable.rs @@ -215,6 +215,8 @@ declare_features! ( (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. diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 7a5647e979a..bfd0f77c237 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1009,6 +1009,7 @@ symbols! { io_stderr, io_stdout, irrefutable_let_patterns, + is, is_val_statically_known, isa_attribute, isize, @@ -1347,6 +1348,8 @@ symbols! { path, pattern_complexity, pattern_parentheses, + pattern_type, + pattern_types, phantom_data, pic, pie, diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 1e52d7ac412..0ba3a557a03 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -396,6 +396,9 @@ pub mod net; 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; diff --git a/library/core/src/pat.rs b/library/core/src/pat.rs new file mode 100644 index 00000000000..a10c4593342 --- /dev/null +++ b/library/core/src/pat.rs @@ -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 */ + }; +} diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index b509503ce4d..c713eefc72c 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -576,6 +576,9 @@ pub mod net; 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; diff --git a/library/std/src/pat.rs b/library/std/src/pat.rs new file mode 100644 index 00000000000..aeddd84c2cb --- /dev/null +++ b/library/std/src/pat.rs @@ -0,0 +1,3 @@ +//! Helper module for exporting the `pattern_type` macro + +pub use core::pattern_type; diff --git a/tests/ui/type/pattern_types/bad_pat.rs b/tests/ui/type/pattern_types/bad_pat.rs new file mode 100644 index 00000000000..4b19f610118 --- /dev/null +++ b/tests/ui/type/pattern_types/bad_pat.rs @@ -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 diff --git a/tests/ui/type/pattern_types/bad_pat.stderr b/tests/ui/type/pattern_types/bad_pat.stderr new file mode 100644 index 00000000000..12fcaf42b8e --- /dev/null +++ b/tests/ui/type/pattern_types/bad_pat.stderr @@ -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`. diff --git a/tests/ui/type/pattern_types/feature-gate-pattern_types.rs b/tests/ui/type/pattern_types/feature-gate-pattern_types.rs new file mode 100644 index 00000000000..3c507a9669d --- /dev/null +++ b/tests/ui/type/pattern_types/feature-gate-pattern_types.rs @@ -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' diff --git a/tests/ui/type/pattern_types/feature-gate-pattern_types.stderr b/tests/ui/type/pattern_types/feature-gate-pattern_types.stderr new file mode 100644 index 00000000000..6f74b89d224 --- /dev/null +++ b/tests/ui/type/pattern_types/feature-gate-pattern_types.stderr @@ -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`. diff --git a/tests/ui/type/pattern_types/feature-gate-pattern_types2.rs b/tests/ui/type/pattern_types/feature-gate-pattern_types2.rs new file mode 100644 index 00000000000..d7b3ea58e02 --- /dev/null +++ b/tests/ui/type/pattern_types/feature-gate-pattern_types2.rs @@ -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(_)); |
