1 use expect_test::expect;
4 use super::{check_infer, check_infer_with_mismatches};
7 fn infer_block_expr_type_mismatch() {
11 let a: i32 = { 1i64 };
15 10..40 '{ ...4 }; }': ()
17 29..37 '{ 1i64 }': i64
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);
52 pub trait Unsize<T: ?Sized> {}
53 #[lang = "coerce_unsized"]
54 pub trait CoerceUnsized<T> {}
56 impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
57 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
61 44..55 '{ loop {} }': T
65 81..92 '{ loop {} }': T
68 121..132 '{ loop {} }': *mut [T; _]
71 159..172 '{ gen() }': *mut [U]
72 165..168 'gen': fn gen<U>() -> *mut [U; _]
73 165..170 'gen()': *mut [U; _]
74 185..419 '{ ...rr); }': ()
75 195..198 'arr': &[u8; _]
76 211..215 '&[1]': &[u8; _]
77 212..215 '[1]': [u8; _]
80 236..239 'arr': &[u8; _]
82 253..254 'f': fn f<u8>(&[u8]) -> u8
84 255..258 'arr': &[u8; _]
86 279..286 '{ arr }': &[u8]
87 281..284 'arr': &[u8; _]
89 300..301 'g': fn g<u8>(S<&[u8]>) -> u8
90 300..315 'g(S { a: arr })': u8
91 302..314 'S { a: arr }': S<&[u8]>
92 309..312 'arr': &[u8; _]
93 325..326 'e': [&[u8]; _]
94 340..345 '[arr]': [&[u8]; _]
95 341..344 'arr': &[u8; _]
96 355..356 'f': [&[u8]; _]
97 370..378 '[arr; 2]': [&[u8]; _]
98 371..374 'arr': &[u8; _]
100 388..389 'g': (&[u8], &[u8])
101 406..416 '(arr, arr)': (&[u8], &[u8])
102 407..410 'arr': &[u8; _]
103 412..415 'arr': &[u8; _]
109 fn infer_let_stmt_coerce() {
113 let x: &[isize] = &[1];
114 let x: *const [isize] = &[1];
118 10..75 '{ ...[1]; }': ()
120 34..38 '&[1]': &[isize; _]
121 35..38 '[1]': [isize; _]
123 48..49 'x': *const [isize]
124 68..72 '&[1]': &[isize; _]
125 69..72 '[1]': [isize; _]
132 fn infer_custom_coerce_unsized() {
135 struct A<T: ?Sized>(*const T);
136 struct B<T: ?Sized>(*const T);
137 struct C<T: ?Sized> { inner: *const T }
139 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<B<U>> for B<T> {}
140 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<C<U>> for C<T> {}
142 fn foo1<T>(x: A<[T]>) -> A<[T]> { x }
143 fn foo2<T>(x: B<[T]>) -> B<[T]> { x }
144 fn foo3<T>(x: C<[T]>) -> C<[T]> { x }
146 fn test(a: A<[u8; 2]>, b: B<[u8; 2]>, c: C<[u8; 2]>) {
156 pub trait Unsize<T: ?Sized> {}
157 #[lang = "coerce_unsized"]
158 pub trait CoerceUnsized<T> {}
160 impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
161 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
165 278..283 '{ x }': A<[T]>
168 316..321 '{ x }': B<[T]>
171 354..359 '{ x }': C<[T]>
173 369..370 'a': A<[u8; _]>
174 384..385 'b': B<[u8; _]>
175 399..400 'c': C<[u8; _]>
176 414..480 '{ ...(c); }': ()
177 424..425 'd': A<[{unknown}]>
178 428..432 'foo1': fn foo1<{unknown}>(A<[{unknown}]>) -> A<[{unknown}]>
179 428..435 'foo1(a)': A<[{unknown}]>
180 433..434 'a': A<[u8; _]>
181 445..446 'e': B<[u8]>
182 449..453 'foo2': fn foo2<u8>(B<[u8]>) -> B<[u8]>
183 449..456 'foo2(b)': B<[u8]>
184 454..455 'b': B<[u8; _]>
185 466..467 'f': C<[u8]>
186 470..474 'foo3': fn foo3<u8>(C<[u8]>) -> C<[u8]>
187 470..477 'foo3(c)': C<[u8]>
188 475..476 'c': C<[u8; _]>
194 fn infer_if_coerce() {
197 fn foo<T>(x: &[T]) -> &[T] { loop {} }
210 pub trait Unsize<T: ?Sized> {}
214 27..38 '{ loop {} }': &[T]
217 49..125 '{ ... }; }': ()
219 63..122 'if tru... }': &[i32]
221 71..96 '{ ... }': &[i32]
222 81..84 'foo': fn foo<i32>(&[i32]) -> &[i32]
223 81..90 'foo(&[1])': &[i32]
224 85..89 '&[1]': &[i32; _]
225 86..89 '[1]': [i32; _]
227 102..122 '{ ... }': &[i32; _]
228 112..116 '&[1]': &[i32; _]
229 113..116 '[1]': [i32; _]
236 fn infer_if_else_coerce() {
239 fn foo<T>(x: &[T]) -> &[T] { loop {} }
251 pub trait Unsize<T: ?Sized> {}
252 #[lang = "coerce_unsized"]
253 pub trait CoerceUnsized<T> {}
255 impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
256 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
260 27..38 '{ loop {} }': &[T]
263 49..125 '{ ... }; }': ()
265 63..122 'if tru... }': &[i32]
267 71..91 '{ ... }': &[i32; _]
268 81..85 '&[1]': &[i32; _]
269 82..85 '[1]': [i32; _]
271 97..122 '{ ... }': &[i32]
272 107..110 'foo': fn foo<i32>(&[i32]) -> &[i32]
273 107..116 'foo(&[1])': &[i32]
274 111..115 '&[1]': &[i32; _]
275 112..115 '[1]': [i32; _]
282 fn infer_match_first_coerce() {
285 fn foo<T>(x: &[T]) -> &[T] { loop {} }
297 pub trait Unsize<T: ?Sized> {}
301 27..38 '{ loop {} }': &[T]
305 55..149 '{ ... }; }': ()
307 69..146 'match ... }': &[i32]
311 92..95 'foo': fn foo<i32>(&[i32]) -> &[i32]
312 92..101 'foo(&[2])': &[i32]
313 96..100 '&[2]': &[i32; _]
314 97..100 '[2]': [i32; _]
318 116..120 '&[1]': &[i32; _]
319 117..120 '[1]': [i32; _]
322 135..139 '&[3]': &[i32; _]
323 136..139 '[3]': [i32; _]
330 fn infer_match_second_coerce() {
333 fn foo<T>(x: &[T]) -> &[T] { loop {} }
345 pub trait Unsize<T: ?Sized> {}
346 #[lang = "coerce_unsized"]
347 pub trait CoerceUnsized<T> {}
349 impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
350 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
354 27..38 '{ loop {} }': &[T]
358 55..149 '{ ... }; }': ()
360 69..146 'match ... }': &[i32]
364 92..96 '&[1]': &[i32; _]
365 93..96 '[1]': [i32; _]
369 111..114 'foo': fn foo<i32>(&[i32]) -> &[i32]
370 111..120 'foo(&[2])': &[i32]
371 115..119 '&[2]': &[i32; _]
372 116..119 '[2]': [i32; _]
375 135..139 '&[3]': &[i32; _]
376 136..139 '[3]': [i32; _]
383 fn coerce_merge_one_by_one1() {
384 mark::check!(coerce_merge_fail_fallback);
393 _ => t as *const i32,
398 10..144 '{ ... }; }': ()
400 24..30 '&mut 1': &mut i32
402 40..41 'x': *const i32
403 44..141 'match ... }': *const i32
408 67..80 't as *mut i32': *mut i32
412 95..104 't as &i32': &i32
414 119..120 't': &mut i32
415 119..134 't as *const i32': *const i32
421 fn return_coerce_unknown() {
422 check_infer_with_mismatches(
429 16..39 '{ ...own; }': u32
430 22..36 'return unknown': !
431 29..36 'unknown': u32
437 fn coerce_autoderef() {
438 check_infer_with_mismatches(
441 fn takes_ref_foo(x: &Foo) {}
444 takes_ref_foo(&&Foo);
445 takes_ref_foo(&&&Foo);
451 51..132 '{ ...oo); }': ()
452 57..70 'takes_ref_foo': fn takes_ref_foo(&Foo)
453 57..76 'takes_...(&Foo)': ()
456 82..95 'takes_ref_foo': fn takes_ref_foo(&Foo)
457 82..102 'takes_...&&Foo)': ()
458 96..101 '&&Foo': &&Foo
461 108..121 'takes_ref_foo': fn takes_ref_foo(&Foo)
462 108..129 'takes_...&&Foo)': ()
463 122..128 '&&&Foo': &&&Foo
464 123..128 '&&Foo': &&Foo
465 124..128 '&Foo': &Foo
472 fn coerce_autoderef_generic() {
473 check_infer_with_mismatches(
476 fn takes_ref<T>(x: &T) -> T { *x }
488 57..126 '{ ...oo); }': ()
489 63..72 'takes_ref': fn takes_ref<Foo>(&Foo) -> Foo
490 63..78 'takes_ref(&Foo)': Foo
493 84..93 'takes_ref': fn takes_ref<&Foo>(&&Foo) -> &Foo
494 84..100 'takes_...&&Foo)': &Foo
495 94..99 '&&Foo': &&Foo
498 106..115 'takes_ref': fn takes_ref<&&Foo>(&&&Foo) -> &&Foo
499 106..123 'takes_...&&Foo)': &&Foo
500 116..122 '&&&Foo': &&&Foo
501 117..122 '&&Foo': &&Foo
502 118..122 '&Foo': &Foo
509 fn coerce_autoderef_block() {
510 check_infer_with_mismatches(
514 trait Deref { type Target; }
515 impl Deref for String { type Target = str; }
516 fn takes_ref_str(x: &str) {}
517 fn returns_string() -> String { loop {} }
519 takes_ref_str(&{ returns_string() });
525 168..179 '{ loop {} }': String
526 170..177 'loop {}': !
528 190..235 '{ ... }); }': ()
529 196..209 'takes_ref_str': fn takes_ref_str(&str)
530 196..232 'takes_...g() })': ()
531 210..231 '&{ ret...ng() }': &String
532 211..231 '{ retu...ng() }': String
533 213..227 'returns_string': fn returns_string() -> String
534 213..229 'return...ring()': String
540 fn closure_return_coerce() {
541 check_infer_with_mismatches(
553 9..105 '{ ... }; }': ()
554 19..20 'x': || -> &u32
555 23..102 '|| { ... }': || -> &u32
556 26..102 '{ ... }': &u32
557 36..81 'if tru... }': ()
560 58..70 'return &1u32': !
563 90..96 '&&1u32': &&u32
571 fn coerce_fn_item_to_fn_ptr() {
572 check_infer_with_mismatches(
574 fn foo(x: u32) -> isize { 1 }
576 let f: fn(u32) -> isize = foo;
581 24..29 '{ 1 }': isize
583 40..78 '{ ...foo; }': ()
584 50..51 'f': fn(u32) -> isize
585 72..75 'foo': fn foo(u32) -> isize
591 fn coerce_fn_items_in_match_arms() {
592 mark::check!(coerce_fn_reification);
594 check_infer_with_mismatches(
596 fn foo1(x: u32) -> isize { 1 }
597 fn foo2(x: u32) -> isize { 2 }
598 fn foo3(x: u32) -> isize { 3 }
609 25..30 '{ 1 }': isize
612 56..61 '{ 2 }': isize
615 87..92 '{ 3 }': isize
617 103..192 '{ ... }; }': ()
618 113..114 'x': fn(u32) -> isize
619 117..189 'match ... }': fn(u32) -> isize
623 140..144 'foo1': fn foo1(u32) -> isize
626 159..163 'foo2': fn foo2(u32) -> isize
628 178..182 'foo3': fn foo3(u32) -> isize
634 fn coerce_closure_to_fn_ptr() {
635 check_infer_with_mismatches(
638 let f: fn(u32) -> isize = |x| { 1 };
642 10..54 '{ ...1 }; }': ()
643 20..21 'f': fn(u32) -> isize
644 42..51 '|x| { 1 }': |u32| -> isize
646 46..51 '{ 1 }': isize
653 fn coerce_placeholder_ref() {
654 // placeholders should unify, even behind references
655 check_infer_with_mismatches(
659 fn get(&self) -> &TT {
665 50..54 'self': &S<TT>
666 63..86 '{ ... }': &TT
667 73..80 '&self.t': &TT
668 74..78 'self': &S<TT>
675 fn coerce_unsize_array() {
676 check_infer_with_mismatches(
679 pub trait Unsize<T> {}
680 #[lang = "coerce_unsized"]
681 pub trait CoerceUnsized<T> {}
683 impl<T: Unsize<U>, U> CoerceUnsized<&U> for &T {}
686 let f: &[usize] = &[1, 2, 3];
690 161..198 '{ ... 3]; }': ()
691 171..172 'f': &[usize]
692 185..195 '&[1, 2, 3]': &[usize; _]
693 186..195 '[1, 2, 3]': [usize; _]
702 fn coerce_unsize_trait_object_simple() {
703 check_infer_with_mismatches(
708 pub trait Unsize<T> {}
709 #[lang = "coerce_unsized"]
710 pub trait CoerceUnsized<T> {}
712 impl<T: Unsize<U>, U> CoerceUnsized<&U> for &T {}
715 trait Bar<U, T, X>: Foo<T, U> {}
716 trait Baz<T, X>: Bar<usize, T, X> {}
719 impl<T, X> Foo<T, usize> for S<T, X> {}
720 impl<T, X> Bar<usize, T, X> for S<T, X> {}
721 impl<T, X> Baz<T, X> for S<T, X> {}
724 let obj: &dyn Baz<i8, i16> = &S;
725 let obj: &dyn Bar<_, i8, i16> = &S;
726 let obj: &dyn Foo<i8, _> = &S;
730 424..539 '{ ... &S; }': ()
731 434..437 'obj': &dyn Baz<i8, i16>
732 459..461 '&S': &S<i8, i16>
733 460..461 'S': S<i8, i16>
734 471..474 'obj': &dyn Bar<usize, i8, i16>
735 499..501 '&S': &S<i8, i16>
736 500..501 'S': S<i8, i16>
737 511..514 'obj': &dyn Foo<i8, usize>
738 534..536 '&S': &S<i8, {unknown}>
739 535..536 'S': S<i8, {unknown}>
745 // The rust reference says this should be possible, but rustc doesn't implement
746 // it. We used to support it, but Chalk doesn't.
748 fn coerce_unsize_trait_object_to_trait_object() {
749 check_infer_with_mismatches(
754 pub trait Unsize<T> {}
755 #[lang = "coerce_unsized"]
756 pub trait CoerceUnsized<T> {}
758 impl<T: Unsize<U>, U> CoerceUnsized<&U> for &T {}
761 trait Bar<U, T, X>: Foo<T, U> {}
762 trait Baz<T, X>: Bar<usize, T, X> {}
765 impl<T, X> Foo<T, usize> for S<T, X> {}
766 impl<T, X> Bar<usize, T, X> for S<T, X> {}
767 impl<T, X> Baz<T, X> for S<T, X> {}
770 let obj: &dyn Baz<i8, i16> = &S;
771 let obj: &dyn Bar<_, _, _> = obj;
772 let obj: &dyn Foo<_, _> = obj;
773 let obj2: &dyn Baz<i8, i16> = &S;
774 let _: &dyn Foo<_, _> = obj2;
778 424..609 '{ ...bj2; }': ()
779 434..437 'obj': &dyn Baz<i8, i16>
780 459..461 '&S': &S<i8, i16>
781 460..461 'S': S<i8, i16>
782 471..474 'obj': &dyn Bar<usize, i8, i16>
783 496..499 'obj': &dyn Baz<i8, i16>
784 509..512 'obj': &dyn Foo<i8, usize>
785 531..534 'obj': &dyn Bar<usize, i8, i16>
786 544..548 'obj2': &dyn Baz<i8, i16>
787 570..572 '&S': &S<i8, i16>
788 571..572 'S': S<i8, i16>
789 582..583 '_': &dyn Foo<i8, usize>
790 602..606 'obj2': &dyn Baz<i8, i16>
796 fn coerce_unsize_super_trait_cycle() {
797 check_infer_with_mismatches(
802 pub trait Unsize<T> {}
803 #[lang = "coerce_unsized"]
804 pub trait CoerceUnsized<T> {}
806 impl<T: Unsize<U>, U> CoerceUnsized<&U> for &T {}
820 let obj: &dyn D = &S;
821 let obj: &dyn A = &S;
825 328..383 '{ ... &S; }': ()
826 338..341 'obj': &dyn D
829 364..367 'obj': &dyn A
838 fn coerce_unsize_generic() {
839 // FIXME: Implement this
840 // https://doc.rust-lang.org/reference/type-coercions.html#unsized-coercions
841 check_infer_with_mismatches(
844 pub trait Unsize<T> {}
845 #[lang = "coerce_unsized"]
846 pub trait CoerceUnsized<T> {}
848 impl<T: Unsize<U>, U> CoerceUnsized<&U> for &T {}
850 struct Foo<T> { t: T };
851 struct Bar<T>(Foo<T>);
854 let _: &Foo<[usize]> = &Foo { t: [1, 2, 3] };
855 let _: &Bar<[usize]> = &Bar(Foo { t: [1, 2, 3] });