1 use super::{check, check_no_mismatches, check_types}
;
4 fn block_expr_type_mismatch() {
9 // ^^^^ expected i32, got i64
19 //- minicore: coerce_unsized
22 fn f<T>(_: &[T]) -> T { loop {} }
23 fn g<T>(_: S<&[T]>) -> T { loop {} }
25 fn gen<T>() -> *mut [T; 2] { loop {} }
26 fn test1<U>() -> *mut [U] {
31 let arr: &[u8; 1] = &[1];
35 let c: &[_] = { arr };
36 let d = g(S { a: arr });
37 let e: [&[_]; 1] = [arr];
38 let f: [&[_]; 2] = [arr; 2];
39 let g: (&[_], &[_]) = (arr, arr);
46 fn let_stmt_coerce() {
49 //- minicore: coerce_unsized
51 let x: &[isize] = &[1];
52 // ^^^^ adjustments: Deref(None), Borrow(Ref(Not)), Pointer(Unsize)
53 let x: *const [isize] = &[1];
54 // ^^^^ adjustments: Deref(None), Borrow(RawPtr(Not)), Pointer(Unsize)
61 fn custom_coerce_unsized() {
64 //- minicore: coerce_unsized
65 use core::{marker::Unsize, ops::CoerceUnsized};
67 struct A<T: ?Sized>(*const T);
68 struct B<T: ?Sized>(*const T);
69 struct C<T: ?Sized> { inner: *const T }
71 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<B<U>> for B<T> {}
72 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<C<U>> for C<T> {}
74 fn foo1<T>(x: A<[T]>) -> A<[T]> { x }
75 fn foo2<T>(x: B<[T]>) -> B<[T]> { x }
76 fn foo3<T>(x: C<[T]>) -> C<[T]> { x }
78 fn test(a: A<[u8; 2]>, b: B<[u8; 2]>, c: C<[u8; 2]>) {
80 // ^ expected A<[{unknown}]>, got A<[u8; 2]>
94 //- minicore: coerce_unsized
95 fn foo<T>(x: &[T]) -> &[T] { x }
99 // ^^^^ adjustments: Deref(None), Borrow(Ref(Not)), Pointer(Unsize)
109 fn if_else_coerce() {
112 //- minicore: coerce_unsized
113 fn foo<T>(x: &[T]) -> &[T] { x }
126 fn match_first_coerce() {
129 //- minicore: coerce_unsized
130 fn foo<T>(x: &[T]) -> &[T] { x }
134 // ^^^^ adjustments: Deref(None), Borrow(Ref(Not)), Pointer(Unsize)
144 fn match_second_coerce() {
147 //- minicore: coerce_unsized
148 fn foo<T>(x: &[T]) -> &[T] { loop {} }
149 // ^^^^^^^ adjustments: NeverToAny
162 fn coerce_merge_one_by_one1() {
163 cov_mark
::check
!(coerce_merge_fail_fallback
);
172 //^^^^^^^^^ expected *mut i32, got &i32
173 _ => t as *const i32,
174 // ^^^^^^^^^^^^^^^ adjustments: Pointer(MutToConstPointer)
186 fn return_coerce_unknown() {
198 fn coerce_autoderef() {
202 fn takes_ref_foo(x: &Foo) {}
205 takes_ref_foo(&&Foo);
206 takes_ref_foo(&&&Foo);
212 fn coerce_autoderef_generic() {
216 fn takes_ref<T>(x: &T) -> T { *x }
227 fn coerce_autoderef_block() {
232 impl core::ops::Deref for String { type Target = str; }
233 fn takes_ref_str(x: &str) {}
234 fn returns_string() -> String { loop {} }
236 takes_ref_str(&{ returns_string() });
237 // ^^^^^^^^^^^^^^^^^^^^^ adjustments: Deref(None), Deref(Some(OverloadedDeref(Not))), Borrow(Ref(Not))
244 fn coerce_autoderef_implication_1() {
249 impl core::ops::Deref for Foo<u32> { type Target = (); }
251 fn takes_ref_foo<T>(x: &Foo<T>) {}
254 //^^^ type: Foo<{unknown}>
265 fn coerce_autoderef_implication_2() {
270 impl core::ops::Deref for Foo<u32> { type Target = (); }
272 fn takes_ref_foo<T>(x: &Foo<T>) {}
275 //^^^ type: Foo<{unknown}>
277 //^^^^ expected &u32, got &Foo<{unknown}>
283 fn closure_return_coerce() {
298 fn generator_yield_return_coerce() {
321 impl core::ops::Deref for String { type Target = str; }
334 fn destructuring_assign_coerce() {
339 impl core::ops::Deref for String { type Target = str; }
344 [text, _] = [&tmp, &tmp];
352 fn coerce_fn_item_to_fn_ptr() {
355 fn foo(x: u32) -> isize { 1 }
357 let f: fn(u32) -> isize = foo;
358 // ^^^ adjustments: Pointer(ReifyFnPointer)
359 let f: unsafe fn(u32) -> isize = foo;
360 // ^^^ adjustments: Pointer(ReifyFnPointer)
366 fn coerce_fn_items_in_match_arms() {
367 cov_mark
::check
!(coerce_fn_reification
);
371 fn foo1(x: u32) -> isize { 1 }
372 fn foo2(x: u32) -> isize { 2 }
373 fn foo3(x: u32) -> isize { 3 }
387 fn coerce_closure_to_fn_ptr() {
391 let f: fn(u32) -> isize = |x| { 1 };
397 fn coerce_placeholder_ref() {
398 // placeholders should unify, even behind references
403 fn get(&self) -> &TT {
411 fn coerce_unsize_array() {
414 //- minicore: coerce_unsized
416 let f: &[usize] = &[1, 2, 3];
423 fn coerce_unsize_trait_object_simple() {
426 //- minicore: coerce_unsized
428 trait Bar<U, T, X>: Foo<T, U> {}
429 trait Baz<T, X>: Bar<usize, T, X> {}
432 impl<T, X> Foo<T, usize> for S<T, X> {}
433 impl<T, X> Bar<usize, T, X> for S<T, X> {}
434 impl<T, X> Baz<T, X> for S<T, X> {}
437 let obj: &dyn Baz<i8, i16> = &S;
439 let obj: &dyn Bar<_, i8, i16> = &S;
441 let obj: &dyn Foo<i8, _> = &S;
448 fn coerce_unsize_super_trait_cycle() {
451 //- minicore: coerce_unsized
464 let obj: &dyn D = &S;
465 let obj: &dyn A = &S;
472 fn coerce_unsize_generic() {
473 // FIXME: fix the type mismatches here
476 //- minicore: coerce_unsized
477 struct Foo<T> { t: T };
478 struct Bar<T>(Foo<T>);
481 let _: &Foo<[usize]> = &Foo { t: [1, 2, 3] };
482 //^^^^^^^^^ expected [usize], got [usize; 3]
483 let _: &Bar<[usize]> = &Bar(Foo { t: [1, 2, 3] });
484 //^^^^^^^^^ expected [usize], got [usize; 3]
491 fn coerce_unsize_apit() {
494 //- minicore: coerce_unsized
497 fn test(f: impl Foo, g: &(impl Foo + ?Sized)) {
498 let _: &dyn Foo = &f;
500 //^ expected &dyn Foo, got &impl Foo + ?Sized
507 fn two_closures_lub() {
511 let add = |a: i32, b: i32| a + b;
512 let sub = |a, b| a - b;
513 //^^^^^^^^^^^^ |i32, i32| -> i32
514 if c > 42 { add } else { sub };
515 //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ fn(i32, i32) -> i32
522 fn match_diverging_branch_1() {
525 enum Result<T> { Ok(T), Err }
526 fn parse<T>() -> T { loop {} }
529 let a = match parse() {
541 fn match_diverging_branch_2() {
542 // same as 1 except for order of branches
545 enum Result<T> { Ok(T), Err }
546 fn parse<T>() -> T { loop {} }
549 let a = match parse() {
566 pub macro panic_2015 {
568 $crate::panicking::panic()
574 pub fn panic() -> ! { loop {} }
577 #[rustc_builtin_macro = "core_panic"]
579 // Expands to either `$crate::panic::panic_2015` or `$crate::panic::panic_2021`
580 // depending on the edition of the caller.
582 /* compiler built-in */
594 fn coerce_unsize_expected_type_1() {
597 //- minicore: coerce_unsized
599 let foo: &[u32] = &[1, 2];
600 let foo: &[u32] = match true {
604 let foo: &[u32] = if true {
615 fn coerce_unsize_expected_type_2() {
618 //- minicore: coerce_unsized
621 fn with_value<U>(self, value: U) -> InFile<U> { InFile }
625 impl AstNode for RecordField {}
627 fn takes_dyn(it: InFile<&dyn AstNode>) {}
630 let x: InFile<()> = InFile;
631 let n = &RecordField;
632 takes_dyn(x.with_value(n));
639 fn coerce_unsize_expected_type_3() {
642 //- minicore: coerce_unsized
643 enum Option<T> { Some(T), None }
646 impl AstNode for RecordField {}
648 fn takes_dyn(it: Option<&dyn AstNode>) {}
651 let x: InFile<()> = InFile;
652 let n = &RecordField;
653 takes_dyn(Option::Some(n));
660 fn coerce_unsize_expected_type_4() {
663 //- minicore: coerce_unsized
664 use core::{marker::Unsize, ops::CoerceUnsized};
666 struct B<T: ?Sized>(*const T);
667 impl<T: ?Sized> B<T> {
668 fn new(t: T) -> Self { B(&t) }
671 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<B<U>> for B<T> {}
674 let _: B<[isize]> = B::new({ [1, 2, 3] });
681 fn coerce_array_elems_lub() {
695 fn coerce_type_var() {
698 //- minicore: from, coerce_unsized
701 let _: &() = &x.into();
708 fn coerce_overloaded_binary_op_rhs() {
711 //- minicore: deref, add
714 impl core::ops::Deref for String { type Target = str; }
716 impl core::ops::Add<&str> for String {
717 type Output = String;
732 fn assign_coerce_struct_fields() {
735 //- minicore: coerce_unsized
745 let mut a: V<&dyn Tr> = V { t: &S };
753 fn destructuring_assign_coerce_struct_fields() {
756 //- minicore: coerce_unsized
765 //^^^^expected V<&S>, got (V<&dyn Tr>,)
767 let mut a: V<&dyn Tr> = V { t: &S };
769 //^^^^expected V<&S>, got (V<&dyn Tr>,)