1 // Copyright 2017 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.
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.
11 #![feature(match_default_bindings)]
13 fn some_or_wildcard(r: &Option<i32>, b: &i32) {
14 let _: &i32 = match r {
20 fn none_or_wildcard(r: &Option<i32>, b: &i32) {
21 let _: &i32 = match r {
27 fn some_or_ref_none(r: &Option<i32>, b: &i32) {
28 let _: &i32 = match r {
34 fn ref_some_or_none(r: &Option<i32>, b: &i32) {
35 let _: &i32 = match r {
41 fn some_or_self(r: &Option<i32>) {
42 let _: &Option<i32> = match r {
51 fn multiple_deref(r: &&&&&Option<i32>) {
52 let _: i32 = match r {
59 // FIXME(tschottdorf): #44912.
61 // let x = &Some((3, 3));
62 // let _: &i32 = match x {
63 // Some((x, 3)) | &Some((ref x, 5)) => x,
69 match (&Some(5), &Some(6)) {
70 (Some(a), &Some(mut b)) => {
71 // Here, the `a` will be `&i32`, because in the first half of the tuple
72 // we hit a non-reference pattern and shift into `ref` mode.
74 // In the second half of the tuple there's no non-reference pattern,
75 // so `b` will be `i32` (bound with `move` mode). Moreover, `b` is
85 fn nested_mixed_multiple_deref_1() {
86 let x = (1, &Some(5));
89 Some((a, Some(b))) => {
97 fn nested_mixed_multiple_deref_2() {
108 fn new_mutable_reference() {
109 let mut x = &mut Some(5);
125 fn let_implicit_ref_binding() {
128 // Note that these rules apply to any pattern matching
129 // whether it be in a `match` or a `let`.
130 // For example, `x` here is a `ref` binding:
131 let Foo(x) = &Foo(3);
135 fn explicit_mut_binding() {
144 match &mut Some(5i32) {
152 match &mut &mut Some(5i32) {
160 fn tuple_mut_and_mut_mut() {
161 match (Some(5i32), &Some(5i32)) {
162 (Some(n), Some(m)) => {
163 // `n` and `m` are bound as immutable references. Make new references from them to
170 // Assert the types. Note that we use `n` and `m` here which would fail had they been
171 // moved due to the assignments above.
178 match (&Some(5i32), &&Some(5i32)) {
179 (Some(n), Some(m)) => {
186 match &mut &mut (Some(5i32), Some(5i32)) {
187 (Some(n), Some(m)) => {
188 // Dereferenced through &mut &mut, so a mutable binding results.
195 match (&mut Some(5i32), &mut &mut Some(5i32)) {
196 (Some(n), Some(m)) => {
204 fn min_mir_embedded_type() {
205 // The reduced invocation that an ICE was diagnosed with (was consuming
206 // adjustments in wrong order).
207 match (0u8, &&Some(5i32)) {
223 // Constant of generic type (string)
224 const Y: &'static str = "foo";
225 assert_eq!(0, match "foo" {
230 // Reference pattern.
235 let r: &Option<i32> = &Some(3);
238 none_or_wildcard(r, b);
239 some_or_wildcard(r, b);
240 some_or_ref_none(r, b);
241 ref_some_or_none(r, b);
244 multiple_deref(&&&&r);
248 nested_mixed_multiple_deref_1();
249 nested_mixed_multiple_deref_2();
251 new_mutable_reference();
252 explicit_mut_binding();
253 tuple_mut_and_mut_mut();
254 min_mir_embedded_type();
256 let_implicit_ref_binding();