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

0 commit comments

Comments
 (0)