Skip to content

Commit 7980305

Browse files
committed
Add pattern types to parser
1 parent c024af2 commit 7980305

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
@@ -909,6 +909,7 @@ symbols! {
909909
io_stderr,
910910
io_stdout,
911911
irrefutable_let_patterns,
912+
is,
912913
isa_attribute,
913914
isize,
914915
issue,
@@ -1203,6 +1204,8 @@ symbols! {
12031204
pat_param,
12041205
path,
12051206
pattern_parentheses,
1207+
pattern_type,
1208+
pattern_types,
12061209
phantom_data,
12071210
pic,
12081211
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

0 commit comments

Comments
 (0)