Skip to content

Commit 33407fb

Browse files
committed
Add pattern types to parser
1 parent 21f49b4 commit 33407fb

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
@@ -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;
@@ -96,6 +97,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
9697
log_syntax: log_syntax::expand_log_syntax,
9798
module_path: source_util::expand_mod,
9899
option_env: env::expand_option_env,
100+
pattern_type: pattern_type::expand,
99101
std_panic: edition_panic::expand_panic,
100102
stringify: source_util::expand_stringify,
101103
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
@@ -554,6 +554,8 @@ declare_features! (
554554
(unstable, offset_of_nested, "CURRENT_RUSTC_VERSION", Some(120140)),
555555
/// Allows using `#[optimize(X)]`.
556556
(unstable, optimize_attribute, "1.34.0", Some(54882)),
557+
/// Allows using pattern types.
558+
(unstable, pattern_types, "CURRENT_RUSTC_VERSION", Some(54882)),
557559
/// Allows macro attributes on expressions, statements and non-inline modules.
558560
(unstable, proc_macro_hygiene, "1.30.0", Some(54727)),
559561
/// 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
@@ -916,6 +916,7 @@ symbols! {
916916
io_stderr,
917917
io_stdout,
918918
irrefutable_let_patterns,
919+
is,
919920
is_val_statically_known,
920921
isa_attribute,
921922
isize,
@@ -1213,6 +1214,8 @@ symbols! {
12131214
pat_param,
12141215
path,
12151216
pattern_parentheses,
1217+
pattern_type,
1218+
pattern_types,
12161219
phantom_data,
12171220
pic,
12181221
pie,

library/core/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@ pub mod net;
383383
pub mod option;
384384
pub mod panic;
385385
pub mod panicking;
386+
#[cfg(not(bootstrap))]
387+
#[unstable(feature = "core_pattern_types", issue = "none")]
388+
pub mod pat;
386389
pub mod pin;
387390
pub mod result;
388391
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
@@ -569,6 +569,9 @@ pub mod net;
569569
pub mod num;
570570
pub mod os;
571571
pub mod panic;
572+
#[cfg(not(bootstrap))]
573+
#[unstable(feature = "core_pattern_types", issue = "none")]
574+
pub mod pat;
572575
pub mod path;
573576
pub mod process;
574577
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)