Skip to content

Commit e3dd737

Browse files
committed
Add pattern types to parser
1 parent 846b80e commit e3dd737

File tree

55 files changed

+227
-104
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+227
-104
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
313313
ast::TyKind::TraitObject(_, ast::TraitObjectSyntax::DynStar, ..) => {
314314
gate_feature_post!(&self, dyn_star, ty.span, "dyn* trait objects are unstable");
315315
}
316+
ast::TyKind::Pat(..) => {
317+
gate_feature_post!(&self, pattern_types, ty.span, "pattern types are unstable");
318+
}
316319
_ => {}
317320
}
318321
visit::walk_ty(self, ty)

compiler/rustc_feature/src/active.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,8 @@ declare_features! (
477477
(active, object_safe_for_dispatch, "1.40.0", Some(43561), None),
478478
/// Allows using `#[optimize(X)]`.
479479
(active, optimize_attribute, "1.34.0", Some(54882), None),
480+
/// Allows using pattern types.
481+
(active, pattern_types, "CURRENT_RUSTC_VERSION", Some(54882), None),
480482
/// Allows `extern "platform-intrinsic" { ... }`.
481483
(active, platform_intrinsics, "1.4.0", Some(27731), None),
482484
/// Allows using `#![plugin(myplugin)]`.

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,12 @@ impl<'a> Parser<'a> {
342342
let span = lo.to(self.prev_token.span);
343343
let mut ty = self.mk_ty(span, kind);
344344

345+
if self.eat_keyword(sym::is) {
346+
let pat = self.parse_pat_no_top_alt(None)?;
347+
let span = lo.to(self.prev_token.span);
348+
ty = self.mk_ty(span, TyKind::Pat(ty, pat));
349+
}
350+
345351
// Try to recover from use of `+` with incorrect priority.
346352
match allow_plus {
347353
AllowPlus::Yes => self.maybe_recover_from_bad_type_plus(&ty)?,

compiler/rustc_span/src/symbol.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,7 @@ symbols! {
831831
intra_doc_pointers,
832832
intrinsics,
833833
irrefutable_let_patterns,
834+
is,
834835
isa_attribute,
835836
isize,
836837
issue,
@@ -1076,6 +1077,7 @@ symbols! {
10761077
pat_param,
10771078
path,
10781079
pattern_parentheses,
1080+
pattern_types,
10791081
phantom_data,
10801082
pin,
10811083
platform_intrinsics,

src/tools/tidy/src/features.rs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::collections::hash_map::{Entry, HashMap};
1414
use std::fmt;
1515
use std::fs;
1616
use std::num::NonZeroU32;
17-
use std::path::Path;
17+
use std::path::{Path, PathBuf};
1818

1919
use regex::Regex;
2020

@@ -51,6 +51,8 @@ pub struct Feature {
5151
pub since: Option<Version>,
5252
pub has_gate_test: bool,
5353
pub tracking_issue: Option<NonZeroU32>,
54+
pub file: PathBuf,
55+
pub line: usize,
5456
}
5557
impl Feature {
5658
fn tracking_issue_display(&self) -> impl fmt::Display {
@@ -72,7 +74,7 @@ pub struct CollectedFeatures {
7274
pub fn collect_lib_features(base_src_path: &Path) -> Features {
7375
let mut lib_features = Features::new();
7476

75-
map_lib_features(base_src_path, &mut |res, _, _| {
77+
map_lib_features(base_src_path, &mut |res| {
7678
if let Ok((name, feature)) = res {
7779
lib_features.insert(name.to_owned(), feature);
7880
}
@@ -185,23 +187,25 @@ pub fn check(
185187
.chain(lib_features.iter().map(|feat| (feat, "lib")));
186188
for ((feature_name, feature), kind) in all_features_iter {
187189
let since = if let Some(since) = feature.since { since } else { continue };
190+
let file = feature.file.display();
191+
let line = feature.line;
188192
if since > version && since != Version::CurrentPlaceholder {
189193
tidy_error!(
190194
bad,
191-
"The stabilization version {since} of {kind} feature `{feature_name}` is newer than the current {version}"
195+
"{file}:{line}: The stabilization version {since} of {kind} feature `{feature_name}` is newer than the current {version}"
192196
);
193197
}
194198
if channel == "nightly" && since == version {
195199
tidy_error!(
196200
bad,
197-
"The stabilization version {since} of {kind} feature `{feature_name}` is written out but should be {}",
201+
"{file}:{line}: The stabilization version {since} of {kind} feature `{feature_name}` is written out but should be {}",
198202
version::VERSION_PLACEHOLDER
199203
);
200204
}
201205
if channel != "nightly" && since == Version::CurrentPlaceholder {
202206
tidy_error!(
203207
bad,
204-
"The placeholder use of {kind} feature `{feature_name}` is not allowed on the {channel} channel",
208+
"{file}:{line}: The placeholder use of {kind} feature `{feature_name}` is not allowed on the {channel} channel",
205209
);
206210
}
207211
}
@@ -435,7 +439,14 @@ fn collect_lang_features_in(features: &mut Features, base: &Path, file: &str, ba
435439
);
436440
}
437441
Entry::Vacant(e) => {
438-
e.insert(Feature { level, since, has_gate_test: false, tracking_issue });
442+
e.insert(Feature {
443+
level,
444+
since,
445+
has_gate_test: false,
446+
tracking_issue,
447+
file: path.to_path_buf(),
448+
line: line_number,
449+
});
439450
}
440451
}
441452
}
@@ -447,16 +458,16 @@ fn get_and_check_lib_features(
447458
lang_features: &Features,
448459
) -> Features {
449460
let mut lib_features = Features::new();
450-
map_lib_features(base_src_path, &mut |res, file, line| match res {
461+
map_lib_features(base_src_path, &mut |res| match res {
451462
Ok((name, f)) => {
452463
let mut check_features = |f: &Feature, list: &Features, display: &str| {
453464
if let Some(ref s) = list.get(name) {
454465
if f.tracking_issue != s.tracking_issue && f.level != Status::Stable {
455466
tidy_error!(
456467
bad,
457468
"{}:{}: `issue` \"{}\" mismatches the {} `issue` of \"{}\"",
458-
file.display(),
459-
line,
469+
f.file.display(),
470+
f.line,
460471
f.tracking_issue_display(),
461472
display,
462473
s.tracking_issue_display(),
@@ -468,7 +479,7 @@ fn get_and_check_lib_features(
468479
check_features(&f, &lib_features, "previous");
469480
lib_features.insert(name.to_owned(), f);
470481
}
471-
Err(msg) => {
482+
Err((msg, file, line)) => {
472483
tidy_error!(bad, "{}:{}: {}", file.display(), line, msg);
473484
}
474485
});
@@ -477,7 +488,7 @@ fn get_and_check_lib_features(
477488

478489
fn map_lib_features(
479490
base_src_path: &Path,
480-
mf: &mut dyn FnMut(Result<(&str, Feature), &str>, &Path, usize),
491+
mf: &mut dyn FnMut(Result<(&str, Feature), (&str, &Path, usize)>),
481492
) {
482493
walk(
483494
base_src_path,
@@ -514,7 +525,7 @@ fn map_lib_features(
514525
while let Some((i, line)) = iter_lines.next() {
515526
macro_rules! err {
516527
($msg:expr) => {{
517-
mf(Err($msg), file, i + 1);
528+
mf(Err(($msg, file, i + 1)));
518529
continue;
519530
}};
520531
}
@@ -532,7 +543,10 @@ fn map_lib_features(
532543
f.tracking_issue = find_attr_val(line, "issue").and_then(handle_issue_none);
533544
}
534545
if line.ends_with(']') {
535-
mf(Ok((name, f.clone())), file, i + 1);
546+
let mut f = f.clone();
547+
f.file = file.to_path_buf();
548+
f.line = i + 1;
549+
mf(Ok((name, f)));
536550
} else if !line.ends_with(',') && !line.ends_with('\\') && !line.ends_with('"')
537551
{
538552
// We need to bail here because we might have missed the
@@ -561,8 +575,10 @@ fn map_lib_features(
561575
since: None,
562576
has_gate_test: false,
563577
tracking_issue: find_attr_val(line, "issue").and_then(handle_issue_none),
578+
file: file.to_path_buf(),
579+
line: i + 1,
564580
};
565-
mf(Ok((feature_name, feature)), file, i + 1);
581+
mf(Ok((feature_name, feature)));
566582
continue;
567583
}
568584
let level = if line.contains("[unstable(") {
@@ -590,9 +606,16 @@ fn map_lib_features(
590606
};
591607
let tracking_issue = find_attr_val(line, "issue").and_then(handle_issue_none);
592608

593-
let feature = Feature { level, since, has_gate_test: false, tracking_issue };
609+
let feature = Feature {
610+
level,
611+
since,
612+
has_gate_test: false,
613+
tracking_issue,
614+
file: file.to_path_buf(),
615+
line: i + 1,
616+
};
594617
if line.contains(']') {
595-
mf(Ok((feature_name, feature)), file, i + 1);
618+
mf(Ok((feature_name, feature)));
596619
} else {
597620
becoming_feature = Some((feature_name, feature));
598621
}

tests/ui/const-generics/bad-const-generic-exprs.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ help: expressions must be enclosed in braces to be used as const generic argumen
142142
LL | let _: Wow<{ (1 + 2) * 3 }>;
143143
| + +
144144

145-
error: expected one of `,` or `>`, found `0`
145+
error: expected one of `,`, `>`, or `is`, found `0`
146146
--> $DIR/bad-const-generic-exprs.rs:43:17
147147
|
148148
LL | let _: Wow<!0>;
149-
| - ^ expected one of `,` or `>`
149+
| - ^ expected one of `,`, `>`, or `is`
150150
| |
151151
| while parsing the type for `_`
152152
|

tests/ui/fn/fn-recover-return-sign2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
fn foo() => impl Fn() => bool {
55
//~^ ERROR return types are denoted using `->`
6-
//~| ERROR expected one of `+`, `->`, `::`, `where`, or `{`, found `=>`
6+
//~| ERROR expected one of `+`, `->`, `::`, `is`, `where`, or `{`, found `=>`
77
unimplemented!()
88
}

tests/ui/fn/fn-recover-return-sign2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ error: return types are denoted using `->`
44
LL | fn foo() => impl Fn() => bool {
55
| ^^ help: use `->` instead
66

7-
error: expected one of `+`, `->`, `::`, `where`, or `{`, found `=>`
7+
error: expected one of `+`, `->`, `::`, `is`, `where`, or `{`, found `=>`
88
--> $DIR/fn-recover-return-sign2.rs:4:23
99
|
1010
LL | fn foo() => impl Fn() => bool {
11-
| ^^ expected one of `+`, `->`, `::`, `where`, or `{`
11+
| ^^ expected one of `+`, `->`, `::`, `is`, `where`, or `{`
1212

1313
error: aborting due to 2 previous errors
1414

tests/ui/generic-associated-types/parse/trait-path-expected-token.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ trait X {
33
}
44

55
fn f1<'a>(arg : Box<dyn X<Y = B = &'a ()>>) {}
6-
//~^ ERROR: expected one of `!`, `(`, `+`, `,`, `::`, `<`, or `>`, found `=`
6+
//~^ ERROR: expected one of `!`, `(`, `+`, `,`, `::`, `<`, `>`, or `is`, found `=`
77

88
fn main() {}

tests/ui/generic-associated-types/parse/trait-path-expected-token.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: expected one of `!`, `(`, `+`, `,`, `::`, `<`, or `>`, found `=`
1+
error: expected one of `!`, `(`, `+`, `,`, `::`, `<`, `>`, or `is`, found `=`
22
--> $DIR/trait-path-expected-token.rs:5:33
33
|
44
LL | fn f1<'a>(arg : Box<dyn X<Y = B = &'a ()>>) {}
5-
| - ^ expected one of 7 possible tokens
5+
| - ^ expected one of 8 possible tokens
66
| |
77
| maybe try to close unmatched angle bracket
88

0 commit comments

Comments
 (0)