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() {
266 impl core::ops::Deref for String { type Target = str; }
279 fn coerce_fn_item_to_fn_ptr() {
282 fn foo(x: u32) -> isize { 1 }
284 let f: fn(u32) -> isize = foo;
285 // ^^^ adjustments: Pointer(ReifyFnPointer)
286 let f: unsafe fn(u32) -> isize = foo;
287 // ^^^ adjustments: Pointer(ReifyFnPointer)
293 fn coerce_fn_items_in_match_arms() {
294 cov_mark::check!(coerce_fn_reification);
298 fn foo1(x: u32) -> isize { 1 }
299 fn foo2(x: u32) -> isize { 2 }
300 fn foo3(x: u32) -> isize { 3 }
314 fn coerce_closure_to_fn_ptr() {
318 let f: fn(u32) -> isize = |x| { 1 };
324 fn coerce_placeholder_ref() {
325 // placeholders should unify, even behind references
330 fn get(&self) -> &TT {
338 fn coerce_unsize_array() {
341 //- minicore: coerce_unsized
343 let f: &[usize] = &[1, 2, 3];
350 fn coerce_unsize_trait_object_simple() {
353 //- minicore: coerce_unsized
355 trait Bar<U, T, X>: Foo<T, U> {}
356 trait Baz<T, X>: Bar<usize, T, X> {}
359 impl<T, X> Foo<T, usize> for S<T, X> {}
360 impl<T, X> Bar<usize, T, X> for S<T, X> {}
361 impl<T, X> Baz<T, X> for S<T, X> {}
364 let obj: &dyn Baz<i8, i16> = &S;
366 let obj: &dyn Bar<_, i8, i16> = &S;
368 let obj: &dyn Foo<i8, _> = &S;
375 fn coerce_unsize_super_trait_cycle() {
378 //- minicore: coerce_unsized
391 let obj: &dyn D = &S;
392 let obj: &dyn A = &S;
399 fn coerce_unsize_generic() {
400 // FIXME: fix the type mismatches here
403 //- minicore: coerce_unsized
404 struct Foo<T> { t: T };
405 struct Bar<T>(Foo<T>);
408 let _: &Foo<[usize]> = &Foo { t: [1, 2, 3] };
409 //^^^^^^^^^ expected [usize], got [usize; 3]
410 let _: &Bar<[usize]> = &Bar(Foo { t: [1, 2, 3] });
411 //^^^^^^^^^ expected [usize], got [usize; 3]
418 fn coerce_unsize_apit() {
421 //- minicore: coerce_unsized
424 fn test(f: impl Foo, g: &(impl Foo + ?Sized)) {
425 let _: &dyn Foo = &f;
427 //^ expected &dyn Foo, got &impl Foo + ?Sized
434 fn two_closures_lub() {
438 let add = |a: i32, b: i32| a + b;
439 let sub = |a, b| a - b;
440 //^^^^^^^^^^^^ |i32, i32| -> i32
441 if c > 42 { add } else { sub };
442 //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ fn(i32, i32) -> i32
449 fn match_diverging_branch_1() {
452 enum Result<T> { Ok(T), Err }
453 fn parse<T>() -> T { loop {} }
456 let a = match parse() {
468 fn match_diverging_branch_2() {
469 // same as 1 except for order of branches
472 enum Result<T> { Ok(T), Err }
473 fn parse<T>() -> T { loop {} }
476 let a = match parse() {
493 pub macro panic_2015 {
495 $crate::panicking::panic()
501 pub fn panic() -> ! { loop {} }
504 #[rustc_builtin_macro = "core_panic"]
506 // Expands to either `$crate::panic::panic_2015` or `$crate::panic::panic_2021`
507 // depending on the edition of the caller.
509 /* compiler built-in */
521 fn coerce_unsize_expected_type_1() {
524 //- minicore: coerce_unsized
526 let foo: &[u32] = &[1, 2];
527 let foo: &[u32] = match true {
531 let foo: &[u32] = if true {
542 fn coerce_unsize_expected_type_2() {
545 //- minicore: coerce_unsized
548 fn with_value<U>(self, value: U) -> InFile<U> { InFile }
552 impl AstNode for RecordField {}
554 fn takes_dyn(it: InFile<&dyn AstNode>) {}
557 let x: InFile<()> = InFile;
558 let n = &RecordField;
559 takes_dyn(x.with_value(n));
566 fn coerce_unsize_expected_type_3() {
569 //- minicore: coerce_unsized
570 enum Option<T> { Some(T), None }
573 impl AstNode for RecordField {}
575 fn takes_dyn(it: Option<&dyn AstNode>) {}
578 let x: InFile<()> = InFile;
579 let n = &RecordField;
580 takes_dyn(Option::Some(n));
587 fn coerce_unsize_expected_type_4() {
590 //- minicore: coerce_unsized
591 use core::{marker::Unsize, ops::CoerceUnsized};
593 struct B<T: ?Sized>(*const T);
594 impl<T: ?Sized> B<T> {
595 fn new(t: T) -> Self { B(&t) }
598 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<B<U>> for B<T> {}
601 let _: B<[isize]> = B::new({ [1, 2, 3] });
608 fn coerce_array_elems_lub() {
622 fn coerce_type_var() {
625 //- minicore: from, coerce_unsized
628 let _: &() = &x.into();