-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use rustc_ast::{ast, ptr::P, tokenstream::TokenStream, Pat, Ty}; | ||
use rustc_errors::PResult; | ||
use rustc_expand::base::{self, DummyResult, ExtCtxt}; | ||
use rustc_span::{sym, Span}; | ||
|
||
pub fn expand( | ||
cx: &mut ExtCtxt<'_>, | ||
sp: Span, | ||
tts: TokenStream, | ||
) -> Box<dyn base::MacResult + 'static> { | ||
let (ty, pat) = match parse_pat_ty(cx, tts) { | ||
Ok(parsed) => parsed, | ||
Err(err) => { | ||
err.emit(); | ||
return DummyResult::any(sp); | ||
} | ||
}; | ||
|
||
base::MacEager::ty(cx.ty(sp, ast::TyKind::Pat(ty, pat))) | ||
} | ||
|
||
fn parse_pat_ty<'a>(cx: &mut ExtCtxt<'a>, stream: TokenStream) -> PResult<'a, (P<Ty>, P<Pat>)> { | ||
let mut parser = cx.new_parser_from_tts(stream); | ||
|
||
let ty = parser.parse_ty()?; | ||
parser.eat_keyword(sym::is); | ||
let pat = parser.parse_pat_no_top_alt(None, None)?; | ||
|
||
Ok((ty, pat)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//! Helper module for exporting the `pattern_type` macro | ||
/// Creates a pattern type. | ||
/// ```ignore (cannot test this from within core yet) | ||
/// type Positive = std::pat::pattern_type!(i32 is 1..); | ||
/// ``` | ||
#[macro_export] | ||
#[rustc_builtin_macro(pattern_type)] | ||
#[unstable(feature = "core_pattern_type", issue = "none")] | ||
macro_rules! pattern_type { | ||
($($arg:tt)*) => { | ||
/* compiler built-in */ | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
//! Helper module for exporting the `pattern_type` macro | ||
pub use core::pattern_type; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// compile-flags: -Zno-analysis | ||
|
||
#![feature(pattern_types)] | ||
#![feature(core_pattern_types)] | ||
#![feature(core_pattern_type)] | ||
|
||
use std::pat::pattern_type; | ||
|
||
type NonNullU32_2 = pattern_type!(u32 is 1..=); | ||
//~^ ERROR: inclusive range with no end | ||
type Positive2 = pattern_type!(i32 is 0..=); | ||
//~^ ERROR: inclusive range with no end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
error[E0586]: inclusive range with no end | ||
--> $DIR/bad_pat.rs:9:43 | ||
| | ||
LL | type NonNullU32_2 = pattern_type!(u32 is 1..=); | ||
| ^^^ help: use `..` instead | ||
| | ||
= note: inclusive ranges must be bounded at the end (`..=b` or `a..=b`) | ||
|
||
error[E0586]: inclusive range with no end | ||
--> $DIR/bad_pat.rs:11:40 | ||
| | ||
LL | type Positive2 = pattern_type!(i32 is 0..=); | ||
| ^^^ help: use `..` instead | ||
| | ||
= note: inclusive ranges must be bounded at the end (`..=b` or `a..=b`) | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0586`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// compile-flags: -Zno-analysis | ||
|
||
use std::pat::pattern_type; | ||
|
||
type NonNullU32 = pattern_type!(u32 is 1..); | ||
//~^ use of unstable library feature 'core_pattern_type' | ||
type Percent = pattern_type!(u32 is 0..=100); | ||
//~^ use of unstable library feature 'core_pattern_type' | ||
type Negative = pattern_type!(i32 is ..=0); | ||
//~^ use of unstable library feature 'core_pattern_type' | ||
type Positive = pattern_type!(i32 is 0..); | ||
//~^ use of unstable library feature 'core_pattern_type' | ||
type Always = pattern_type!(Option<u32> is Some(_)); | ||
//~^ use of unstable library feature 'core_pattern_type' |
48 changes: 48 additions & 0 deletions
48
tests/ui/type/pattern_types/feature-gate-pattern_types.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
error[E0658]: use of unstable library feature 'core_pattern_type' | ||
--> $DIR/feature-gate-pattern_types.rs:5:19 | ||
| | ||
LL | type NonNullU32 = pattern_type!(u32 is 1..); | ||
| ^^^^^^^^^^^^ | ||
| | ||
= help: add `#![feature(core_pattern_type)]` to the crate attributes to enable | ||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date | ||
|
||
error[E0658]: use of unstable library feature 'core_pattern_type' | ||
--> $DIR/feature-gate-pattern_types.rs:7:16 | ||
| | ||
LL | type Percent = pattern_type!(u32 is 0..=100); | ||
| ^^^^^^^^^^^^ | ||
| | ||
= help: add `#![feature(core_pattern_type)]` to the crate attributes to enable | ||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date | ||
|
||
error[E0658]: use of unstable library feature 'core_pattern_type' | ||
--> $DIR/feature-gate-pattern_types.rs:9:17 | ||
| | ||
LL | type Negative = pattern_type!(i32 is ..=0); | ||
| ^^^^^^^^^^^^ | ||
| | ||
= help: add `#![feature(core_pattern_type)]` to the crate attributes to enable | ||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date | ||
|
||
error[E0658]: use of unstable library feature 'core_pattern_type' | ||
--> $DIR/feature-gate-pattern_types.rs:11:17 | ||
| | ||
LL | type Positive = pattern_type!(i32 is 0..); | ||
| ^^^^^^^^^^^^ | ||
| | ||
= help: add `#![feature(core_pattern_type)]` to the crate attributes to enable | ||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date | ||
|
||
error[E0658]: use of unstable library feature 'core_pattern_type' | ||
--> $DIR/feature-gate-pattern_types.rs:13:15 | ||
| | ||
LL | type Always = pattern_type!(Option<u32> is Some(_)); | ||
| ^^^^^^^^^^^^ | ||
| | ||
= help: add `#![feature(core_pattern_type)]` to the crate attributes to enable | ||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date | ||
|
||
error: aborting due to 5 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
12 changes: 12 additions & 0 deletions
12
tests/ui/type/pattern_types/feature-gate-pattern_types2.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// compile-flags: -Zno-analysis | ||
// check-pass | ||
|
||
#![feature(core_pattern_type)] | ||
|
||
use std::pat::pattern_type; | ||
|
||
type NonNullU32 = pattern_type!(u32 is 1..); | ||
type Percent = pattern_type!(u32 is 0..=100); | ||
type Negative = pattern_type!(i32 is ..=0); | ||
type Positive = pattern_type!(i32 is 0..); | ||
type Always = pattern_type!(Option<u32> is Some(_)); |