Skip to content

Commit b645f14

Browse files
authored
Merge pull request #2707 from petrochenkov/ddpat
Mini-RFC: Make `..` a pattern syntactically
2 parents 14394d7 + 52bb5b6 commit b645f14

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

text/2707-dotdot-patterns.md

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
- Feature Name: `dotdot_patterns`
2+
- Start Date: 2019-06-01
3+
- RFC PR: [rust-lang/rfcs#2707](https://github.com/rust-lang/rfcs/pull/2707)
4+
- Rust Issue: [rust-lang/rust#62254](https://github.com/rust-lang/rust/issues/62254)
5+
6+
# Summary
7+
[summary]: #summary
8+
9+
Make `..` a pattern rather than a syntactic fragment of some other patterns.
10+
11+
# Motivation
12+
[motivation]: #motivation
13+
14+
The change simplifies pattern grammar and simplifies use of `..` in macros.
15+
In particular, the `pat` macro matcher will now accept `..` and `IDENT @ ..`.
16+
17+
# Guide-level explanation
18+
[guide-level-explanation]: #guide-level-explanation
19+
20+
`..` becomes a pattern syntacticaly.
21+
The notable consequences of this are listed below.
22+
23+
- `pat` macro matcher will now accept `..` and more complex pattern containing `..`,
24+
for example `ref x @ ..`.
25+
26+
- A trailing comma is accepted after `..` in tuple struct, tuple or slice pattern.
27+
```rust
28+
Variant(a, b, ..,) // OK
29+
```
30+
31+
- Some nonsensical code can now be accepted under `cfg(FALSE)`.
32+
```rust
33+
#[cfg(FALSE)]
34+
Tuple(.., a, ..) // OK
35+
```
36+
37+
`..` in "inappropriate" positions is still rejected semantically.
38+
```rust
39+
let .. = 10; // Semantic error, `..` is not a part of a "list" pattern
40+
let Option(.., ..) = 11; // Semantic error, multiple `..`s in a single "list" pattern
41+
```
42+
43+
# Reference-level explanation
44+
[reference-level-explanation]: #reference-level-explanation
45+
46+
Pattern grammar is extended with a new production
47+
```
48+
PAT = ..
49+
```
50+
Special productions allowing `..` in tuple struct, tuple and slice patterns are subsumed by this
51+
new production and removed.
52+
53+
Semantically, the `..` pattern is accepted
54+
- Immediately inside a tuple struct/variant pattern `Tuple(PAT, .., PAT)`
55+
- Immediately inside a tuple pattern `(PAT, .., PAT)`
56+
- Immediately inside a slice pattern `[PAT, .., PAT]`.
57+
- Immediately inside a binding pattern inside a slice pattern `[PAT, BINDING @ .., PAT]`.
58+
59+
An error is produced if this pattern is used in any other position.
60+
61+
An error is produced if more that one `..` or `BINDING @ ..` pattern is used inside its containing
62+
tuple struct / tuple / slice pattern.
63+
64+
`(..)` is still a tuple pattern and not a parenthesized `..` pattern for backward compatibility.
65+
66+
Note that `..` in struct patterns
67+
```rust
68+
Struct { field1: PAT, field2, .. }
69+
```
70+
is still not a pattern, but a fragment of a struct pattern syntax.
71+
72+
# Drawbacks
73+
[drawbacks]: #drawbacks
74+
75+
More meaningless code may be accepted under `cfg(FALSE)` where semantic checks are not performed.
76+
77+
# Rationale and alternatives
78+
[rationale-and-alternatives]: #rationale-and-alternatives
79+
80+
See "Motivation" for the rationale.
81+
Status quo is always an alternative.
82+
83+
# Prior art
84+
[prior-art]: #prior-art
85+
86+
This RFC is a follow up to https://github.com/rust-lang/rfcs/pull/2359.
87+
88+
# Unresolved questions
89+
[unresolved-questions]: #unresolved-questions
90+
91+
None so far.
92+
93+
# Future possibilities
94+
[future-possibilities]: #future-possibilities
95+
96+
Accept `BINDING @ ..` in tuple patterns, `(head, tail @ ..)`.

0 commit comments

Comments
 (0)