Skip to content

Commit af5d4b8

Browse files
committed
Address review comments
1 parent d912f90 commit af5d4b8

File tree

12 files changed

+355
-211
lines changed

12 files changed

+355
-211
lines changed

src/librustc/hir/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ pub enum PatKind {
539539
Struct(Path, HirVec<Spanned<FieldPat>>, bool),
540540

541541
/// A tuple struct/variant pattern `Variant(x, y, .., z)`.
542-
/// If the `..` pattern fragment presents, then `Option<usize>` denotes its position.
542+
/// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
543543
/// 0 <= position <= subpats.len()
544544
TupleStruct(Path, HirVec<P<Pat>>, Option<usize>),
545545

@@ -554,7 +554,7 @@ pub enum PatKind {
554554
QPath(QSelf, Path),
555555

556556
/// A tuple pattern `(a, b)`.
557-
/// If the `..` pattern fragment presents, then `Option<usize>` denotes its position.
557+
/// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
558558
/// 0 <= position <= subpats.len()
559559
Tuple(HirVec<P<Pat>>, Option<usize>),
560560
/// A `box` pattern

src/librustc/hir/pat_util.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ use std::cell::RefCell;
2222
pub type PatIdMap = FnvHashMap<ast::Name, ast::NodeId>;
2323

2424
#[derive(Clone, Copy)]
25-
pub struct AjustPos {
25+
pub struct AdjustPos {
2626
gap_pos: usize,
2727
gap_len: usize,
2828
}
2929

30-
impl FnOnce<(usize,)> for AjustPos {
30+
impl FnOnce<(usize,)> for AdjustPos {
3131
type Output = usize;
3232
extern "rust-call" fn call_once(self, (i,): (usize,)) -> usize {
3333
if i < self.gap_pos { i } else { i + self.gap_len }
@@ -36,8 +36,8 @@ impl FnOnce<(usize,)> for AjustPos {
3636

3737
// Returns a functional object used to adjust tuple pattern indexes. Example: for 5-tuple and
3838
// pattern (a, b, .., c) expected_len is 5, actual_len is 3 and gap_pos is Some(2).
39-
pub fn pat_adjust_pos(expected_len: usize, actual_len: usize, gap_pos: Option<usize>) -> AjustPos {
40-
AjustPos {
39+
pub fn pat_adjust_pos(expected_len: usize, actual_len: usize, gap_pos: Option<usize>) -> AdjustPos {
40+
AdjustPos {
4141
gap_pos: if let Some(gap_pos) = gap_pos { gap_pos } else { expected_len },
4242
gap_len: expected_len - actual_len,
4343
}

src/libsyntax/ast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ pub enum PatKind {
631631
Struct(Path, Vec<Spanned<FieldPat>>, bool),
632632

633633
/// A tuple struct/variant pattern `Variant(x, y, .., z)`.
634-
/// If the `..` pattern fragment presents, then `Option<usize>` denotes its position.
634+
/// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
635635
/// 0 <= position <= subpats.len()
636636
TupleStruct(Path, Vec<P<Pat>>, Option<usize>),
637637

@@ -646,7 +646,7 @@ pub enum PatKind {
646646
QPath(QSelf, Path),
647647

648648
/// A tuple pattern `(a, b)`.
649-
/// If the `..` pattern fragment presents, then `Option<usize>` denotes its position.
649+
/// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
650650
/// 0 <= position <= subpats.len()
651651
Tuple(Vec<P<Pat>>, Option<usize>),
652652
/// A `box` pattern

src/libsyntax/parse/parser.rs

+4
Original file line numberDiff line numberDiff line change
@@ -3426,6 +3426,10 @@ impl<'a> Parser<'a> {
34263426
// `..` needs to be followed by `)` or `, pat`, `..,)` is disallowed.
34273427
fields.push(self.parse_pat()?);
34283428
}
3429+
} else if ddpos.is_some() && self.eat(&token::DotDot) {
3430+
// Emit a friendly error, ignore `..` and continue parsing
3431+
self.span_err(self.last_span, "`..` can only be used once per \
3432+
tuple or tuple struct pattern");
34293433
} else {
34303434
fields.push(self.parse_pat()?);
34313435
}

src/test/parse-fail/pat-tuple-3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212

1313
fn main() {
1414
match 0 {
15-
(.., pat, ..) => {} //~ ERROR expected pattern, found `..`
15+
(.., pat, ..) => {} //~ ERROR `..` can only be used once per tuple or tuple struct pattern
1616
}
1717
}

src/test/run-pass/pat-tuple-1.rs

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(dotdot_in_tuple_patterns)]
12+
13+
fn tuple() {
14+
let x = (1, 2, 3);
15+
match x {
16+
(a, b, ..) => {
17+
assert_eq!(a, 1);
18+
assert_eq!(b, 2);
19+
}
20+
}
21+
match x {
22+
(.., b, c) => {
23+
assert_eq!(b, 2);
24+
assert_eq!(c, 3);
25+
}
26+
}
27+
match x {
28+
(a, .., c) => {
29+
assert_eq!(a, 1);
30+
assert_eq!(c, 3);
31+
}
32+
}
33+
match x {
34+
(a, b, c) => {
35+
assert_eq!(a, 1);
36+
assert_eq!(b, 2);
37+
assert_eq!(c, 3);
38+
}
39+
}
40+
match x {
41+
(a, b, c, ..) => {
42+
assert_eq!(a, 1);
43+
assert_eq!(b, 2);
44+
assert_eq!(c, 3);
45+
}
46+
}
47+
match x {
48+
(.., a, b, c) => {
49+
assert_eq!(a, 1);
50+
assert_eq!(b, 2);
51+
assert_eq!(c, 3);
52+
}
53+
}
54+
}
55+
56+
fn tuple_struct() {
57+
struct S(u8, u8, u8);
58+
59+
let x = S(1, 2, 3);
60+
match x {
61+
S(a, b, ..) => {
62+
assert_eq!(a, 1);
63+
assert_eq!(b, 2);
64+
}
65+
}
66+
match x {
67+
S(.., b, c) => {
68+
assert_eq!(b, 2);
69+
assert_eq!(c, 3);
70+
}
71+
}
72+
match x {
73+
S(a, .., c) => {
74+
assert_eq!(a, 1);
75+
assert_eq!(c, 3);
76+
}
77+
}
78+
match x {
79+
S(a, b, c) => {
80+
assert_eq!(a, 1);
81+
assert_eq!(b, 2);
82+
assert_eq!(c, 3);
83+
}
84+
}
85+
match x {
86+
S(a, b, c, ..) => {
87+
assert_eq!(a, 1);
88+
assert_eq!(b, 2);
89+
assert_eq!(c, 3);
90+
}
91+
}
92+
match x {
93+
S(.., a, b, c) => {
94+
assert_eq!(a, 1);
95+
assert_eq!(b, 2);
96+
assert_eq!(c, 3);
97+
}
98+
}
99+
}
100+
101+
fn main() {
102+
tuple();
103+
tuple_struct();
104+
}

src/test/run-pass/pat-tuple-2.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(dotdot_in_tuple_patterns)]
12+
13+
fn tuple() {
14+
let x = (1,);
15+
match x {
16+
(2, ..) => panic!(),
17+
(..) => ()
18+
}
19+
}
20+
21+
fn tuple_struct() {
22+
struct S(u8);
23+
24+
let x = S(1);
25+
match x {
26+
S(2, ..) => panic!(),
27+
S(..) => ()
28+
}
29+
}
30+
31+
fn main() {
32+
tuple();
33+
tuple_struct();
34+
}

src/test/run-pass/pat-tuple-3.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(dotdot_in_tuple_patterns)]
12+
13+
fn tuple() {
14+
let x = (1, 2, 3);
15+
let branch = match x {
16+
(1, 1, ..) => 0,
17+
(1, 2, 3, ..) => 1,
18+
(1, 2, ..) => 2,
19+
_ => 3
20+
};
21+
assert_eq!(branch, 1);
22+
}
23+
24+
fn tuple_struct() {
25+
struct S(u8, u8, u8);
26+
27+
let x = S(1, 2, 3);
28+
let branch = match x {
29+
S(1, 1, ..) => 0,
30+
S(1, 2, 3, ..) => 1,
31+
S(1, 2, ..) => 2,
32+
_ => 3
33+
};
34+
assert_eq!(branch, 1);
35+
}
36+
37+
fn main() {
38+
tuple();
39+
tuple_struct();
40+
}

src/test/run-pass/pat-tuple-4.rs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(dotdot_in_tuple_patterns)]
12+
13+
fn tuple() {
14+
let x = (1, 2, 3);
15+
match x {
16+
(1, 2, 4) => unreachable!(),
17+
(0, 2, 3, ..) => unreachable!(),
18+
(0, .., 3) => unreachable!(),
19+
(0, ..) => unreachable!(),
20+
(1, 2, 3) => (),
21+
(_, _, _) => unreachable!(),
22+
}
23+
match x {
24+
(..) => (),
25+
}
26+
match x {
27+
(_, _, _, ..) => (),
28+
}
29+
match x {
30+
(a, b, c) => {
31+
assert_eq!(1, a);
32+
assert_eq!(2, b);
33+
assert_eq!(3, c);
34+
}
35+
}
36+
}
37+
38+
fn tuple_struct() {
39+
struct S(u8, u8, u8);
40+
41+
let x = S(1, 2, 3);
42+
match x {
43+
S(1, 2, 4) => unreachable!(),
44+
S(0, 2, 3, ..) => unreachable!(),
45+
S(0, .., 3) => unreachable!(),
46+
S(0, ..) => unreachable!(),
47+
S(1, 2, 3) => (),
48+
S(_, _, _) => unreachable!(),
49+
}
50+
match x {
51+
S(..) => (),
52+
}
53+
match x {
54+
S(_, _, _, ..) => (),
55+
}
56+
match x {
57+
S(a, b, c) => {
58+
assert_eq!(1, a);
59+
assert_eq!(2, b);
60+
assert_eq!(3, c);
61+
}
62+
}
63+
}
64+
65+
fn main() {
66+
tuple();
67+
tuple_struct();
68+
}

src/test/run-pass/pat-tuple-5.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(dotdot_in_tuple_patterns)]
12+
13+
fn tuple() {
14+
struct S;
15+
struct Z;
16+
struct W;
17+
let x = (S, Z, W);
18+
match x { (S, ..) => {} }
19+
match x { (.., W) => {} }
20+
match x { (S, .., W) => {} }
21+
match x { (.., Z, _) => {} }
22+
}
23+
24+
fn tuple_struct() {
25+
struct SS(S, Z, W);
26+
27+
struct S;
28+
struct Z;
29+
struct W;
30+
let x = SS(S, Z, W);
31+
match x { SS(S, ..) => {} }
32+
match x { SS(.., W) => {} }
33+
match x { SS(S, .., W) => {} }
34+
match x { SS(.., Z, _) => {} }
35+
}
36+
37+
fn main() {
38+
tuple();
39+
tuple_struct();
40+
}

0 commit comments

Comments
 (0)