Skip to content

Commit

Permalink
Add pattern types to parser
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jan 22, 2024
1 parent 3f4b4cb commit 2577285
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 0 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,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)
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_builtin_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ mod format;
mod format_foreign;
mod global_allocator;
mod log_syntax;
mod pattern_type;
mod source_util;
mod test;
mod trace_macros;
Expand Down Expand Up @@ -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,
Expand Down
30 changes: 30 additions & 0 deletions compiler/rustc_builtin_macros/src/pattern_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use rustc_ast::{ast, ptr::P, tokenstream::TokenStream, Pat, Ty};
use rustc_errors::PResult;
use rustc_expand::base::{self, DummyResult, ExtCtxt};
use rustc_span::{sym, Span};

pub fn expand(
cx: &mut ExtCtxt<'_>,
sp: Span,
tts: TokenStream,
) -> Box<dyn base::MacResult + 'static> {
let (ty, pat) = match parse_pat_ty(cx, tts) {
Ok(parsed) => parsed,
Err(err) => {
err.emit();
return DummyResult::any(sp);
}
};

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))
}
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,8 @@ declare_features! (
(unstable, offset_of_nested, "CURRENT_RUSTC_VERSION", Some(120140)),
/// Allows using `#[optimize(X)]`.
(unstable, optimize_attribute, "1.34.0", Some(54882)),
/// Allows using pattern types.
(unstable, pattern_types, "CURRENT_RUSTC_VERSION", Some(54882)),
/// Allows macro attributes on expressions, statements and non-inline modules.
(unstable, proc_macro_hygiene, "1.30.0", Some(54727)),
/// Allows `&raw const $place_expr` and `&raw mut $place_expr` expressions.
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,7 @@ symbols! {
io_stderr,
io_stdout,
irrefutable_let_patterns,
is,
isa_attribute,
isize,
issue,
Expand Down Expand Up @@ -1204,6 +1205,8 @@ symbols! {
pat_param,
path,
pattern_parentheses,
pattern_type,
pattern_types,
phantom_data,
pic,
pie,
Expand Down
3 changes: 3 additions & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,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;
Expand Down
14 changes: 14 additions & 0 deletions library/core/src/pat.rs
Original file line number Diff line number Diff line change
@@ -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 */
};
}
3 changes: 3 additions & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,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;
Expand Down
3 changes: 3 additions & 0 deletions library/std/src/pat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Helper module for exporting the `pattern_type` macro
pub use core::pattern_type;
12 changes: 12 additions & 0 deletions tests/ui/type/pattern_types/bad_pat.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 19 additions & 0 deletions tests/ui/type/pattern_types/bad_pat.stderr
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 0 deletions tests/ui/type/pattern_types/feature-gate-pattern_types.rs
Original file line number Diff line number Diff line change
@@ -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'
48 changes: 48 additions & 0 deletions tests/ui/type/pattern_types/feature-gate-pattern_types.stderr
Original file line number Diff line number Diff line change
@@ -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 changes: 12 additions & 0 deletions tests/ui/type/pattern_types/feature-gate-pattern_types2.rs
Original file line number Diff line number Diff line change
@@ -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(_));

0 comments on commit 2577285

Please sign in to comment.