1 use expect_test::expect;
3 use super::{check_infer, check_infer_with_mismatches, check_no_mismatches, check_types};
6 fn infer_block_expr_type_mismatch() {
10 let a: i32 = { 1i64 };
14 10..40 '{ ...4 }; }': ()
16 29..37 '{ 1i64 }': i64
26 //- minicore: coerce_unsized
29 fn f<T>(_: &[T]) -> T { loop {} }
30 fn g<T>(_: S<&[T]>) -> T { loop {} }
32 fn gen<T>() -> *mut [T; 2] { loop {} }
33 fn test1<U>() -> *mut [U] {
38 let arr: &[u8; 1] = &[1];
42 let c: &[_] = { arr };
43 let d = g(S { a: arr });
44 let e: [&[_]; 1] = [arr];
45 let f: [&[_]; 2] = [arr; 2];
46 let g: (&[_], &[_]) = (arr, arr);
51 44..55 '{ loop {} }': T
55 81..92 '{ loop {} }': T
58 121..132 '{ loop {} }': *mut [T; 2]
61 159..172 '{ gen() }': *mut [U]
62 165..168 'gen': fn gen<U>() -> *mut [U; 2]
63 165..170 'gen()': *mut [U; 2]
64 185..419 '{ ...rr); }': ()
65 195..198 'arr': &[u8; 1]
66 211..215 '&[1]': &[u8; 1]
67 212..215 '[1]': [u8; 1]
70 236..239 'arr': &[u8; 1]
72 253..254 'f': fn f<u8>(&[u8]) -> u8
74 255..258 'arr': &[u8; 1]
76 279..286 '{ arr }': &[u8]
77 281..284 'arr': &[u8; 1]
79 300..301 'g': fn g<u8>(S<&[u8]>) -> u8
80 300..315 'g(S { a: arr })': u8
81 302..314 'S { a: arr }': S<&[u8]>
82 309..312 'arr': &[u8; 1]
83 325..326 'e': [&[u8]; 1]
84 340..345 '[arr]': [&[u8]; 1]
85 341..344 'arr': &[u8; 1]
86 355..356 'f': [&[u8]; 2]
87 370..378 '[arr; 2]': [&[u8]; 2]
88 371..374 'arr': &[u8; 1]
90 388..389 'g': (&[u8], &[u8])
91 406..416 '(arr, arr)': (&[u8], &[u8])
92 407..410 'arr': &[u8; 1]
93 412..415 'arr': &[u8; 1]
99 fn infer_let_stmt_coerce() {
103 let x: &[isize] = &[1];
104 let x: *const [isize] = &[1];
108 10..75 '{ ...[1]; }': ()
110 34..38 '&[1]': &[isize; 1]
111 35..38 '[1]': [isize; 1]
113 48..49 'x': *const [isize]
114 68..72 '&[1]': &[isize; 1]
115 69..72 '[1]': [isize; 1]
122 fn infer_custom_coerce_unsized() {
125 //- minicore: coerce_unsized
126 use core::{marker::Unsize, ops::CoerceUnsized};
128 struct A<T: ?Sized>(*const T);
129 struct B<T: ?Sized>(*const T);
130 struct C<T: ?Sized> { inner: *const T }
132 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<B<U>> for B<T> {}
133 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<C<U>> for C<T> {}
135 fn foo1<T>(x: A<[T]>) -> A<[T]> { x }
136 fn foo2<T>(x: B<[T]>) -> B<[T]> { x }
137 fn foo3<T>(x: C<[T]>) -> C<[T]> { x }
139 fn test(a: A<[u8; 2]>, b: B<[u8; 2]>, c: C<[u8; 2]>) {
147 327..332 '{ x }': A<[T]>
150 365..370 '{ x }': B<[T]>
153 403..408 '{ x }': C<[T]>
155 418..419 'a': A<[u8; 2]>
156 433..434 'b': B<[u8; 2]>
157 448..449 'c': C<[u8; 2]>
158 463..529 '{ ...(c); }': ()
159 473..474 'd': A<[{unknown}]>
160 477..481 'foo1': fn foo1<{unknown}>(A<[{unknown}]>) -> A<[{unknown}]>
161 477..484 'foo1(a)': A<[{unknown}]>
162 482..483 'a': A<[u8; 2]>
163 494..495 'e': B<[u8]>
164 498..502 'foo2': fn foo2<u8>(B<[u8]>) -> B<[u8]>
165 498..505 'foo2(b)': B<[u8]>
166 503..504 'b': B<[u8; 2]>
167 515..516 'f': C<[u8]>
168 519..523 'foo3': fn foo3<u8>(C<[u8]>) -> C<[u8]>
169 519..526 'foo3(c)': C<[u8]>
170 524..525 'c': C<[u8; 2]>
176 fn infer_if_coerce() {
180 fn foo<T>(x: &[T]) -> &[T] { loop {} }
191 27..38 '{ loop {} }': &[T]
194 49..125 '{ ... }; }': ()
196 63..122 'if tru... }': &[i32]
198 71..96 '{ ... }': &[i32]
199 81..84 'foo': fn foo<i32>(&[i32]) -> &[i32]
200 81..90 'foo(&[1])': &[i32]
201 85..89 '&[1]': &[i32; 1]
202 86..89 '[1]': [i32; 1]
204 102..122 '{ ... }': &[i32; 1]
205 112..116 '&[1]': &[i32; 1]
206 113..116 '[1]': [i32; 1]
213 fn infer_if_else_coerce() {
216 //- minicore: coerce_unsized
217 fn foo<T>(x: &[T]) -> &[T] { loop {} }
228 27..38 '{ loop {} }': &[T]
231 49..125 '{ ... }; }': ()
233 63..122 'if tru... }': &[i32]
235 71..91 '{ ... }': &[i32; 1]
236 81..85 '&[1]': &[i32; 1]
237 82..85 '[1]': [i32; 1]
239 97..122 '{ ... }': &[i32]
240 107..110 'foo': fn foo<i32>(&[i32]) -> &[i32]
241 107..116 'foo(&[1])': &[i32]
242 111..115 '&[1]': &[i32; 1]
243 112..115 '[1]': [i32; 1]
250 fn infer_match_first_coerce() {
254 fn foo<T>(x: &[T]) -> &[T] { loop {} }
265 27..38 '{ loop {} }': &[T]
269 55..149 '{ ... }; }': ()
271 69..146 'match ... }': &[i32]
275 92..95 'foo': fn foo<i32>(&[i32]) -> &[i32]
276 92..101 'foo(&[2])': &[i32]
277 96..100 '&[2]': &[i32; 1]
278 97..100 '[2]': [i32; 1]
282 116..120 '&[1]': &[i32; 1]
283 117..120 '[1]': [i32; 1]
286 135..139 '&[3]': &[i32; 1]
287 136..139 '[3]': [i32; 1]
294 fn infer_match_second_coerce() {
297 //- minicore: coerce_unsized
298 fn foo<T>(x: &[T]) -> &[T] { loop {} }
309 27..38 '{ loop {} }': &[T]
313 55..149 '{ ... }; }': ()
315 69..146 'match ... }': &[i32]
319 92..96 '&[1]': &[i32; 1]
320 93..96 '[1]': [i32; 1]
324 111..114 'foo': fn foo<i32>(&[i32]) -> &[i32]
325 111..120 'foo(&[2])': &[i32]
326 115..119 '&[2]': &[i32; 1]
327 116..119 '[2]': [i32; 1]
330 135..139 '&[3]': &[i32; 1]
331 136..139 '[3]': [i32; 1]
338 fn coerce_merge_one_by_one1() {
339 cov_mark::check!(coerce_merge_fail_fallback);
348 _ => t as *const i32,
353 10..144 '{ ... }; }': ()
355 24..30 '&mut 1': &mut i32
357 40..41 'x': *const i32
358 44..141 'match ... }': *const i32
363 67..80 't as *mut i32': *mut i32
367 95..104 't as &i32': &i32
369 119..120 't': &mut i32
370 119..134 't as *const i32': *const i32
376 fn return_coerce_unknown() {
377 check_infer_with_mismatches(
384 16..39 '{ ...own; }': u32
385 22..36 'return unknown': !
386 29..36 'unknown': u32
392 fn coerce_autoderef() {
393 check_infer_with_mismatches(
396 fn takes_ref_foo(x: &Foo) {}
399 takes_ref_foo(&&Foo);
400 takes_ref_foo(&&&Foo);
406 51..132 '{ ...oo); }': ()
407 57..70 'takes_ref_foo': fn takes_ref_foo(&Foo)
408 57..76 'takes_...(&Foo)': ()
411 82..95 'takes_ref_foo': fn takes_ref_foo(&Foo)
412 82..102 'takes_...&&Foo)': ()
413 96..101 '&&Foo': &&Foo
416 108..121 'takes_ref_foo': fn takes_ref_foo(&Foo)
417 108..129 'takes_...&&Foo)': ()
418 122..128 '&&&Foo': &&&Foo
419 123..128 '&&Foo': &&Foo
420 124..128 '&Foo': &Foo
427 fn coerce_autoderef_generic() {
428 check_infer_with_mismatches(
431 fn takes_ref<T>(x: &T) -> T { *x }
443 57..126 '{ ...oo); }': ()
444 63..72 'takes_ref': fn takes_ref<Foo>(&Foo) -> Foo
445 63..78 'takes_ref(&Foo)': Foo
448 84..93 'takes_ref': fn takes_ref<&Foo>(&&Foo) -> &Foo
449 84..100 'takes_...&&Foo)': &Foo
450 94..99 '&&Foo': &&Foo
453 106..115 'takes_ref': fn takes_ref<&&Foo>(&&&Foo) -> &&Foo
454 106..123 'takes_...&&Foo)': &&Foo
455 116..122 '&&&Foo': &&&Foo
456 117..122 '&&Foo': &&Foo
457 118..122 '&Foo': &Foo
464 fn coerce_autoderef_block() {
465 check_infer_with_mismatches(
469 impl core::ops::Deref for String { type Target = str; }
470 fn takes_ref_str(x: &str) {}
471 fn returns_string() -> String { loop {} }
473 takes_ref_str(&{ returns_string() });
479 132..143 '{ loop {} }': String
480 134..141 'loop {}': !
482 154..199 '{ ... }); }': ()
483 160..173 'takes_ref_str': fn takes_ref_str(&str)
484 160..196 'takes_...g() })': ()
485 174..195 '&{ ret...ng() }': &String
486 175..195 '{ retu...ng() }': String
487 177..191 'returns_string': fn returns_string() -> String
488 177..193 'return...ring()': String
494 fn closure_return_coerce() {
495 check_infer_with_mismatches(
507 9..105 '{ ... }; }': ()
508 19..20 'x': || -> &u32
509 23..102 '|| { ... }': || -> &u32
510 26..102 '{ ... }': &u32
511 36..81 'if tru... }': ()
514 58..70 'return &1u32': !
517 90..96 '&&1u32': &&u32
525 fn coerce_fn_item_to_fn_ptr() {
526 check_infer_with_mismatches(
528 fn foo(x: u32) -> isize { 1 }
530 let f: fn(u32) -> isize = foo;
535 24..29 '{ 1 }': isize
537 40..78 '{ ...foo; }': ()
538 50..51 'f': fn(u32) -> isize
539 72..75 'foo': fn foo(u32) -> isize
545 fn coerce_fn_items_in_match_arms() {
546 cov_mark::check!(coerce_fn_reification);
548 check_infer_with_mismatches(
550 fn foo1(x: u32) -> isize { 1 }
551 fn foo2(x: u32) -> isize { 2 }
552 fn foo3(x: u32) -> isize { 3 }
563 25..30 '{ 1 }': isize
566 56..61 '{ 2 }': isize
569 87..92 '{ 3 }': isize
571 103..192 '{ ... }; }': ()
572 113..114 'x': fn(u32) -> isize
573 117..189 'match ... }': fn(u32) -> isize
577 140..144 'foo1': fn foo1(u32) -> isize
580 159..163 'foo2': fn foo2(u32) -> isize
582 178..182 'foo3': fn foo3(u32) -> isize
588 fn coerce_closure_to_fn_ptr() {
589 check_infer_with_mismatches(
592 let f: fn(u32) -> isize = |x| { 1 };
596 10..54 '{ ...1 }; }': ()
597 20..21 'f': fn(u32) -> isize
598 42..51 '|x| { 1 }': |u32| -> isize
600 46..51 '{ 1 }': isize
607 fn coerce_placeholder_ref() {
608 // placeholders should unify, even behind references
609 check_infer_with_mismatches(
613 fn get(&self) -> &TT {
619 50..54 'self': &S<TT>
620 63..86 '{ ... }': &TT
621 73..80 '&self.t': &TT
622 74..78 'self': &S<TT>
629 fn coerce_unsize_array() {
630 check_infer_with_mismatches(
632 //- minicore: coerce_unsized
634 let f: &[usize] = &[1, 2, 3];
638 10..47 '{ ... 3]; }': ()
640 34..44 '&[1, 2, 3]': &[usize; 3]
641 35..44 '[1, 2, 3]': [usize; 3]
650 fn coerce_unsize_trait_object_simple() {
651 check_infer_with_mismatches(
653 //- minicore: coerce_unsized
655 trait Bar<U, T, X>: Foo<T, U> {}
656 trait Baz<T, X>: Bar<usize, T, X> {}
659 impl<T, X> Foo<T, usize> for S<T, X> {}
660 impl<T, X> Bar<usize, T, X> for S<T, X> {}
661 impl<T, X> Baz<T, X> for S<T, X> {}
664 let obj: &dyn Baz<i8, i16> = &S;
665 let obj: &dyn Bar<_, i8, i16> = &S;
666 let obj: &dyn Foo<i8, _> = &S;
670 236..351 '{ ... &S; }': ()
671 246..249 'obj': &dyn Baz<i8, i16>
672 271..273 '&S': &S<i8, i16>
673 272..273 'S': S<i8, i16>
674 283..286 'obj': &dyn Bar<usize, i8, i16>
675 311..313 '&S': &S<i8, i16>
676 312..313 'S': S<i8, i16>
677 323..326 'obj': &dyn Foo<i8, usize>
678 346..348 '&S': &S<i8, {unknown}>
679 347..348 'S': S<i8, {unknown}>
685 fn coerce_unsize_trait_object_to_trait_object() {
686 // FIXME: The rust reference says this should be possible, but rustc doesn't
687 // implement it. We used to support it, but Chalk doesn't. Here's the
690 // 424..609 '{ ...bj2; }': ()
691 // 434..437 'obj': &dyn Baz<i8, i16>
692 // 459..461 '&S': &S<i8, i16>
693 // 460..461 'S': S<i8, i16>
694 // 471..474 'obj': &dyn Bar<usize, i8, i16>
695 // 496..499 'obj': &dyn Baz<i8, i16>
696 // 509..512 'obj': &dyn Foo<i8, usize>
697 // 531..534 'obj': &dyn Bar<usize, i8, i16>
698 // 544..548 'obj2': &dyn Baz<i8, i16>
699 // 570..572 '&S': &S<i8, i16>
700 // 571..572 'S': S<i8, i16>
701 // 582..583 '_': &dyn Foo<i8, usize>
702 // 602..606 'obj2': &dyn Baz<i8, i16>
703 check_infer_with_mismatches(
705 //- minicore: coerce_unsized
707 trait Bar<U, T, X>: Foo<T, U> {}
708 trait Baz<T, X>: Bar<usize, T, X> {}
711 impl<T, X> Foo<T, usize> for S<T, X> {}
712 impl<T, X> Bar<usize, T, X> for S<T, X> {}
713 impl<T, X> Baz<T, X> for S<T, X> {}
716 let obj: &dyn Baz<i8, i16> = &S;
717 let obj: &dyn Bar<_, _, _> = obj;
718 let obj: &dyn Foo<_, _> = obj;
719 let obj2: &dyn Baz<i8, i16> = &S;
720 let _: &dyn Foo<_, _> = obj2;
724 236..421 '{ ...bj2; }': ()
725 246..249 'obj': &dyn Baz<i8, i16>
726 271..273 '&S': &S<i8, i16>
727 272..273 'S': S<i8, i16>
728 283..286 'obj': &dyn Bar<{unknown}, {unknown}, {unknown}>
729 308..311 'obj': &dyn Baz<i8, i16>
730 321..324 'obj': &dyn Foo<{unknown}, {unknown}>
731 343..346 'obj': &dyn Bar<{unknown}, {unknown}, {unknown}>
732 356..360 'obj2': &dyn Baz<i8, i16>
733 382..384 '&S': &S<i8, i16>
734 383..384 'S': S<i8, i16>
735 394..395 '_': &dyn Foo<{unknown}, {unknown}>
736 414..418 'obj2': &dyn Baz<i8, i16>
737 308..311: expected &dyn Bar<{unknown}, {unknown}, {unknown}>, got &dyn Baz<i8, i16>
738 343..346: expected &dyn Foo<{unknown}, {unknown}>, got &dyn Bar<{unknown}, {unknown}, {unknown}>
739 414..418: expected &dyn Foo<{unknown}, {unknown}>, got &dyn Baz<i8, i16>
745 fn coerce_unsize_super_trait_cycle() {
746 check_infer_with_mismatches(
748 //- minicore: coerce_unsized
761 let obj: &dyn D = &S;
762 let obj: &dyn A = &S;
766 140..195 '{ ... &S; }': ()
767 150..153 'obj': &dyn D
770 176..179 'obj': &dyn A
778 fn coerce_unsize_generic() {
779 // FIXME: fix the type mismatches here
780 check_infer_with_mismatches(
782 //- minicore: coerce_unsized
783 struct Foo<T> { t: T };
784 struct Bar<T>(Foo<T>);
787 let _: &Foo<[usize]> = &Foo { t: [1, 2, 3] };
788 let _: &Bar<[usize]> = &Bar(Foo { t: [1, 2, 3] });
792 58..166 '{ ... }); }': ()
793 68..69 '_': &Foo<[usize]>
794 87..108 '&Foo {..., 3] }': &Foo<[usize]>
795 88..108 'Foo { ..., 3] }': Foo<[usize]>
796 97..106 '[1, 2, 3]': [usize; 3]
800 118..119 '_': &Bar<[usize]>
801 137..163 '&Bar(F... 3] })': &Bar<[i32; 3]>
802 138..141 'Bar': Bar<[i32; 3]>(Foo<[i32; 3]>) -> Bar<[i32; 3]>
803 138..163 'Bar(Fo... 3] })': Bar<[i32; 3]>
804 142..162 'Foo { ..., 3] }': Foo<[i32; 3]>
805 151..160 '[1, 2, 3]': [i32; 3]
809 97..106: expected [usize], got [usize; 3]
810 137..163: expected &Bar<[usize]>, got &Bar<[i32; 3]>
816 fn coerce_unsize_apit() {
818 check_infer_with_mismatches(
820 //- minicore: coerce_unsized
823 fn test(f: impl Foo) {
824 let _: &dyn Foo = &f;
829 35..64 '{ ... &f; }': ()
831 59..61 '&f': &impl Foo
833 59..61: expected &dyn Foo, got &impl Foo
839 fn infer_two_closures_lub() {
843 let add = |a: i32, b: i32| a + b;
844 let sub = |a, b| a - b;
845 //^ |i32, i32| -> i32
846 if c > 42 { add } else { sub };
847 //^ fn(i32, i32) -> i32
854 fn infer_match_diverging_branch_1() {
857 enum Result<T> { Ok(T), Err }
858 fn parse<T>() -> T { loop {} }
861 let a = match parse() {
873 fn infer_match_diverging_branch_2() {
874 // same as 1 except for order of branches
877 enum Result<T> { Ok(T), Err }
878 fn parse<T>() -> T { loop {} }
881 let a = match parse() {
898 pub macro panic_2015 {
900 $crate::panicking::panic()
906 pub fn panic() -> ! { loop {} }
909 #[rustc_builtin_macro = "core_panic"]
911 // Expands to either `$crate::panic::panic_2015` or `$crate::panic::panic_2021`
912 // depending on the edition of the caller.
914 /* compiler built-in */
926 fn coerce_unsize_expected_type() {
929 //- minicore: coerce_unsized
931 let foo: &[u32] = &[1, 2];
932 let foo: &[u32] = match true {
936 let foo: &[u32] = if true {