Skip to content

Commit

Permalink
Reestablish feature gate for RangeFrom in slices
Browse files Browse the repository at this point in the history
  • Loading branch information
workingjubilee committed Jun 4, 2021
1 parent 45d9dd6 commit 43bad44
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 0 deletions.
16 changes: 16 additions & 0 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,22 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {

fn visit_pat(&mut self, pattern: &'a ast::Pat) {
match &pattern.kind {
PatKind::Slice(pats) => {
for pat in pats {
let inner_pat = match &pat.kind {
PatKind::Ident(.., Some(pat)) => pat,
_ => pat,
};
if let PatKind::Range(Some(_), None, Spanned { .. }) = inner_pat.kind {
gate_feature_post!(
&self,
half_open_range_patterns,
pat.span,
"`X..` patterns in slices are experimental"
);
}
}
}
PatKind::Box(..) => {
gate_feature_post!(
&self,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![feature(half_open_range_patterns)]
#![feature(exclusive_range_pattern)]

fn main() {
let xs = [13, 1, 5, 2, 3, 1, 21, 8];
let [a, b, c, rest @ ..] = xs;
// Consider the following example:
assert!(a == 13 && b == 1 && c == 5 && rest.len() == 5);

// What if we wanted to pull this apart without individually binding a, b, and c?
let [first_three @ ..3, rest @ 2..] = xs;
//~^ pattern requires 2 elements but array has 8
// This is somewhat unintuitive and makes slice patterns exceedingly verbose.
// We want to stabilize half-open RangeFrom (`X..`) patterns
// but without banning us from using them for a more efficient slice pattern syntax.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0527]: pattern requires 2 elements but array has 8
--> $DIR/slice_pattern_syntax_problem0.rs:11:9
|
LL | let [first_three @ ..3, rest @ 2..] = xs;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 8 elements

error: aborting due to previous error

For more information about this error, try `rustc --explain E0527`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Instead of allowing the previous case, maintain the feature gate for slice patterns for now.
fn main() {
let xs = [13, 1, 5, 2, 3, 1, 21, 8];
let [a @ 3.., b @ ..3, c @ 4..6, ..] = xs;
//~^ `X..` patterns in slices are experimental
//~| half-open range patterns are unstable
//~| exclusive range pattern syntax is experimental
//~| exclusive range pattern syntax is experimental
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
error[E0658]: half-open range patterns are unstable
--> $DIR/slice_pattern_syntax_problem1.rs:4:23
|
LL | let [a @ 3.., b @ ..3, c @ 4..6, ..] = xs;
| ^^^
|
= note: see issue #67264 <https://github.com/rust-lang/rust/issues/67264> for more information
= help: add `#![feature(half_open_range_patterns)]` to the crate attributes to enable

error[E0658]: `X..` patterns in slices are experimental
--> $DIR/slice_pattern_syntax_problem1.rs:4:10
|
LL | let [a @ 3.., b @ ..3, c @ 4..6, ..] = xs;
| ^^^^^^^
|
= note: see issue #67264 <https://github.com/rust-lang/rust/issues/67264> for more information
= help: add `#![feature(half_open_range_patterns)]` to the crate attributes to enable

error[E0658]: exclusive range pattern syntax is experimental
--> $DIR/slice_pattern_syntax_problem1.rs:4:23
|
LL | let [a @ 3.., b @ ..3, c @ 4..6, ..] = xs;
| ^^^
|
= note: see issue #37854 <https://github.com/rust-lang/rust/issues/37854> for more information
= help: add `#![feature(exclusive_range_pattern)]` to the crate attributes to enable

error[E0658]: exclusive range pattern syntax is experimental
--> $DIR/slice_pattern_syntax_problem1.rs:4:32
|
LL | let [a @ 3.., b @ ..3, c @ 4..6, ..] = xs;
| ^^^^
|
= note: see issue #37854 <https://github.com/rust-lang/rust/issues/37854> for more information
= help: add `#![feature(exclusive_range_pattern)]` to the crate attributes to enable

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0658`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// run-pass

fn main() {
let xs = [13, 1, 5, 2, 3, 1, 21, 8];
if let [3..=14, ..] = xs {
/* this variant must pass for now, unfortunately.
* This test is included here to help inform a future plan for these.
*/
};
}

0 comments on commit 43bad44

Please sign in to comment.