1 use super::{check, check_no_mismatches, check_types};
4 fn block_expr_type_mismatch() {
5 // FIXME fix double type mismatch
10 // ^^^^^^^^ expected i32, got i64
11 // ^^^^ expected i32, got i64
21 //- minicore: coerce_unsized
24 fn f<T>(_: &[T]) -> T { loop {} }
25 fn g<T>(_: S<&[T]>) -> T { loop {} }
27 fn gen<T>() -> *mut [T; 2] { loop {} }
28 fn test1<U>() -> *mut [U] {
33 let arr: &[u8; 1] = &[1];
37 let c: &[_] = { arr };
38 let d = g(S { a: arr });
39 let e: [&[_]; 1] = [arr];
40 let f: [&[_]; 2] = [arr; 2];
41 let g: (&[_], &[_]) = (arr, arr);
48 fn let_stmt_coerce() {
51 //- minicore: coerce_unsized
53 let x: &[isize] = &[1];
54 // ^^^^ adjustments: Deref(None), Borrow(Ref(Not)), Pointer(Unsize)
55 let x: *const [isize] = &[1];
56 // ^^^^ adjustments: Deref(None), Borrow(RawPtr(Not)), Pointer(Unsize)
63 fn custom_coerce_unsized() {
66 //- minicore: coerce_unsized
67 use core::{marker::Unsize, ops::CoerceUnsized};
69 struct A<T: ?Sized>(*const T);
70 struct B<T: ?Sized>(*const T);
71 struct C<T: ?Sized> { inner: *const T }
73 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<B<U>> for B<T> {}
74 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<C<U>> for C<T> {}
76 fn foo1<T>(x: A<[T]>) -> A<[T]> { x }
77 fn foo2<T>(x: B<[T]>) -> B<[T]> { x }
78 fn foo3<T>(x: C<[T]>) -> C<[T]> { x }
80 fn test(a: A<[u8; 2]>, b: B<[u8; 2]>, c: C<[u8; 2]>) {
82 // ^ expected A<[{unknown}]>, got A<[u8; 2]>
96 //- minicore: coerce_unsized
97 fn foo<T>(x: &[T]) -> &[T] { x }
101 // ^^^^ adjustments: Deref(None), Borrow(Ref(Not)), Pointer(Unsize)
111 fn if_else_coerce() {
114 //- minicore: coerce_unsized
115 fn foo<T>(x: &[T]) -> &[T] { x }
128 fn match_first_coerce() {
131 //- minicore: coerce_unsized
132 fn foo<T>(x: &[T]) -> &[T] { x }
136 // ^^^^ adjustments: Deref(None), Borrow(Ref(Not)), Pointer(Unsize)
146 fn match_second_coerce() {
149 //- minicore: coerce_unsized
150 fn foo<T>(x: &[T]) -> &[T] { loop {} }
151 // ^^^^^^^ adjustments: NeverToAny
164 fn coerce_merge_one_by_one1() {
165 cov_mark::check!(coerce_merge_fail_fallback);
174 //^^^^^^^^^ expected *mut i32, got &i32
175 _ => t as *const i32,
176 // ^^^^^^^^^^^^^^^ adjustments: Pointer(MutToConstPointer)
188 fn return_coerce_unknown() {
200 fn coerce_autoderef() {
204 fn takes_ref_foo(x: &Foo) {}
207 takes_ref_foo(&&Foo);
208 takes_ref_foo(&&&Foo);
214 fn coerce_autoderef_generic() {
218 fn takes_ref<T>(x: &T) -> T { *x }
229 fn coerce_autoderef_block() {
234 impl core::ops::Deref for String { type Target = str; }
235 fn takes_ref_str(x: &str) {}
236 fn returns_string() -> String { loop {} }
238 takes_ref_str(&{ returns_string() });
239 // ^^^^^^^^^^^^^^^^^^^^^ adjustments: Deref(None), Deref(Some(OverloadedDeref(Not))), Borrow(Ref(Not))
246 fn closure_return_coerce() {
261 fn coerce_fn_item_to_fn_ptr() {
264 fn foo(x: u32) -> isize { 1 }
266 let f: fn(u32) -> isize = foo;
267 // ^^^ adjustments: Pointer(ReifyFnPointer)
268 let f: unsafe fn(u32) -> isize = foo;
269 // ^^^ adjustments: Pointer(ReifyFnPointer)
275 fn coerce_fn_items_in_match_arms() {
276 cov_mark::check!(coerce_fn_reification);
280 fn foo1(x: u32) -> isize { 1 }
281 fn foo2(x: u32) -> isize { 2 }
282 fn foo3(x: u32) -> isize { 3 }
296 fn coerce_closure_to_fn_ptr() {
300 let f: fn(u32) -> isize = |x| { 1 };
306 fn coerce_placeholder_ref() {
307 // placeholders should unify, even behind references
312 fn get(&self) -> &TT {
320 fn coerce_unsize_array() {
323 //- minicore: coerce_unsized
325 let f: &[usize] = &[1, 2, 3];
332 fn coerce_unsize_trait_object_simple() {
335 //- minicore: coerce_unsized
337 trait Bar<U, T, X>: Foo<T, U> {}
338 trait Baz<T, X>: Bar<usize, T, X> {}
341 impl<T, X> Foo<T, usize> for S<T, X> {}
342 impl<T, X> Bar<usize, T, X> for S<T, X> {}
343 impl<T, X> Baz<T, X> for S<T, X> {}
346 let obj: &dyn Baz<i8, i16> = &S;
348 let obj: &dyn Bar<_, i8, i16> = &S;
350 let obj: &dyn Foo<i8, _> = &S;
357 fn coerce_unsize_super_trait_cycle() {
360 //- minicore: coerce_unsized
373 let obj: &dyn D = &S;
374 let obj: &dyn A = &S;
381 fn coerce_unsize_generic() {
382 // FIXME: fix the type mismatches here
385 //- minicore: coerce_unsized
386 struct Foo<T> { t: T };
387 struct Bar<T>(Foo<T>);
390 let _: &Foo<[usize]> = &Foo { t: [1, 2, 3] };
391 //^^^^^^^^^ expected [usize], got [usize; 3]
392 let _: &Bar<[usize]> = &Bar(Foo { t: [1, 2, 3] });
393 //^^^^^^^^^^^^^^^^^^^^^^^^^^ expected &Bar<[usize]>, got &Bar<[i32; 3]>
400 fn coerce_unsize_apit() {
404 //- minicore: coerce_unsized
407 fn test(f: impl Foo) {
408 let _: &dyn Foo = &f;
409 //^^ expected &dyn Foo, got &impl Foo
416 fn two_closures_lub() {
420 let add = |a: i32, b: i32| a + b;
421 let sub = |a, b| a - b;
422 //^^^^^^^^^^^^ |i32, i32| -> i32
423 if c > 42 { add } else { sub };
424 //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ fn(i32, i32) -> i32
431 fn match_diverging_branch_1() {
434 enum Result<T> { Ok(T), Err }
435 fn parse<T>() -> T { loop {} }
438 let a = match parse() {
450 fn match_diverging_branch_2() {
451 // same as 1 except for order of branches
454 enum Result<T> { Ok(T), Err }
455 fn parse<T>() -> T { loop {} }
458 let a = match parse() {
475 pub macro panic_2015 {
477 $crate::panicking::panic()
483 pub fn panic() -> ! { loop {} }
486 #[rustc_builtin_macro = "core_panic"]
488 // Expands to either `$crate::panic::panic_2015` or `$crate::panic::panic_2021`
489 // depending on the edition of the caller.
491 /* compiler built-in */
503 fn coerce_unsize_expected_type_1() {
506 //- minicore: coerce_unsized
508 let foo: &[u32] = &[1, 2];
509 let foo: &[u32] = match true {
513 let foo: &[u32] = if true {
524 fn coerce_unsize_expected_type_2() {
525 // FIXME: this is wrong, #9560
528 //- minicore: coerce_unsized
531 fn with_value<U>(self, value: U) -> InFile<U> { InFile }
535 impl AstNode for RecordField {}
537 fn takes_dyn(it: InFile<&dyn AstNode>) {}
540 let x: InFile<()> = InFile;
541 let n = &RecordField;
542 takes_dyn(x.with_value(n));
543 // ^^^^^^^^^^^^^^^ expected InFile<&dyn AstNode>, got InFile<&RecordField>
550 fn coerce_array_elems_lub() {