1 use expect_test::expect;
4 use super::{check_infer, check_infer_with_mismatches};
14 let (c, d) = (1, "hello");
16 for (e, f) in some_iter {
24 let lambda = |a: u64, b, c: i32| { a + b; c };
28 let ref mut mut_ref_to_x = x;
34 17..368 '{ ...o_x; }': ()
42 73..79 '(c, d)': (i32, &str)
45 82..94 '(1, "hello")': (i32, &str)
47 86..93 '"hello"': &str
48 101..151 'for (e... }': ()
49 105..111 '(e, f)': ({unknown}, {unknown})
50 106..107 'e': {unknown}
51 109..110 'f': {unknown}
52 115..124 'some_iter': {unknown}
53 125..151 '{ ... }': ()
54 139..140 'g': {unknown}
55 143..144 'e': {unknown}
56 157..204 'if let... }': ()
57 164..169 '[val]': [{unknown}]
58 165..168 'val': {unknown}
59 172..175 'opt': [{unknown}]
60 176..204 '{ ... }': ()
61 190..191 'h': {unknown}
62 194..197 'val': {unknown}
63 214..220 'lambda': |u64, u64, i32| -> i32
64 223..255 '|a: u6...b; c }': |u64, u64, i32| -> i32
68 243..255 '{ a + b; c }': i32
73 266..278 'ref ref_to_x': &&i32
75 292..301 'mut mut_x': &i32
77 315..335 'ref mu...f_to_x': &mut &i32
79 349..350 'k': &mut &i32
80 353..365 'mut_ref_to_x': &mut &i32
86 fn infer_literal_pattern() {
87 check_infer_with_mismatches(
89 fn any<T>() -> T { loop {} }
91 if let "foo" = any() {}
93 if let 1u32 = any() {}
94 if let 1f32 = any() {}
96 if let true = any() {}
100 17..28 '{ loop {} }': T
104 46..208 '{ ...) {} }': ()
105 52..75 'if let...y() {}': ()
108 67..70 'any': fn any<&str>() -> &str
111 80..99 'if let...y() {}': ()
114 91..94 'any': fn any<i32>() -> i32
117 104..126 'if let...y() {}': ()
120 118..121 'any': fn any<u32>() -> u32
121 118..123 'any()': u32
123 131..153 'if let...y() {}': ()
126 145..148 'any': fn any<f32>() -> f32
127 145..150 'any()': f32
129 158..179 'if let...y() {}': ()
132 171..174 'any': fn any<f64>() -> f64
133 171..176 'any()': f64
135 184..206 'if let...y() {}': ()
136 191..195 'true': bool
137 191..195 'true': bool
138 198..201 'any': fn any<bool>() -> bool
139 198..203 'any()': bool
146 fn infer_range_pattern() {
147 check_infer_with_mismatches(
150 if let 1..76 = 2u32 {}
151 if let 1..=76 = 2u32 {}
156 17..75 '{ ...2 {} }': ()
157 23..45 'if let...u32 {}': ()
161 50..73 'if let...u32 {}': ()
170 fn infer_pattern_match_ergonomics() {
177 let A(n) = &mut A(1);
181 27..78 '{ ...(1); }': ()
182 37..41 'A(n)': A<i32>
184 44..49 '&A(1)': &A<i32>
185 45..46 'A': A<i32>(i32) -> A<i32>
186 45..49 'A(1)': A<i32>
188 59..63 'A(n)': A<i32>
190 66..75 '&mut A(1)': &mut A<i32>
191 71..72 'A': A<i32>(i32) -> A<i32>
192 71..75 'A(1)': A<i32>
199 fn infer_pattern_match_ergonomics_ref() {
200 mark::check!(match_ergonomics_ref);
209 10..56 '{ ...= v; }': ()
210 20..21 'v': &(i32, &i32)
211 24..32 '&(1, &2)': &(i32, &i32)
212 25..32 '(1, &2)': (i32, &i32)
216 42..49 '(_, &w)': (i32, &i32)
220 52..53 'v': &(i32, &i32)
226 fn infer_pattern_match_slice() {
230 let slice: &[f64] = &[0.0];
245 10..209 '{ ... } }': ()
246 20..25 'slice': &[f64]
247 36..42 '&[0.0]': &[f64; _]
248 37..42 '[0.0]': [f64; _]
250 48..207 'match ... }': ()
251 54..59 'slice': &[f64]
255 89..93 '&[a]': &[f64]
258 97..123 '{ ... }': ()
260 133..140 '&[b, c]': &[f64]
261 134..140 '[b, c]': [f64]
264 144..185 '{ ... }': ()
274 fn infer_pattern_match_string_literal() {
275 check_infer_with_mismatches(
278 let s: &str = "hello";
286 10..98 '{ ... } }': ()
288 30..37 '"hello"': &str
289 43..96 'match ... }': ()
291 61..68 '"hello"': &str
292 61..68 '"hello"': &str
301 fn infer_pattern_match_or() {
302 check_infer_with_mismatches(
305 let s: &str = "hello";
307 "hello" | "world" => {}
313 10..108 '{ ... } }': ()
315 30..37 '"hello"': &str
316 43..106 'match ... }': ()
318 61..68 '"hello"': &str
319 61..68 '"hello"': &str
320 61..78 '"hello...world"': &str
321 71..78 '"world"': &str
322 71..78 '"world"': &str
331 fn infer_pattern_match_arr() {
335 let arr: [f64; 2] = [0.0, 1.0];
348 10..179 '{ ... } }': ()
349 20..23 'arr': [f64; _]
350 36..46 '[0.0, 1.0]': [f64; _]
353 52..177 'match ... }': ()
354 58..61 'arr': [f64; _]
355 72..80 '[1.0, a]': [f64; _]
359 84..110 '{ ... }': ()
361 120..126 '[b, c]': [f64; _]
364 130..171 '{ ... }': ()
372 fn infer_adt_pattern() {
383 let e = E::A { x: 3 };
386 let E::A { x: new_var } = e;
394 let ref d @ E::A { .. } = e;
399 67..288 '{ ... d; }': ()
401 81..94 'E::A { x: 3 }': E
403 105..112 'S(y, z)': S
407 128..147 'E::A {..._var }': E
408 138..145 'new_var': usize
410 158..244 'match ... }': usize
412 176..186 'E::A { x }': E
420 255..274 'ref d ...{ .. }': &E
421 263..274 'E::A { .. }': E
429 fn enum_variant_through_self_in_pattern() {
441 Self::A { x } => { x; },
442 Self::B(x) => { x; },
449 75..217 '{ ... }': ()
450 85..210 'match ... }': ()
453 115..128 'Self::A { x }': E
455 132..138 '{ x; }': ()
457 152..162 'Self::B(x)': E
459 166..172 '{ x; }': ()
461 186..193 'Self::C': E
468 fn infer_generics_in_patterns() {
480 fn test(a1: A<u32>, o: Option<u64>) {
481 let A { x: x2 } = a1;
482 let A::<i64> { x: x3 } = A { x: 1 };
484 Option::Some(t) => t,
491 90..91 'o': Option<u64>
492 106..243 '{ ... }; }': ()
493 116..127 'A { x: x2 }': A<u32>
495 130..132 'a1': A<u32>
496 142..160 'A::<i6...: x3 }': A<i64>
498 163..173 'A { x: 1 }': A<i64>
500 179..240 'match ... }': u64
501 185..186 'o': Option<u64>
502 197..212 'Option::Some(t)': Option<u64>
505 227..228 '_': Option<u64>
512 fn infer_const_pattern() {
513 check_infer_with_mismatches(
515 enum Option<T> { None }
518 const Bar: usize = 1;
521 let a: Option<u32> = None;
522 let b: Option<i64> = match a {
525 let _: () = match () { Foo => Foo }; // Expected mismatch
526 let _: () = match () { Bar => Bar }; // Expected mismatch
531 87..309 '{ ...atch }': ()
532 97..98 'a': Option<u32>
533 114..118 'None': Option<u32>
534 128..129 'b': Option<i64>
535 145..182 'match ... }': Option<i64>
536 151..152 'a': Option<u32>
537 163..167 'None': Option<u32>
538 171..175 'None': Option<i64>
540 200..223 'match ... Foo }': Foo
545 262..285 'match ... Bar }': usize
547 273..276 'Bar': usize
548 280..283 'Bar': usize
549 200..223: expected (), got Foo
550 262..285: expected (), got usize
560 impl S { fn foo(&self) -> bool { false } }
570 41..50 '{ false }': bool
572 64..115 '{ ... } }': ()
573 70..113 'match ... }': ()
577 93..100 's.foo()': bool
584 fn match_ergonomics_in_closure_params() {
592 fn foo<T, U, F: FnOnce(T) -> U>(t: T, f: F) -> U { loop {} }
595 foo(&(1, "a"), |&(x, y)| x); // normal, no match ergonomics
596 foo(&(1, "a"), |(x, y)| x);
602 110..121 '{ loop {} }': U
603 112..119 'loop {}': !
605 133..232 '{ ... x); }': ()
606 139..142 'foo': fn foo<&(i32, &str), i32, |&(i32, &str)| -> i32>(&(i32, &str), |&(i32, &str)| -> i32) -> i32
607 139..166 'foo(&(...y)| x)': i32
608 143..152 '&(1, "a")': &(i32, &str)
609 144..152 '(1, "a")': (i32, &str)
612 154..165 '|&(x, y)| x': |&(i32, &str)| -> i32
613 155..162 '&(x, y)': &(i32, &str)
614 156..162 '(x, y)': (i32, &str)
618 203..206 'foo': fn foo<&(i32, &str), &i32, |&(i32, &str)| -> &i32>(&(i32, &str), |&(i32, &str)| -> &i32) -> &i32
619 203..229 'foo(&(...y)| x)': &i32
620 207..216 '&(1, "a")': &(i32, &str)
621 208..216 '(1, "a")': (i32, &str)
624 218..228 '|(x, y)| x': |&(i32, &str)| -> &i32
625 219..225 '(x, y)': (i32, &str)
634 fn slice_tail_pattern() {
637 fn foo(params: &[i32]) {
639 [head, tail @ ..] => {
645 7..13 'params': &[i32]
646 23..92 '{ ... } }': ()
647 29..90 'match ... }': ()
648 35..41 'params': &[i32]
649 52..69 '[head,... @ ..]': [i32]
651 59..68 'tail @ ..': &[i32]
662 #[lang = "owned_box"]
663 pub struct Box<T>(T);
665 fn foo(params: Box<i32>) {
672 52..58 'params': Box<i32>
673 70..124 '{ ... } }': ()
674 76..122 'match ... }': ()
675 82..88 'params': Box<i32>
676 99..110 'box integer': Box<i32>
677 103..110 'integer': i32