Skip to content

Commit 43bad44

Browse files
Reestablish feature gate for RangeFrom in slices
1 parent 45d9dd6 commit 43bad44

6 files changed

+99
-0
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

+16
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,22 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
540540

541541
fn visit_pat(&mut self, pattern: &'a ast::Pat) {
542542
match &pattern.kind {
543+
PatKind::Slice(pats) => {
544+
for pat in pats {
545+
let inner_pat = match &pat.kind {
546+
PatKind::Ident(.., Some(pat)) => pat,
547+
_ => pat,
548+
};
549+
if let PatKind::Range(Some(_), None, Spanned { .. }) = inner_pat.kind {
550+
gate_feature_post!(
551+
&self,
552+
half_open_range_patterns,
553+
pat.span,
554+
"`X..` patterns in slices are experimental"
555+
);
556+
}
557+
}
558+
}
543559
PatKind::Box(..) => {
544560
gate_feature_post!(
545561
&self,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(half_open_range_patterns)]
2+
#![feature(exclusive_range_pattern)]
3+
4+
fn main() {
5+
let xs = [13, 1, 5, 2, 3, 1, 21, 8];
6+
let [a, b, c, rest @ ..] = xs;
7+
// Consider the following example:
8+
assert!(a == 13 && b == 1 && c == 5 && rest.len() == 5);
9+
10+
// What if we wanted to pull this apart without individually binding a, b, and c?
11+
let [first_three @ ..3, rest @ 2..] = xs;
12+
//~^ pattern requires 2 elements but array has 8
13+
// This is somewhat unintuitive and makes slice patterns exceedingly verbose.
14+
// We want to stabilize half-open RangeFrom (`X..`) patterns
15+
// but without banning us from using them for a more efficient slice pattern syntax.
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0527]: pattern requires 2 elements but array has 8
2+
--> $DIR/slice_pattern_syntax_problem0.rs:11:9
3+
|
4+
LL | let [first_three @ ..3, rest @ 2..] = xs;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 8 elements
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0527`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Instead of allowing the previous case, maintain the feature gate for slice patterns for now.
2+
fn main() {
3+
let xs = [13, 1, 5, 2, 3, 1, 21, 8];
4+
let [a @ 3.., b @ ..3, c @ 4..6, ..] = xs;
5+
//~^ `X..` patterns in slices are experimental
6+
//~| half-open range patterns are unstable
7+
//~| exclusive range pattern syntax is experimental
8+
//~| exclusive range pattern syntax is experimental
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error[E0658]: half-open range patterns are unstable
2+
--> $DIR/slice_pattern_syntax_problem1.rs:4:23
3+
|
4+
LL | let [a @ 3.., b @ ..3, c @ 4..6, ..] = xs;
5+
| ^^^
6+
|
7+
= note: see issue #67264 <https://github.com/rust-lang/rust/issues/67264> for more information
8+
= help: add `#![feature(half_open_range_patterns)]` to the crate attributes to enable
9+
10+
error[E0658]: `X..` patterns in slices are experimental
11+
--> $DIR/slice_pattern_syntax_problem1.rs:4:10
12+
|
13+
LL | let [a @ 3.., b @ ..3, c @ 4..6, ..] = xs;
14+
| ^^^^^^^
15+
|
16+
= note: see issue #67264 <https://github.com/rust-lang/rust/issues/67264> for more information
17+
= help: add `#![feature(half_open_range_patterns)]` to the crate attributes to enable
18+
19+
error[E0658]: exclusive range pattern syntax is experimental
20+
--> $DIR/slice_pattern_syntax_problem1.rs:4:23
21+
|
22+
LL | let [a @ 3.., b @ ..3, c @ 4..6, ..] = xs;
23+
| ^^^
24+
|
25+
= note: see issue #37854 <https://github.com/rust-lang/rust/issues/37854> for more information
26+
= help: add `#![feature(exclusive_range_pattern)]` to the crate attributes to enable
27+
28+
error[E0658]: exclusive range pattern syntax is experimental
29+
--> $DIR/slice_pattern_syntax_problem1.rs:4:32
30+
|
31+
LL | let [a @ 3.., b @ ..3, c @ 4..6, ..] = xs;
32+
| ^^^^
33+
|
34+
= note: see issue #37854 <https://github.com/rust-lang/rust/issues/37854> for more information
35+
= help: add `#![feature(exclusive_range_pattern)]` to the crate attributes to enable
36+
37+
error: aborting due to 4 previous errors
38+
39+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// run-pass
2+
3+
fn main() {
4+
let xs = [13, 1, 5, 2, 3, 1, 21, 8];
5+
if let [3..=14, ..] = xs {
6+
/* this variant must pass for now, unfortunately.
7+
* This test is included here to help inform a future plan for these.
8+
*/
9+
};
10+
}

0 commit comments

Comments
 (0)