Skip to content

Commit e11f6d5

Browse files
committedOct 7, 2017
Auto merge of #44614 - tschottdorf:pat_adjustments, r=nikomatsakis
implement pattern-binding-modes RFC See the [RFC] and [tracking issue]. [tracking issue]: #42640 [RFC]: https://github.com/rust-lang/rfcs/blob/491e0af/text/2005-match-ergonomics.md
·
1.88.01.22.0
2 parents 05cbece + de55b4f commit e11f6d5

Some content is hidden

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

46 files changed

+1406
-75
lines changed
 
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# `match_default_bindings`
2+
3+
The tracking issue for this feature is: [#42640]
4+
5+
[#42640]: https://github.com/rust-lang/rust/issues/42640
6+
7+
------------------------
8+
9+
Match default bindings (also called "default binding modes in match") improves ergonomics for
10+
pattern-matching on references by introducing automatic dereferencing (and a corresponding shift
11+
in binding modes) for large classes of patterns that would otherwise not compile.
12+
13+
For example, under match default bindings,
14+
15+
```rust
16+
#![feature(match_default_bindings)]
17+
18+
fn main() {
19+
let x: &Option<_> = &Some(0);
20+
21+
match x {
22+
Some(y) => {
23+
println!("y={}", *y);
24+
},
25+
None => {},
26+
}
27+
}
28+
```
29+
30+
compiles and is equivalent to either of the below:
31+
32+
```rust
33+
fn main() {
34+
let x: &Option<_> = &Some(0);
35+
36+
match *x {
37+
Some(ref y) => {
38+
println!("y={}", *y);
39+
},
40+
None => {},
41+
}
42+
}
43+
```
44+
45+
or
46+
47+
```rust
48+
fn main() {
49+
let x: &Option<_> = &Some(0);
50+
51+
match x {
52+
&Some(ref y) => {
53+
println!("y={}", *y);
54+
},
55+
&None => {},
56+
}
57+
}
58+
```

‎src/librustc/hir/pat_util.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,13 @@ impl hir::Pat {
160160
variants
161161
}
162162

163-
/// Checks if the pattern contains any `ref` or `ref mut` bindings,
164-
/// and if yes whether it contains mutable or just immutables ones.
163+
/// Checks if the pattern contains any `ref` or `ref mut` bindings, and if
164+
/// yes whether it contains mutable or just immutables ones.
165165
///
166-
/// FIXME(tschottdorf): this is problematic as the HIR is being scraped,
167-
/// but ref bindings may be implicit after #42640.
166+
/// FIXME(tschottdorf): this is problematic as the HIR is being scraped, but
167+
/// ref bindings are be implicit after #42640 (default match binding modes).
168+
///
169+
/// See #44848.
168170
pub fn contains_explicit_ref_binding(&self) -> Option<hir::Mutability> {
169171
let mut result = None;
170172
self.each_binding(|annotation, _, _, _| {
@@ -188,7 +190,9 @@ impl hir::Arm {
188190
/// bindings, and if yes whether its containing mutable ones or just immutables ones.
189191
pub fn contains_explicit_ref_binding(&self) -> Option<hir::Mutability> {
190192
// FIXME(tschottdorf): contains_explicit_ref_binding() must be removed
191-
// for #42640.
193+
// for #42640 (default match binding modes).
194+
//
195+
// See #44848.
192196
self.pats.iter()
193197
.filter_map(|pat| pat.contains_explicit_ref_binding())
194198
.max_by_key(|m| match *m {

0 commit comments

Comments
 (0)
Please sign in to comment.