Skip to content

Commit c4bf74a

Browse files
committed
Add pattern types to parser
1 parent 858b2fc commit c4bf74a

File tree

14 files changed

+168
-0
lines changed

14 files changed

+168
-0
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
334334
ast::TyKind::Never => {
335335
gate!(&self, never_type, ty.span, "the `!` type is experimental");
336336
}
337+
ast::TyKind::Pat(..) => {
338+
gate!(&self, pattern_types, ty.span, "pattern types are unstable");
339+
}
337340
_ => {}
338341
}
339342
visit::walk_ty(self, ty)

compiler/rustc_builtin_macros/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ mod format;
4545
mod format_foreign;
4646
mod global_allocator;
4747
mod log_syntax;
48+
mod pattern_type;
4849
mod source_util;
4950
mod test;
5051
mod trace_macros;
@@ -95,6 +96,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
9596
log_syntax: log_syntax::expand_log_syntax,
9697
module_path: source_util::expand_mod,
9798
option_env: env::expand_option_env,
99+
pattern_type: pattern_type::expand,
98100
std_panic: edition_panic::expand_panic,
99101
stringify: source_util::expand_stringify,
100102
trace_macros: trace_macros::expand_trace_macros,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use rustc_ast::{ast, ptr::P, tokenstream::TokenStream, Pat, Ty};
2+
use rustc_errors::PResult;
3+
use rustc_expand::base::{self, DummyResult, ExtCtxt};
4+
use rustc_span::{sym, Span};
5+
6+
pub fn expand(
7+
cx: &mut ExtCtxt<'_>,
8+
sp: Span,
9+
tts: TokenStream,
10+
) -> Box<dyn base::MacResult + 'static> {
11+
let (ty, pat) = match parse_pat_ty(cx, tts) {
12+
Ok(parsed) => parsed,
13+
Err(err) => {
14+
err.emit();
15+
return DummyResult::any(sp);
16+
}
17+
};
18+
19+
base::MacEager::ty(cx.ty(sp, ast::TyKind::Pat(ty, pat)))
20+
}
21+
22+
fn parse_pat_ty<'a>(cx: &mut ExtCtxt<'a>, stream: TokenStream) -> PResult<'a, (P<Ty>, P<Pat>)> {
23+
let mut parser = cx.new_parser_from_tts(stream);
24+
25+
let ty = parser.parse_ty()?;
26+
parser.eat_keyword(sym::is);
27+
let pat = parser.parse_pat_no_top_alt(None, None)?;
28+
29+
Ok((ty, pat))
30+
}

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,8 @@ declare_features! (
552552
(unstable, offset_of_enum, "1.75.0", Some(106655)),
553553
/// Allows using `#[optimize(X)]`.
554554
(unstable, optimize_attribute, "1.34.0", Some(54882)),
555+
/// Allows using pattern types.
556+
(unstable, pattern_types, "CURRENT_RUSTC_VERSION", Some(54882)),
555557
/// Allows macro attributes on expressions, statements and non-inline modules.
556558
(unstable, proc_macro_hygiene, "1.30.0", Some(54727)),
557559
/// Allows `&raw const $place_expr` and `&raw mut $place_expr` expressions.

compiler/rustc_span/src/symbol.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,7 @@ symbols! {
908908
io_stderr,
909909
io_stdout,
910910
irrefutable_let_patterns,
911+
is,
911912
isa_attribute,
912913
isize,
913914
issue,
@@ -1202,6 +1203,8 @@ symbols! {
12021203
pat_param,
12031204
path,
12041205
pattern_parentheses,
1206+
pattern_type,
1207+
pattern_types,
12051208
phantom_data,
12061209
pic,
12071210
pie,

library/core/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,9 @@ pub mod net;
379379
pub mod option;
380380
pub mod panic;
381381
pub mod panicking;
382+
#[cfg(not(bootstrap))]
383+
#[unstable(feature = "core_pattern_types", issue = "none")]
384+
pub mod pat;
382385
pub mod pin;
383386
pub mod result;
384387
pub mod sync;

library/core/src/pat.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//! Helper module for exporting the `pattern_type` macro
2+
3+
/// Creates a pattern type.
4+
/// ```rust
5+
/// type Positive = pattern_type!(i32 is 1..);
6+
/// ```
7+
#[macro_export]
8+
#[rustc_builtin_macro(pattern_type)]
9+
#[unstable(feature = "core_pattern_type", issue = "none")]
10+
macro_rules! pattern_type {
11+
($($arg:tt)*) => {
12+
/* compiler built-in */
13+
};
14+
}

library/std/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,9 @@ pub mod net;
570570
pub mod num;
571571
pub mod os;
572572
pub mod panic;
573+
#[cfg(not(bootstrap))]
574+
#[unstable(feature = "core_pattern_types", issue = "none")]
575+
pub mod pat;
573576
pub mod path;
574577
pub mod process;
575578
pub mod sync;

library/std/src/pat.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//! Helper module for exporting the `pattern_type` macro
2+
3+
pub use core::pattern_type;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// compile-flags: -Zno-analysis
2+
3+
#![feature(pattern_types)]
4+
#![feature(core_pattern_types)]
5+
#![feature(core_pattern_type)]
6+
7+
use std::pat::pattern_type;
8+
9+
type NonNullU32_2 = pattern_type!(u32 is 1..=);
10+
//~^ ERROR: inclusive range with no end
11+
type Positive2 = pattern_type!(i32 is 0..=);
12+
//~^ ERROR: inclusive range with no end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0586]: inclusive range with no end
2+
--> $DIR/bad_pat.rs:9:43
3+
|
4+
LL | type NonNullU32_2 = pattern_type!(u32 is 1..=);
5+
| ^^^ help: use `..` instead
6+
|
7+
= note: inclusive ranges must be bounded at the end (`..=b` or `a..=b`)
8+
9+
error[E0586]: inclusive range with no end
10+
--> $DIR/bad_pat.rs:11:40
11+
|
12+
LL | type Positive2 = pattern_type!(i32 is 0..=);
13+
| ^^^ help: use `..` instead
14+
|
15+
= note: inclusive ranges must be bounded at the end (`..=b` or `a..=b`)
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0586`.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// compile-flags: -Zno-analysis
2+
3+
use std::pat::pattern_type;
4+
5+
type NonNullU32 = pattern_type!(u32 is 1..);
6+
//~^ use of unstable library feature 'core_pattern_type'
7+
type Percent = pattern_type!(u32 is 0..=100);
8+
//~^ use of unstable library feature 'core_pattern_type'
9+
type Negative = pattern_type!(i32 is ..=0);
10+
//~^ use of unstable library feature 'core_pattern_type'
11+
type Positive = pattern_type!(i32 is 0..);
12+
//~^ use of unstable library feature 'core_pattern_type'
13+
type Always = pattern_type!(Option<u32> is Some(_));
14+
//~^ use of unstable library feature 'core_pattern_type'
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
error[E0658]: use of unstable library feature 'core_pattern_type'
2+
--> $DIR/feature-gate-pattern_types.rs:5:19
3+
|
4+
LL | type NonNullU32 = pattern_type!(u32 is 1..);
5+
| ^^^^^^^^^^^^
6+
|
7+
= help: add `#![feature(core_pattern_type)]` to the crate attributes to enable
8+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
9+
10+
error[E0658]: use of unstable library feature 'core_pattern_type'
11+
--> $DIR/feature-gate-pattern_types.rs:7:16
12+
|
13+
LL | type Percent = pattern_type!(u32 is 0..=100);
14+
| ^^^^^^^^^^^^
15+
|
16+
= help: add `#![feature(core_pattern_type)]` to the crate attributes to enable
17+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
18+
19+
error[E0658]: use of unstable library feature 'core_pattern_type'
20+
--> $DIR/feature-gate-pattern_types.rs:9:17
21+
|
22+
LL | type Negative = pattern_type!(i32 is ..=0);
23+
| ^^^^^^^^^^^^
24+
|
25+
= help: add `#![feature(core_pattern_type)]` to the crate attributes to enable
26+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
27+
28+
error[E0658]: use of unstable library feature 'core_pattern_type'
29+
--> $DIR/feature-gate-pattern_types.rs:11:17
30+
|
31+
LL | type Positive = pattern_type!(i32 is 0..);
32+
| ^^^^^^^^^^^^
33+
|
34+
= help: add `#![feature(core_pattern_type)]` to the crate attributes to enable
35+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
36+
37+
error[E0658]: use of unstable library feature 'core_pattern_type'
38+
--> $DIR/feature-gate-pattern_types.rs:13:15
39+
|
40+
LL | type Always = pattern_type!(Option<u32> is Some(_));
41+
| ^^^^^^^^^^^^
42+
|
43+
= help: add `#![feature(core_pattern_type)]` to the crate attributes to enable
44+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
45+
46+
error: aborting due to 5 previous errors
47+
48+
For more information about this error, try `rustc --explain E0658`.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// compile-flags: -Zno-analysis
2+
// check-pass
3+
4+
#![feature(core_pattern_type)]
5+
6+
use std::pat::pattern_type;
7+
8+
type NonNullU32 = pattern_type!(u32 is 1..);
9+
type Percent = pattern_type!(u32 is 0..=100);
10+
type Negative = pattern_type!(i32 is ..=0);
11+
type Positive = pattern_type!(i32 is 0..);
12+
type Always = pattern_type!(Option<u32> is Some(_));

0 commit comments

Comments
 (0)