Skip to content

Commit c340e67

Browse files
committed
Add pattern types to parser
1 parent fc27a91 commit c340e67

File tree

14 files changed

+167
-0
lines changed

14 files changed

+167
-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
@@ -332,6 +332,9 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
332332
ast::TyKind::Never => {
333333
gate!(&self, never_type, ty.span, "the `!` type is experimental");
334334
}
335+
ast::TyKind::Pat(..) => {
336+
gate!(&self, pattern_types, ty.span, "pattern types are unstable");
337+
}
335338
_ => {}
336339
}
337340
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
@@ -46,6 +46,7 @@ mod format;
4646
mod format_foreign;
4747
mod global_allocator;
4848
mod log_syntax;
49+
mod pattern_type;
4950
mod source_util;
5051
mod test;
5152
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use rustc_ast::{ast, ptr::P, tokenstream::TokenStream, Pat, Ty};
2+
use rustc_errors::PResult;
3+
use rustc_expand::base::{self, DummyResult, ExpandResult, ExtCtxt, MacroExpanderResult};
4+
use rustc_span::{sym, Span};
5+
6+
pub fn expand<'cx>(
7+
cx: &'cx mut ExtCtxt<'_>,
8+
sp: Span,
9+
tts: TokenStream,
10+
) -> MacroExpanderResult<'cx> {
11+
let (ty, pat) = match parse_pat_ty(cx, tts) {
12+
Ok(parsed) => parsed,
13+
Err(err) => {
14+
return ExpandResult::Ready(DummyResult::any(sp, err.emit()));
15+
}
16+
};
17+
18+
ExpandResult::Ready(base::MacEager::ty(cx.ty(sp, ast::TyKind::Pat(ty, pat))))
19+
}
20+
21+
fn parse_pat_ty<'a>(cx: &mut ExtCtxt<'a>, stream: TokenStream) -> PResult<'a, (P<Ty>, P<Pat>)> {
22+
let mut parser = cx.new_parser_from_tts(stream);
23+
24+
let ty = parser.parse_ty()?;
25+
parser.eat_keyword(sym::is);
26+
let pat = parser.parse_pat_no_top_alt(None, None)?;
27+
28+
Ok((ty, pat))
29+
}

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ declare_features! (
215215
(internal, omit_gdb_pretty_printer_section, "1.5.0", None),
216216
/// Set the maximum pattern complexity allowed (not limited by default).
217217
(internal, pattern_complexity, "1.78.0", None),
218+
/// Allows using pattern types.
219+
(internal, pattern_types, "CURRENT_RUSTC_VERSION", Some(54882)),
218220
/// Allows using `#[prelude_import]` on glob `use` items.
219221
(internal, prelude_import, "1.2.0", None),
220222
/// Used to identify crates that contain the profiler runtime.

compiler/rustc_span/src/symbol.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,7 @@ symbols! {
10091009
io_stderr,
10101010
io_stdout,
10111011
irrefutable_let_patterns,
1012+
is,
10121013
is_val_statically_known,
10131014
isa_attribute,
10141015
isize,
@@ -1347,6 +1348,8 @@ symbols! {
13471348
path,
13481349
pattern_complexity,
13491350
pattern_parentheses,
1351+
pattern_type,
1352+
pattern_types,
13501353
phantom_data,
13511354
pic,
13521355
pie,

library/core/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,9 @@ pub mod net;
396396
pub mod option;
397397
pub mod panic;
398398
pub mod panicking;
399+
#[cfg(not(bootstrap))]
400+
#[unstable(feature = "core_pattern_types", issue = "none")]
401+
pub mod pat;
399402
pub mod pin;
400403
pub mod result;
401404
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+
/// ```ignore (cannot test this from within core yet)
5+
/// type Positive = std::pat::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
@@ -576,6 +576,9 @@ pub mod net;
576576
pub mod num;
577577
pub mod os;
578578
pub mod panic;
579+
#[cfg(not(bootstrap))]
580+
#[unstable(feature = "core_pattern_types", issue = "none")]
581+
pub mod pat;
579582
pub mod path;
580583
pub mod process;
581584
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)