diff options
Diffstat (limited to 'crates/hir_ty/src/tests/coercion.rs')
-rw-r--r-- | crates/hir_ty/src/tests/coercion.rs | 629 |
1 files changed, 266 insertions, 363 deletions
diff --git a/crates/hir_ty/src/tests/coercion.rs b/crates/hir_ty/src/tests/coercion.rs index 4f859fc85..713b74165 100644 --- a/crates/hir_ty/src/tests/coercion.rs +++ b/crates/hir_ty/src/tests/coercion.rs | |||
@@ -23,38 +23,29 @@ fn infer_block_expr_type_mismatch() { | |||
23 | fn coerce_places() { | 23 | fn coerce_places() { |
24 | check_infer( | 24 | check_infer( |
25 | r#" | 25 | r#" |
26 | struct S<T> { a: T } | 26 | //- minicore: coerce_unsized |
27 | struct S<T> { a: T } | ||
27 | 28 | ||
28 | fn f<T>(_: &[T]) -> T { loop {} } | 29 | fn f<T>(_: &[T]) -> T { loop {} } |
29 | fn g<T>(_: S<&[T]>) -> T { loop {} } | 30 | fn g<T>(_: S<&[T]>) -> T { loop {} } |
30 | 31 | ||
31 | fn gen<T>() -> *mut [T; 2] { loop {} } | 32 | fn gen<T>() -> *mut [T; 2] { loop {} } |
32 | fn test1<U>() -> *mut [U] { | 33 | fn test1<U>() -> *mut [U] { |
33 | gen() | 34 | gen() |
34 | } | 35 | } |
35 | |||
36 | fn test2() { | ||
37 | let arr: &[u8; 1] = &[1]; | ||
38 | |||
39 | let a: &[_] = arr; | ||
40 | let b = f(arr); | ||
41 | let c: &[_] = { arr }; | ||
42 | let d = g(S { a: arr }); | ||
43 | let e: [&[_]; 1] = [arr]; | ||
44 | let f: [&[_]; 2] = [arr; 2]; | ||
45 | let g: (&[_], &[_]) = (arr, arr); | ||
46 | } | ||
47 | 36 | ||
48 | #[lang = "sized"] | 37 | fn test2() { |
49 | pub trait Sized {} | 38 | let arr: &[u8; 1] = &[1]; |
50 | #[lang = "unsize"] | ||
51 | pub trait Unsize<T: ?Sized> {} | ||
52 | #[lang = "coerce_unsized"] | ||
53 | pub trait CoerceUnsized<T> {} | ||
54 | 39 | ||
55 | impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {} | 40 | let a: &[_] = arr; |
56 | impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {} | 41 | let b = f(arr); |
57 | "#, | 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); | ||
47 | } | ||
48 | "#, | ||
58 | expect![[r#" | 49 | expect![[r#" |
59 | 30..31 '_': &[T] | 50 | 30..31 '_': &[T] |
60 | 44..55 '{ loop {} }': T | 51 | 44..55 '{ loop {} }': T |
@@ -131,60 +122,52 @@ fn infer_let_stmt_coerce() { | |||
131 | fn infer_custom_coerce_unsized() { | 122 | fn infer_custom_coerce_unsized() { |
132 | check_infer( | 123 | check_infer( |
133 | r#" | 124 | r#" |
134 | struct A<T: ?Sized>(*const T); | 125 | //- minicore: coerce_unsized |
135 | struct B<T: ?Sized>(*const T); | 126 | use core::{marker::Unsize, ops::CoerceUnsized}; |
136 | struct C<T: ?Sized> { inner: *const T } | ||
137 | 127 | ||
138 | impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<B<U>> for B<T> {} | 128 | struct A<T: ?Sized>(*const T); |
139 | impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<C<U>> for C<T> {} | 129 | struct B<T: ?Sized>(*const T); |
130 | struct C<T: ?Sized> { inner: *const T } | ||
140 | 131 | ||
141 | fn foo1<T>(x: A<[T]>) -> A<[T]> { x } | 132 | impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<B<U>> for B<T> {} |
142 | fn foo2<T>(x: B<[T]>) -> B<[T]> { x } | 133 | impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<C<U>> for C<T> {} |
143 | fn foo3<T>(x: C<[T]>) -> C<[T]> { x } | ||
144 | 134 | ||
145 | fn test(a: A<[u8; 2]>, b: B<[u8; 2]>, c: C<[u8; 2]>) { | 135 | fn foo1<T>(x: A<[T]>) -> A<[T]> { x } |
146 | let d = foo1(a); | 136 | fn foo2<T>(x: B<[T]>) -> B<[T]> { x } |
147 | let e = foo2(b); | 137 | fn foo3<T>(x: C<[T]>) -> C<[T]> { x } |
148 | let f = foo3(c); | ||
149 | } | ||
150 | |||
151 | |||
152 | #[lang = "sized"] | ||
153 | pub trait Sized {} | ||
154 | #[lang = "unsize"] | ||
155 | pub trait Unsize<T: ?Sized> {} | ||
156 | #[lang = "coerce_unsized"] | ||
157 | pub trait CoerceUnsized<T> {} | ||
158 | 138 | ||
159 | impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {} | 139 | fn test(a: A<[u8; 2]>, b: B<[u8; 2]>, c: C<[u8; 2]>) { |
160 | impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {} | 140 | let d = foo1(a); |
161 | "#, | 141 | let e = foo2(b); |
142 | let f = foo3(c); | ||
143 | } | ||
144 | "#, | ||
162 | expect![[r#" | 145 | expect![[r#" |
163 | 257..258 'x': A<[T]> | 146 | 306..307 'x': A<[T]> |
164 | 278..283 '{ x }': A<[T]> | 147 | 327..332 '{ x }': A<[T]> |
165 | 280..281 'x': A<[T]> | 148 | 329..330 'x': A<[T]> |
166 | 295..296 'x': B<[T]> | 149 | 344..345 'x': B<[T]> |
167 | 316..321 '{ x }': B<[T]> | 150 | 365..370 '{ x }': B<[T]> |
168 | 318..319 'x': B<[T]> | 151 | 367..368 'x': B<[T]> |
169 | 333..334 'x': C<[T]> | 152 | 382..383 'x': C<[T]> |
170 | 354..359 '{ x }': C<[T]> | 153 | 403..408 '{ x }': C<[T]> |
171 | 356..357 'x': C<[T]> | 154 | 405..406 'x': C<[T]> |
172 | 369..370 'a': A<[u8; 2]> | 155 | 418..419 'a': A<[u8; 2]> |
173 | 384..385 'b': B<[u8; 2]> | 156 | 433..434 'b': B<[u8; 2]> |
174 | 399..400 'c': C<[u8; 2]> | 157 | 448..449 'c': C<[u8; 2]> |
175 | 414..480 '{ ...(c); }': () | 158 | 463..529 '{ ...(c); }': () |
176 | 424..425 'd': A<[{unknown}]> | 159 | 473..474 'd': A<[{unknown}]> |
177 | 428..432 'foo1': fn foo1<{unknown}>(A<[{unknown}]>) -> A<[{unknown}]> | 160 | 477..481 'foo1': fn foo1<{unknown}>(A<[{unknown}]>) -> A<[{unknown}]> |
178 | 428..435 'foo1(a)': A<[{unknown}]> | 161 | 477..484 'foo1(a)': A<[{unknown}]> |
179 | 433..434 'a': A<[u8; 2]> | 162 | 482..483 'a': A<[u8; 2]> |
180 | 445..446 'e': B<[u8]> | 163 | 494..495 'e': B<[u8]> |
181 | 449..453 'foo2': fn foo2<u8>(B<[u8]>) -> B<[u8]> | 164 | 498..502 'foo2': fn foo2<u8>(B<[u8]>) -> B<[u8]> |
182 | 449..456 'foo2(b)': B<[u8]> | 165 | 498..505 'foo2(b)': B<[u8]> |
183 | 454..455 'b': B<[u8; 2]> | 166 | 503..504 'b': B<[u8; 2]> |
184 | 466..467 'f': C<[u8]> | 167 | 515..516 'f': C<[u8]> |
185 | 470..474 'foo3': fn foo3<u8>(C<[u8]>) -> C<[u8]> | 168 | 519..523 'foo3': fn foo3<u8>(C<[u8]>) -> C<[u8]> |
186 | 470..477 'foo3(c)': C<[u8]> | 169 | 519..526 'foo3(c)': C<[u8]> |
187 | 475..476 'c': C<[u8; 2]> | 170 | 524..525 'c': C<[u8; 2]> |
188 | "#]], | 171 | "#]], |
189 | ); | 172 | ); |
190 | } | 173 | } |
@@ -193,21 +176,16 @@ fn infer_custom_coerce_unsized() { | |||
193 | fn infer_if_coerce() { | 176 | fn infer_if_coerce() { |
194 | check_infer( | 177 | check_infer( |
195 | r#" | 178 | r#" |
196 | fn foo<T>(x: &[T]) -> &[T] { loop {} } | 179 | //- minicore: unsize |
197 | fn test() { | 180 | fn foo<T>(x: &[T]) -> &[T] { loop {} } |
198 | let x = if true { | 181 | fn test() { |
199 | foo(&[1]) | 182 | let x = if true { |
200 | } else { | 183 | foo(&[1]) |
201 | &[1] | 184 | } else { |
202 | }; | 185 | &[1] |
203 | } | 186 | }; |
204 | 187 | } | |
205 | 188 | "#, | |
206 | #[lang = "sized"] | ||
207 | pub trait Sized {} | ||
208 | #[lang = "unsize"] | ||
209 | pub trait Unsize<T: ?Sized> {} | ||
210 | "#, | ||
211 | expect![[r#" | 189 | expect![[r#" |
212 | 10..11 'x': &[T] | 190 | 10..11 'x': &[T] |
213 | 27..38 '{ loop {} }': &[T] | 191 | 27..38 '{ loop {} }': &[T] |
@@ -235,25 +213,16 @@ fn infer_if_coerce() { | |||
235 | fn infer_if_else_coerce() { | 213 | fn infer_if_else_coerce() { |
236 | check_infer( | 214 | check_infer( |
237 | r#" | 215 | r#" |
238 | fn foo<T>(x: &[T]) -> &[T] { loop {} } | 216 | //- minicore: coerce_unsized |
239 | fn test() { | 217 | fn foo<T>(x: &[T]) -> &[T] { loop {} } |
240 | let x = if true { | 218 | fn test() { |
241 | &[1] | 219 | let x = if true { |
242 | } else { | 220 | &[1] |
243 | foo(&[1]) | 221 | } else { |
244 | }; | 222 | foo(&[1]) |
245 | } | 223 | }; |
246 | 224 | } | |
247 | #[lang = "sized"] | 225 | "#, |
248 | pub trait Sized {} | ||
249 | #[lang = "unsize"] | ||
250 | pub trait Unsize<T: ?Sized> {} | ||
251 | #[lang = "coerce_unsized"] | ||
252 | pub trait CoerceUnsized<T> {} | ||
253 | |||
254 | impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {} | ||
255 | impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {} | ||
256 | "#, | ||
257 | expect![[r#" | 226 | expect![[r#" |
258 | 10..11 'x': &[T] | 227 | 10..11 'x': &[T] |
259 | 27..38 '{ loop {} }': &[T] | 228 | 27..38 '{ loop {} }': &[T] |
@@ -281,20 +250,16 @@ fn infer_if_else_coerce() { | |||
281 | fn infer_match_first_coerce() { | 250 | fn infer_match_first_coerce() { |
282 | check_infer( | 251 | check_infer( |
283 | r#" | 252 | r#" |
284 | fn foo<T>(x: &[T]) -> &[T] { loop {} } | 253 | //- minicore: unsize |
285 | fn test(i: i32) { | 254 | fn foo<T>(x: &[T]) -> &[T] { loop {} } |
286 | let x = match i { | 255 | fn test(i: i32) { |
287 | 2 => foo(&[2]), | 256 | let x = match i { |
288 | 1 => &[1], | 257 | 2 => foo(&[2]), |
289 | _ => &[3], | 258 | 1 => &[1], |
290 | }; | 259 | _ => &[3], |
291 | } | 260 | }; |
292 | 261 | } | |
293 | #[lang = "sized"] | 262 | "#, |
294 | pub trait Sized {} | ||
295 | #[lang = "unsize"] | ||
296 | pub trait Unsize<T: ?Sized> {} | ||
297 | "#, | ||
298 | expect![[r#" | 263 | expect![[r#" |
299 | 10..11 'x': &[T] | 264 | 10..11 'x': &[T] |
300 | 27..38 '{ loop {} }': &[T] | 265 | 27..38 '{ loop {} }': &[T] |
@@ -329,25 +294,16 @@ fn infer_match_first_coerce() { | |||
329 | fn infer_match_second_coerce() { | 294 | fn infer_match_second_coerce() { |
330 | check_infer( | 295 | check_infer( |
331 | r#" | 296 | r#" |
332 | fn foo<T>(x: &[T]) -> &[T] { loop {} } | 297 | //- minicore: coerce_unsized |
333 | fn test(i: i32) { | 298 | fn foo<T>(x: &[T]) -> &[T] { loop {} } |
334 | let x = match i { | 299 | fn test(i: i32) { |
335 | 1 => &[1], | 300 | let x = match i { |
336 | 2 => foo(&[2]), | 301 | 1 => &[1], |
337 | _ => &[3], | 302 | 2 => foo(&[2]), |
338 | }; | 303 | _ => &[3], |
339 | } | 304 | }; |
340 | 305 | } | |
341 | #[lang = "sized"] | 306 | "#, |
342 | pub trait Sized {} | ||
343 | #[lang = "unsize"] | ||
344 | pub trait Unsize<T: ?Sized> {} | ||
345 | #[lang = "coerce_unsized"] | ||
346 | pub trait CoerceUnsized<T> {} | ||
347 | |||
348 | impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {} | ||
349 | impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {} | ||
350 | "#, | ||
351 | expect![[r#" | 307 | expect![[r#" |
352 | 10..11 'x': &[T] | 308 | 10..11 'x': &[T] |
353 | 27..38 '{ loop {} }': &[T] | 309 | 27..38 '{ loop {} }': &[T] |
@@ -470,15 +426,15 @@ fn coerce_autoderef() { | |||
470 | #[test] | 426 | #[test] |
471 | fn coerce_autoderef_generic() { | 427 | fn coerce_autoderef_generic() { |
472 | check_infer_with_mismatches( | 428 | check_infer_with_mismatches( |
473 | r" | 429 | r#" |
474 | struct Foo; | 430 | struct Foo; |
475 | fn takes_ref<T>(x: &T) -> T { *x } | 431 | fn takes_ref<T>(x: &T) -> T { *x } |
476 | fn test() { | 432 | fn test() { |
477 | takes_ref(&Foo); | 433 | takes_ref(&Foo); |
478 | takes_ref(&&Foo); | 434 | takes_ref(&&Foo); |
479 | takes_ref(&&&Foo); | 435 | takes_ref(&&&Foo); |
480 | } | 436 | } |
481 | ", | 437 | "#, |
482 | expect![[r" | 438 | expect![[r" |
483 | 28..29 'x': &T | 439 | 28..29 'x': &T |
484 | 40..46 '{ *x }': T | 440 | 40..46 '{ *x }': T |
@@ -508,30 +464,29 @@ fn coerce_autoderef_generic() { | |||
508 | fn coerce_autoderef_block() { | 464 | fn coerce_autoderef_block() { |
509 | check_infer_with_mismatches( | 465 | check_infer_with_mismatches( |
510 | r#" | 466 | r#" |
511 | struct String {} | 467 | //- minicore: deref |
512 | #[lang = "deref"] | 468 | struct String {} |
513 | trait Deref { type Target; } | 469 | impl core::ops::Deref for String { type Target = str; } |
514 | impl Deref for String { type Target = str; } | 470 | fn takes_ref_str(x: &str) {} |
515 | fn takes_ref_str(x: &str) {} | 471 | fn returns_string() -> String { loop {} } |
516 | fn returns_string() -> String { loop {} } | 472 | fn test() { |
517 | fn test() { | 473 | takes_ref_str(&{ returns_string() }); |
518 | takes_ref_str(&{ returns_string() }); | 474 | } |
519 | } | 475 | "#, |
520 | "#, | 476 | expect![[r#" |
521 | expect![[r" | 477 | 90..91 'x': &str |
522 | 126..127 'x': &str | 478 | 99..101 '{}': () |
523 | 135..137 '{}': () | 479 | 132..143 '{ loop {} }': String |
524 | 168..179 '{ loop {} }': String | 480 | 134..141 'loop {}': ! |
525 | 170..177 'loop {}': ! | 481 | 139..141 '{}': () |
526 | 175..177 '{}': () | 482 | 154..199 '{ ... }); }': () |
527 | 190..235 '{ ... }); }': () | 483 | 160..173 'takes_ref_str': fn takes_ref_str(&str) |
528 | 196..209 'takes_ref_str': fn takes_ref_str(&str) | 484 | 160..196 'takes_...g() })': () |
529 | 196..232 'takes_...g() })': () | 485 | 174..195 '&{ ret...ng() }': &String |
530 | 210..231 '&{ ret...ng() }': &String | 486 | 175..195 '{ retu...ng() }': String |
531 | 211..231 '{ retu...ng() }': String | 487 | 177..191 'returns_string': fn returns_string() -> String |
532 | 213..227 'returns_string': fn returns_string() -> String | 488 | 177..193 'return...ring()': String |
533 | 213..229 'return...ring()': String | 489 | "#]], |
534 | "]], | ||
535 | ); | 490 | ); |
536 | } | 491 | } |
537 | 492 | ||
@@ -674,25 +629,19 @@ fn coerce_placeholder_ref() { | |||
674 | fn coerce_unsize_array() { | 629 | fn coerce_unsize_array() { |
675 | check_infer_with_mismatches( | 630 | check_infer_with_mismatches( |
676 | r#" | 631 | r#" |
677 | #[lang = "unsize"] | 632 | //- minicore: coerce_unsized |
678 | pub trait Unsize<T> {} | 633 | fn test() { |
679 | #[lang = "coerce_unsized"] | 634 | let f: &[usize] = &[1, 2, 3]; |
680 | pub trait CoerceUnsized<T> {} | 635 | } |
681 | |||
682 | impl<T: Unsize<U>, U> CoerceUnsized<&U> for &T {} | ||
683 | |||
684 | fn test() { | ||
685 | let f: &[usize] = &[1, 2, 3]; | ||
686 | } | ||
687 | "#, | 636 | "#, |
688 | expect![[r#" | 637 | expect![[r#" |
689 | 161..198 '{ ... 3]; }': () | 638 | 10..47 '{ ... 3]; }': () |
690 | 171..172 'f': &[usize] | 639 | 20..21 'f': &[usize] |
691 | 185..195 '&[1, 2, 3]': &[usize; 3] | 640 | 34..44 '&[1, 2, 3]': &[usize; 3] |
692 | 186..195 '[1, 2, 3]': [usize; 3] | 641 | 35..44 '[1, 2, 3]': [usize; 3] |
693 | 187..188 '1': usize | 642 | 36..37 '1': usize |
694 | 190..191 '2': usize | 643 | 39..40 '2': usize |
695 | 193..194 '3': usize | 644 | 42..43 '3': usize |
696 | "#]], | 645 | "#]], |
697 | ); | 646 | ); |
698 | } | 647 | } |
@@ -701,42 +650,34 @@ fn coerce_unsize_array() { | |||
701 | fn coerce_unsize_trait_object_simple() { | 650 | fn coerce_unsize_trait_object_simple() { |
702 | check_infer_with_mismatches( | 651 | check_infer_with_mismatches( |
703 | r#" | 652 | r#" |
704 | #[lang = "sized"] | 653 | //- minicore: coerce_unsized |
705 | pub trait Sized {} | 654 | trait Foo<T, U> {} |
706 | #[lang = "unsize"] | 655 | trait Bar<U, T, X>: Foo<T, U> {} |
707 | pub trait Unsize<T> {} | 656 | trait Baz<T, X>: Bar<usize, T, X> {} |
708 | #[lang = "coerce_unsized"] | 657 | |
709 | pub trait CoerceUnsized<T> {} | 658 | struct S<T, X>; |
710 | 659 | impl<T, X> Foo<T, usize> for S<T, X> {} | |
711 | impl<T: Unsize<U>, U> CoerceUnsized<&U> for &T {} | 660 | impl<T, X> Bar<usize, T, X> for S<T, X> {} |
712 | 661 | impl<T, X> Baz<T, X> for S<T, X> {} | |
713 | trait Foo<T, U> {} | 662 | |
714 | trait Bar<U, T, X>: Foo<T, U> {} | 663 | fn test() { |
715 | trait Baz<T, X>: Bar<usize, T, X> {} | 664 | let obj: &dyn Baz<i8, i16> = &S; |
716 | 665 | let obj: &dyn Bar<_, i8, i16> = &S; | |
717 | struct S<T, X>; | 666 | let obj: &dyn Foo<i8, _> = &S; |
718 | impl<T, X> Foo<T, usize> for S<T, X> {} | 667 | } |
719 | impl<T, X> Bar<usize, T, X> for S<T, X> {} | 668 | "#, |
720 | impl<T, X> Baz<T, X> for S<T, X> {} | 669 | expect![[r#" |
721 | 670 | 236..351 '{ ... &S; }': () | |
722 | fn test() { | 671 | 246..249 'obj': &dyn Baz<i8, i16> |
723 | let obj: &dyn Baz<i8, i16> = &S; | 672 | 271..273 '&S': &S<i8, i16> |
724 | let obj: &dyn Bar<_, i8, i16> = &S; | 673 | 272..273 'S': S<i8, i16> |
725 | let obj: &dyn Foo<i8, _> = &S; | 674 | 283..286 'obj': &dyn Bar<usize, i8, i16> |
726 | } | 675 | 311..313 '&S': &S<i8, i16> |
727 | "#, | 676 | 312..313 'S': S<i8, i16> |
728 | expect![[r" | 677 | 323..326 'obj': &dyn Foo<i8, usize> |
729 | 424..539 '{ ... &S; }': () | 678 | 346..348 '&S': &S<i8, {unknown}> |
730 | 434..437 'obj': &dyn Baz<i8, i16> | 679 | 347..348 'S': S<i8, {unknown}> |
731 | 459..461 '&S': &S<i8, i16> | 680 | "#]], |
732 | 460..461 'S': S<i8, i16> | ||
733 | 471..474 'obj': &dyn Bar<usize, i8, i16> | ||
734 | 499..501 '&S': &S<i8, i16> | ||
735 | 500..501 'S': S<i8, i16> | ||
736 | 511..514 'obj': &dyn Foo<i8, usize> | ||
737 | 534..536 '&S': &S<i8, {unknown}> | ||
738 | 535..536 'S': S<i8, {unknown}> | ||
739 | "]], | ||
740 | ); | 681 | ); |
741 | } | 682 | } |
742 | 683 | ||
@@ -761,49 +702,41 @@ fn coerce_unsize_trait_object_to_trait_object() { | |||
761 | // 602..606 'obj2': &dyn Baz<i8, i16> | 702 | // 602..606 'obj2': &dyn Baz<i8, i16> |
762 | check_infer_with_mismatches( | 703 | check_infer_with_mismatches( |
763 | r#" | 704 | r#" |
764 | #[lang = "sized"] | 705 | //- minicore: coerce_unsized |
765 | pub trait Sized {} | 706 | trait Foo<T, U> {} |
766 | #[lang = "unsize"] | 707 | trait Bar<U, T, X>: Foo<T, U> {} |
767 | pub trait Unsize<T> {} | 708 | trait Baz<T, X>: Bar<usize, T, X> {} |
768 | #[lang = "coerce_unsized"] | 709 | |
769 | pub trait CoerceUnsized<T> {} | 710 | struct S<T, X>; |
770 | 711 | impl<T, X> Foo<T, usize> for S<T, X> {} | |
771 | impl<T: Unsize<U>, U> CoerceUnsized<&U> for &T {} | 712 | impl<T, X> Bar<usize, T, X> for S<T, X> {} |
772 | 713 | impl<T, X> Baz<T, X> for S<T, X> {} | |
773 | trait Foo<T, U> {} | 714 | |
774 | trait Bar<U, T, X>: Foo<T, U> {} | 715 | fn test() { |
775 | trait Baz<T, X>: Bar<usize, T, X> {} | 716 | let obj: &dyn Baz<i8, i16> = &S; |
776 | 717 | let obj: &dyn Bar<_, _, _> = obj; | |
777 | struct S<T, X>; | 718 | let obj: &dyn Foo<_, _> = obj; |
778 | impl<T, X> Foo<T, usize> for S<T, X> {} | 719 | let obj2: &dyn Baz<i8, i16> = &S; |
779 | impl<T, X> Bar<usize, T, X> for S<T, X> {} | 720 | let _: &dyn Foo<_, _> = obj2; |
780 | impl<T, X> Baz<T, X> for S<T, X> {} | 721 | } |
781 | 722 | "#, | |
782 | fn test() { | ||
783 | let obj: &dyn Baz<i8, i16> = &S; | ||
784 | let obj: &dyn Bar<_, _, _> = obj; | ||
785 | let obj: &dyn Foo<_, _> = obj; | ||
786 | let obj2: &dyn Baz<i8, i16> = &S; | ||
787 | let _: &dyn Foo<_, _> = obj2; | ||
788 | } | ||
789 | "#, | ||
790 | expect![[r#" | 723 | expect![[r#" |
791 | 424..609 '{ ...bj2; }': () | 724 | 236..421 '{ ...bj2; }': () |
792 | 434..437 'obj': &dyn Baz<i8, i16> | 725 | 246..249 'obj': &dyn Baz<i8, i16> |
793 | 459..461 '&S': &S<i8, i16> | 726 | 271..273 '&S': &S<i8, i16> |
794 | 460..461 'S': S<i8, i16> | 727 | 272..273 'S': S<i8, i16> |
795 | 471..474 'obj': &dyn Bar<{unknown}, {unknown}, {unknown}> | 728 | 283..286 'obj': &dyn Bar<{unknown}, {unknown}, {unknown}> |
796 | 496..499 'obj': &dyn Baz<i8, i16> | 729 | 308..311 'obj': &dyn Baz<i8, i16> |
797 | 509..512 'obj': &dyn Foo<{unknown}, {unknown}> | 730 | 321..324 'obj': &dyn Foo<{unknown}, {unknown}> |
798 | 531..534 'obj': &dyn Bar<{unknown}, {unknown}, {unknown}> | 731 | 343..346 'obj': &dyn Bar<{unknown}, {unknown}, {unknown}> |
799 | 544..548 'obj2': &dyn Baz<i8, i16> | 732 | 356..360 'obj2': &dyn Baz<i8, i16> |
800 | 570..572 '&S': &S<i8, i16> | 733 | 382..384 '&S': &S<i8, i16> |
801 | 571..572 'S': S<i8, i16> | 734 | 383..384 'S': S<i8, i16> |
802 | 582..583 '_': &dyn Foo<{unknown}, {unknown}> | 735 | 394..395 '_': &dyn Foo<{unknown}, {unknown}> |
803 | 602..606 'obj2': &dyn Baz<i8, i16> | 736 | 414..418 'obj2': &dyn Baz<i8, i16> |
804 | 496..499: expected &dyn Bar<{unknown}, {unknown}, {unknown}>, got &dyn Baz<i8, i16> | 737 | 308..311: expected &dyn Bar<{unknown}, {unknown}, {unknown}>, got &dyn Baz<i8, i16> |
805 | 531..534: expected &dyn Foo<{unknown}, {unknown}>, got &dyn Bar<{unknown}, {unknown}, {unknown}> | 738 | 343..346: expected &dyn Foo<{unknown}, {unknown}>, got &dyn Bar<{unknown}, {unknown}, {unknown}> |
806 | 602..606: expected &dyn Foo<{unknown}, {unknown}>, got &dyn Baz<i8, i16> | 739 | 414..418: expected &dyn Foo<{unknown}, {unknown}>, got &dyn Baz<i8, i16> |
807 | "#]], | 740 | "#]], |
808 | ); | 741 | ); |
809 | } | 742 | } |
@@ -812,40 +745,32 @@ fn coerce_unsize_trait_object_to_trait_object() { | |||
812 | fn coerce_unsize_super_trait_cycle() { | 745 | fn coerce_unsize_super_trait_cycle() { |
813 | check_infer_with_mismatches( | 746 | check_infer_with_mismatches( |
814 | r#" | 747 | r#" |
815 | #[lang = "sized"] | 748 | //- minicore: coerce_unsized |
816 | pub trait Sized {} | 749 | trait A {} |
817 | #[lang = "unsize"] | 750 | trait B: C + A {} |
818 | pub trait Unsize<T> {} | 751 | trait C: B {} |
819 | #[lang = "coerce_unsized"] | 752 | trait D: C |
820 | pub trait CoerceUnsized<T> {} | 753 | |
821 | 754 | struct S; | |
822 | impl<T: Unsize<U>, U> CoerceUnsized<&U> for &T {} | 755 | impl A for S {} |
823 | 756 | impl B for S {} | |
824 | trait A {} | 757 | impl C for S {} |
825 | trait B: C + A {} | 758 | impl D for S {} |
826 | trait C: B {} | 759 | |
827 | trait D: C | 760 | fn test() { |
828 | 761 | let obj: &dyn D = &S; | |
829 | struct S; | 762 | let obj: &dyn A = &S; |
830 | impl A for S {} | 763 | } |
831 | impl B for S {} | 764 | "#, |
832 | impl C for S {} | 765 | expect![[r#" |
833 | impl D for S {} | 766 | 140..195 '{ ... &S; }': () |
834 | 767 | 150..153 'obj': &dyn D | |
835 | fn test() { | 768 | 164..166 '&S': &S |
836 | let obj: &dyn D = &S; | 769 | 165..166 'S': S |
837 | let obj: &dyn A = &S; | 770 | 176..179 'obj': &dyn A |
838 | } | 771 | 190..192 '&S': &S |
839 | "#, | 772 | 191..192 'S': S |
840 | expect![[r" | 773 | "#]], |
841 | 328..383 '{ ... &S; }': () | ||
842 | 338..341 'obj': &dyn D | ||
843 | 352..354 '&S': &S | ||
844 | 353..354 'S': S | ||
845 | 364..367 'obj': &dyn A | ||
846 | 378..380 '&S': &S | ||
847 | 379..380 'S': S | ||
848 | "]], | ||
849 | ); | 774 | ); |
850 | } | 775 | } |
851 | 776 | ||
@@ -854,41 +779,35 @@ fn coerce_unsize_generic() { | |||
854 | // FIXME: fix the type mismatches here | 779 | // FIXME: fix the type mismatches here |
855 | check_infer_with_mismatches( | 780 | check_infer_with_mismatches( |
856 | r#" | 781 | r#" |
857 | #[lang = "unsize"] | 782 | //- minicore: coerce_unsized |
858 | pub trait Unsize<T> {} | 783 | struct Foo<T> { t: T }; |
859 | #[lang = "coerce_unsized"] | 784 | struct Bar<T>(Foo<T>); |
860 | pub trait CoerceUnsized<T> {} | ||
861 | |||
862 | impl<T: Unsize<U>, U> CoerceUnsized<&U> for &T {} | ||
863 | 785 | ||
864 | struct Foo<T> { t: T }; | 786 | fn test() { |
865 | struct Bar<T>(Foo<T>); | 787 | let _: &Foo<[usize]> = &Foo { t: [1, 2, 3] }; |
866 | 788 | let _: &Bar<[usize]> = &Bar(Foo { t: [1, 2, 3] }); | |
867 | fn test() { | 789 | } |
868 | let _: &Foo<[usize]> = &Foo { t: [1, 2, 3] }; | 790 | "#, |
869 | let _: &Bar<[usize]> = &Bar(Foo { t: [1, 2, 3] }); | ||
870 | } | ||
871 | "#, | ||
872 | expect![[r#" | 791 | expect![[r#" |
873 | 209..317 '{ ... }); }': () | 792 | 58..166 '{ ... }); }': () |
874 | 219..220 '_': &Foo<[usize]> | 793 | 68..69 '_': &Foo<[usize]> |
875 | 238..259 '&Foo {..., 3] }': &Foo<[usize]> | 794 | 87..108 '&Foo {..., 3] }': &Foo<[usize]> |
876 | 239..259 'Foo { ..., 3] }': Foo<[usize]> | 795 | 88..108 'Foo { ..., 3] }': Foo<[usize]> |
877 | 248..257 '[1, 2, 3]': [usize; 3] | 796 | 97..106 '[1, 2, 3]': [usize; 3] |
878 | 249..250 '1': usize | 797 | 98..99 '1': usize |
879 | 252..253 '2': usize | 798 | 101..102 '2': usize |
880 | 255..256 '3': usize | 799 | 104..105 '3': usize |
881 | 269..270 '_': &Bar<[usize]> | 800 | 118..119 '_': &Bar<[usize]> |
882 | 288..314 '&Bar(F... 3] })': &Bar<[i32; 3]> | 801 | 137..163 '&Bar(F... 3] })': &Bar<[i32; 3]> |
883 | 289..292 'Bar': Bar<[i32; 3]>(Foo<[i32; 3]>) -> Bar<[i32; 3]> | 802 | 138..141 'Bar': Bar<[i32; 3]>(Foo<[i32; 3]>) -> Bar<[i32; 3]> |
884 | 289..314 'Bar(Fo... 3] })': Bar<[i32; 3]> | 803 | 138..163 'Bar(Fo... 3] })': Bar<[i32; 3]> |
885 | 293..313 'Foo { ..., 3] }': Foo<[i32; 3]> | 804 | 142..162 'Foo { ..., 3] }': Foo<[i32; 3]> |
886 | 302..311 '[1, 2, 3]': [i32; 3] | 805 | 151..160 '[1, 2, 3]': [i32; 3] |
887 | 303..304 '1': i32 | 806 | 152..153 '1': i32 |
888 | 306..307 '2': i32 | 807 | 155..156 '2': i32 |
889 | 309..310 '3': i32 | 808 | 158..159 '3': i32 |
890 | 248..257: expected [usize], got [usize; 3] | 809 | 97..106: expected [usize], got [usize; 3] |
891 | 288..314: expected &Bar<[usize]>, got &Bar<[i32; 3]> | 810 | 137..163: expected &Bar<[usize]>, got &Bar<[i32; 3]> |
892 | "#]], | 811 | "#]], |
893 | ); | 812 | ); |
894 | } | 813 | } |
@@ -898,15 +817,7 @@ fn coerce_unsize_apit() { | |||
898 | // FIXME: #8984 | 817 | // FIXME: #8984 |
899 | check_infer_with_mismatches( | 818 | check_infer_with_mismatches( |
900 | r#" | 819 | r#" |
901 | #[lang = "sized"] | 820 | //- minicore: coerce_unsized |
902 | pub trait Sized {} | ||
903 | #[lang = "unsize"] | ||
904 | pub trait Unsize<T> {} | ||
905 | #[lang = "coerce_unsized"] | ||
906 | pub trait CoerceUnsized<T> {} | ||
907 | |||
908 | impl<T: Unsize<U>, U> CoerceUnsized<&U> for &T {} | ||
909 | |||
910 | trait Foo {} | 821 | trait Foo {} |
911 | 822 | ||
912 | fn test(f: impl Foo) { | 823 | fn test(f: impl Foo) { |
@@ -914,12 +825,12 @@ fn test(f: impl Foo) { | |||
914 | } | 825 | } |
915 | "#, | 826 | "#, |
916 | expect![[r#" | 827 | expect![[r#" |
917 | 210..211 'f': impl Foo | 828 | 22..23 'f': impl Foo |
918 | 223..252 '{ ... &f; }': () | 829 | 35..64 '{ ... &f; }': () |
919 | 233..234 '_': &dyn Foo | 830 | 45..46 '_': &dyn Foo |
920 | 247..249 '&f': &impl Foo | 831 | 59..61 '&f': &impl Foo |
921 | 248..249 'f': impl Foo | 832 | 60..61 'f': impl Foo |
922 | 247..249: expected &dyn Foo, got &impl Foo | 833 | 59..61: expected &dyn Foo, got &impl Foo |
923 | "#]], | 834 | "#]], |
924 | ); | 835 | ); |
925 | } | 836 | } |
@@ -1015,15 +926,7 @@ fn main() { | |||
1015 | fn coerce_unsize_expected_type() { | 926 | fn coerce_unsize_expected_type() { |
1016 | check_no_mismatches( | 927 | check_no_mismatches( |
1017 | r#" | 928 | r#" |
1018 | #[lang = "sized"] | 929 | //- minicore: coerce_unsized |
1019 | pub trait Sized {} | ||
1020 | #[lang = "unsize"] | ||
1021 | pub trait Unsize<T> {} | ||
1022 | #[lang = "coerce_unsized"] | ||
1023 | pub trait CoerceUnsized<T> {} | ||
1024 | |||
1025 | impl<T: Unsize<U>, U> CoerceUnsized<&U> for &T {} | ||
1026 | |||
1027 | fn main() { | 930 | fn main() { |
1028 | let foo: &[u32] = &[1, 2]; | 931 | let foo: &[u32] = &[1, 2]; |
1029 | let foo: &[u32] = match true { | 932 | let foo: &[u32] = match true { |