]> git.lizzy.rs Git - rust.git/blob - tests/ui/rfcs/rfc-2005-default-binding-mode/general.rs
Rollup merge of #106570 - Xaeroxe:div-duration-tests, r=JohnTitor
[rust.git] / tests / ui / rfcs / rfc-2005-default-binding-mode / general.rs
1 // run-pass
2 #![allow(unused_variables)]
3 fn some_or_wildcard(r: &Option<i32>, b: &i32) {
4     let _: &i32 = match r {
5         Some(a) => a,
6         _ => b,
7     };
8 }
9
10 fn none_or_wildcard(r: &Option<i32>, b: &i32) {
11     let _: &i32 = match r {
12         None => b,
13         _ => b,
14     };
15 }
16
17 fn some_or_ref_none(r: &Option<i32>, b: &i32) {
18     let _: &i32 = match r {
19         Some(a) => a,
20         &None => b,
21     };
22 }
23
24 fn ref_some_or_none(r: &Option<i32>, b: &i32) {
25     let _: &i32 = match r {
26         &Some(ref a) => a,
27         None => b,
28     };
29 }
30
31 fn some_or_self(r: &Option<i32>) {
32     let _: &Option<i32> = match r {
33         Some(n) => {
34             let _: &i32 = n;
35             r
36         },
37         x => x,
38     };
39 }
40
41 fn multiple_deref(r: &&&&&Option<i32>) {
42     let _: i32 = match r {
43         Some(a) => *a,
44         None => 5,
45     };
46 }
47
48 fn match_with_or() {
49     // FIXME(tschottdorf): #44912.
50     //
51     // let x = &Some((3, 3));
52     // let _: &i32 = match x {
53     //     Some((x, 3)) | &Some((ref x, 5)) => x,
54     //     _ => &5i32,
55     // };
56 }
57
58 fn nested_mixed() {
59     match (&Some(5), &Some(6)) {
60         (Some(a), &Some(mut b)) => {
61             // Here, the `a` will be `&i32`, because in the first half of the tuple
62             // we hit a non-reference pattern and shift into `ref` mode.
63             //
64             // In the second half of the tuple there's no non-reference pattern,
65             // so `b` will be `i32` (bound with `move` mode). Moreover, `b` is
66             // mutable.
67             let _: &i32 = a;
68             b = 7;
69             let _: i32 = b;
70         },
71         _ => {},
72     };
73 }
74
75 fn nested_mixed_multiple_deref_1() {
76     let x = (1, &Some(5));
77     let y = &Some(x);
78     match y {
79         Some((a, Some(b))) => {
80             let _: &i32 = a;
81             let _: &i32 = b;
82         },
83         _ => {},
84     };
85 }
86
87 fn nested_mixed_multiple_deref_2() {
88     let x = &Some(5);
89     let y = &x;
90     match y {
91         Some(z) => {
92             let _: &i32 = z;
93         },
94         _ => {},
95     }
96 }
97
98 fn new_mutable_reference() {
99     let mut x = &mut Some(5);
100     match &mut x {
101         Some(y) => {
102             *y = 5;
103         },
104         None => { },
105     }
106
107     match &mut x {
108         Some(y) => {
109             println!("{}", *y);
110         },
111         None => {},
112     }
113 }
114
115 fn let_implicit_ref_binding() {
116     struct Foo(i32);
117
118     // Note that these rules apply to any pattern matching
119     // whether it be in a `match` or a `let`.
120     // For example, `x` here is a `ref` binding:
121     let Foo(x) = &Foo(3);
122     let _: &i32 = x;
123 }
124
125 fn explicit_mut_binding() {
126     match &Some(5i32) {
127         Some(mut n) => {
128             n += 1;
129             let _ = n;
130         }
131         None => {},
132     };
133
134     match &mut Some(5i32) {
135         Some(n) => {
136             *n += 1;
137             let _ = n;
138         }
139         None => {},
140     };
141
142     match &mut &mut Some(5i32) {
143         Some(n) => {
144              let _: &mut i32 = n;
145         }
146         None => {},
147     };
148 }
149
150 fn tuple_mut_and_mut_mut() {
151     match (Some(5i32), &Some(5i32)) {
152         (Some(n), Some(m)) => {
153             // `n` and `m` are bound as immutable references. Make new references from them to
154             // assert that.
155             let r = n;
156             let _ = r;
157             let q = m;
158             let _ = q;
159
160             // Assert the types. Note that we use `n` and `m` here which would fail had they been
161             // moved due to the assignments above.
162             let _: i32 = n;
163             let _: &i32 = m;
164         }
165         (_, _) => {},
166     };
167
168     match (&Some(5i32), &&Some(5i32)) {
169         (Some(n), Some(m)) => {
170             let _: &i32 = n;
171             let _: &i32 = m;
172         }
173         (_, _) => {},
174     };
175
176     match &mut &mut (Some(5i32), Some(5i32)) {
177         (Some(n), Some(m)) => {
178             // Dereferenced through &mut &mut, so a mutable binding results.
179             let _: &mut i32 = n;
180             let _: &mut i32 = m;
181         }
182         (_, _) => {},
183     };
184
185     match (&mut Some(5i32), &mut &mut Some(5i32)) {
186         (Some(n), Some(m)) => {
187             let _: &mut i32 = n;
188             let _: &mut i32 = m;
189         }
190         (_, _) => {},
191     };
192 }
193
194 fn min_mir_embedded_type() {
195     // The reduced invocation that an ICE was diagnosed with (was consuming
196     // adjustments in wrong order).
197     match (0u8, &&Some(5i32)) {
198         (_, Some(m)) => {
199             let _: &i32 = m;
200         }
201         (_, _) => {},
202     };
203 }
204
205 fn no_autoderef() {
206     // Binding.
207     let x = &3;
208     println!("{}", *x);
209
210     // Wildcard.
211     let _ = &3;
212
213     // Constant of generic type (string)
214     const Y: &'static str = "foo";
215     assert_eq!(0, match "foo" {
216         Y => 0,
217         _ => 1,
218     });
219
220     // Reference pattern.
221     let &x = &3;
222 }
223
224 pub fn main() {
225     let r: &Option<i32> = &Some(3);
226     let b = &4i32;
227
228     none_or_wildcard(r, b);
229     some_or_wildcard(r, b);
230     some_or_ref_none(r, b);
231     ref_some_or_none(r, b);
232
233     some_or_self(r);
234     multiple_deref(&&&&r);
235     match_with_or();
236
237     nested_mixed();
238     nested_mixed_multiple_deref_1();
239     nested_mixed_multiple_deref_2();
240
241     new_mutable_reference();
242     explicit_mut_binding();
243     tuple_mut_and_mut_mut();
244     min_mir_embedded_type();
245
246     let_implicit_ref_binding();
247
248     no_autoderef();
249 }