]> git.lizzy.rs Git - rust.git/blob - crates/hir_ty/src/tests/coercion.rs
Merge #9954
[rust.git] / crates / hir_ty / src / tests / coercion.rs
1 use super::{check, check_no_mismatches, check_types};
2
3 #[test]
4 fn block_expr_type_mismatch() {
5     // FIXME fix double type mismatch
6     check(
7         r"
8 fn test() {
9     let a: i32 = { 1i64 };
10               // ^^^^^^^^ expected i32, got i64
11                 // ^^^^ expected i32, got i64
12 }
13         ",
14     );
15 }
16
17 #[test]
18 fn coerce_places() {
19     check_no_mismatches(
20         r#"
21 //- minicore: coerce_unsized
22 struct S<T> { a: T }
23
24 fn f<T>(_: &[T]) -> T { loop {} }
25 fn g<T>(_: S<&[T]>) -> T { loop {} }
26
27 fn gen<T>() -> *mut [T; 2] { loop {} }
28 fn test1<U>() -> *mut [U] {
29     gen()
30 }
31
32 fn test2() {
33     let arr: &[u8; 1] = &[1];
34
35     let a: &[_] = arr;
36     let b = f(arr);
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);
42 }
43 "#,
44     );
45 }
46
47 #[test]
48 fn let_stmt_coerce() {
49     check(
50         r"
51 //- minicore: coerce_unsized
52 fn test() {
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)
57 }
58 ",
59     );
60 }
61
62 #[test]
63 fn custom_coerce_unsized() {
64     check(
65         r#"
66 //- minicore: coerce_unsized
67 use core::{marker::Unsize, ops::CoerceUnsized};
68
69 struct A<T: ?Sized>(*const T);
70 struct B<T: ?Sized>(*const T);
71 struct C<T: ?Sized> { inner: *const T }
72
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> {}
75
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 }
79
80 fn test(a: A<[u8; 2]>, b: B<[u8; 2]>, c: C<[u8; 2]>) {
81     let d = foo1(a);
82               // ^ expected A<[{unknown}]>, got A<[u8; 2]>
83     let e = foo2(b);
84      // ^ type: B<[u8]>
85     let f = foo3(c);
86      // ^ type: C<[u8]>
87 }
88 "#,
89     );
90 }
91
92 #[test]
93 fn if_coerce() {
94     check_no_mismatches(
95         r#"
96 //- minicore: coerce_unsized
97 fn foo<T>(x: &[T]) -> &[T] { x }
98 fn test() {
99     let x = if true {
100         foo(&[1])
101          // ^^^^ adjustments: Deref(None), Borrow(Ref(Not)), Pointer(Unsize)
102     } else {
103         &[1]
104     };
105 }
106 "#,
107     );
108 }
109
110 #[test]
111 fn if_else_coerce() {
112     check_no_mismatches(
113         r#"
114 //- minicore: coerce_unsized
115 fn foo<T>(x: &[T]) -> &[T] { x }
116 fn test() {
117     let x = if true {
118         &[1]
119     } else {
120         foo(&[1])
121     };
122 }
123 "#,
124     )
125 }
126
127 #[test]
128 fn match_first_coerce() {
129     check_no_mismatches(
130         r#"
131 //- minicore: coerce_unsized
132 fn foo<T>(x: &[T]) -> &[T] { x }
133 fn test(i: i32) {
134     let x = match i {
135         2 => foo(&[2]),
136               // ^^^^ adjustments: Deref(None), Borrow(Ref(Not)), Pointer(Unsize)
137         1 => &[1],
138         _ => &[3],
139     };
140 }
141 "#,
142     );
143 }
144
145 #[test]
146 fn match_second_coerce() {
147     check_no_mismatches(
148         r#"
149 //- minicore: coerce_unsized
150 fn foo<T>(x: &[T]) -> &[T] { loop {} }
151                           // ^^^^^^^ adjustments: NeverToAny
152 fn test(i: i32) {
153     let x = match i {
154         1 => &[1],
155         2 => foo(&[2]),
156         _ => &[3],
157     };
158 }
159 "#,
160     );
161 }
162
163 #[test]
164 fn coerce_merge_one_by_one1() {
165     cov_mark::check!(coerce_merge_fail_fallback);
166
167     check(
168         r"
169 fn test() {
170     let t = &mut 1;
171     let x = match 1 {
172         1 => t as *mut i32,
173         2 => t as &i32,
174            //^^^^^^^^^ expected *mut i32, got &i32
175         _ => t as *const i32,
176           // ^^^^^^^^^^^^^^^ adjustments: Pointer(MutToConstPointer)
177
178     };
179     x;
180   //^ type: *const i32
181
182 }
183         ",
184     );
185 }
186
187 #[test]
188 fn return_coerce_unknown() {
189     check_types(
190         r"
191 fn foo() -> u32 {
192     return unknown;
193          //^^^^^^^ u32
194 }
195         ",
196     );
197 }
198
199 #[test]
200 fn coerce_autoderef() {
201     check_no_mismatches(
202         r"
203 struct Foo;
204 fn takes_ref_foo(x: &Foo) {}
205 fn test() {
206     takes_ref_foo(&Foo);
207     takes_ref_foo(&&Foo);
208     takes_ref_foo(&&&Foo);
209 }",
210     );
211 }
212
213 #[test]
214 fn coerce_autoderef_generic() {
215     check_no_mismatches(
216         r#"
217 struct Foo;
218 fn takes_ref<T>(x: &T) -> T { *x }
219 fn test() {
220     takes_ref(&Foo);
221     takes_ref(&&Foo);
222     takes_ref(&&&Foo);
223 }
224 "#,
225     );
226 }
227
228 #[test]
229 fn coerce_autoderef_block() {
230     check_no_mismatches(
231         r#"
232 //- minicore: deref
233 struct String {}
234 impl core::ops::Deref for String { type Target = str; }
235 fn takes_ref_str(x: &str) {}
236 fn returns_string() -> String { loop {} }
237 fn test() {
238     takes_ref_str(&{ returns_string() });
239                // ^^^^^^^^^^^^^^^^^^^^^ adjustments: Deref(None), Deref(Some(OverloadedDeref(Not))), Borrow(Ref(Not))
240 }
241 "#,
242     );
243 }
244
245 #[test]
246 fn closure_return_coerce() {
247     check_no_mismatches(
248         r"
249 fn foo() {
250     let x = || {
251         if true {
252             return &1u32;
253         }
254         &&1u32
255     };
256 }",
257     );
258 }
259
260 #[test]
261 fn assign_coerce() {
262     check_no_mismatches(
263         r"
264 //- minicore: deref
265 struct String;
266 impl core::ops::Deref for String { type Target = str; }
267 fn g(_text: &str) {}
268 fn f(text: &str) {
269     let mut text = text;
270     let tmp = String;
271     text = &tmp;
272     g(text);
273 }
274 ",
275     );
276 }
277
278 #[test]
279 fn coerce_fn_item_to_fn_ptr() {
280     check_no_mismatches(
281         r"
282 fn foo(x: u32) -> isize { 1 }
283 fn test() {
284     let f: fn(u32) -> isize = foo;
285                            // ^^^ adjustments: Pointer(ReifyFnPointer)
286     let f: unsafe fn(u32) -> isize = foo;
287                                   // ^^^ adjustments: Pointer(ReifyFnPointer)
288 }",
289     );
290 }
291
292 #[test]
293 fn coerce_fn_items_in_match_arms() {
294     cov_mark::check!(coerce_fn_reification);
295
296     check_types(
297         r"
298 fn foo1(x: u32) -> isize { 1 }
299 fn foo2(x: u32) -> isize { 2 }
300 fn foo3(x: u32) -> isize { 3 }
301 fn test() {
302     let x = match 1 {
303         1 => foo1,
304         2 => foo2,
305         _ => foo3,
306     };
307     x;
308   //^ fn(u32) -> isize
309 }",
310     );
311 }
312
313 #[test]
314 fn coerce_closure_to_fn_ptr() {
315     check_no_mismatches(
316         r"
317 fn test() {
318     let f: fn(u32) -> isize = |x| { 1 };
319 }",
320     );
321 }
322
323 #[test]
324 fn coerce_placeholder_ref() {
325     // placeholders should unify, even behind references
326     check_no_mismatches(
327         r"
328 struct S<T> { t: T }
329 impl<TT> S<TT> {
330     fn get(&self) -> &TT {
331         &self.t
332     }
333 }",
334     );
335 }
336
337 #[test]
338 fn coerce_unsize_array() {
339     check_types(
340         r#"
341 //- minicore: coerce_unsized
342 fn test() {
343     let f: &[usize] = &[1, 2, 3];
344                       //^ usize
345 }"#,
346     );
347 }
348
349 #[test]
350 fn coerce_unsize_trait_object_simple() {
351     check_types(
352         r#"
353 //- minicore: coerce_unsized
354 trait Foo<T, U> {}
355 trait Bar<U, T, X>: Foo<T, U> {}
356 trait Baz<T, X>: Bar<usize, T, X> {}
357
358 struct S<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> {}
362
363 fn test() {
364     let obj: &dyn Baz<i8, i16> = &S;
365                                 //^ S<i8, i16>
366     let obj: &dyn Bar<_, i8, i16> = &S;
367                                    //^ S<i8, i16>
368     let obj: &dyn Foo<i8, _> = &S;
369                               //^ S<i8, {unknown}>
370 }"#,
371     );
372 }
373
374 #[test]
375 fn coerce_unsize_super_trait_cycle() {
376     check_no_mismatches(
377         r#"
378 //- minicore: coerce_unsized
379 trait A {}
380 trait B: C + A {}
381 trait C: B {}
382 trait D: C
383
384 struct S;
385 impl A for S {}
386 impl B for S {}
387 impl C for S {}
388 impl D for S {}
389
390 fn test() {
391     let obj: &dyn D = &S;
392     let obj: &dyn A = &S;
393 }
394 "#,
395     );
396 }
397
398 #[test]
399 fn coerce_unsize_generic() {
400     // FIXME: fix the type mismatches here
401     check(
402         r#"
403 //- minicore: coerce_unsized
404 struct Foo<T> { t: T };
405 struct Bar<T>(Foo<T>);
406
407 fn test() {
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]
412 }
413 "#,
414     );
415 }
416
417 #[test]
418 fn coerce_unsize_apit() {
419     check(
420         r#"
421 //- minicore: coerce_unsized
422 trait Foo {}
423
424 fn test(f: impl Foo, g: &(impl Foo + ?Sized)) {
425     let _: &dyn Foo = &f;
426     let _: &dyn Foo = g;
427                     //^ expected &dyn Foo, got &impl Foo + ?Sized
428 }
429         "#,
430     );
431 }
432
433 #[test]
434 fn two_closures_lub() {
435     check_types(
436         r#"
437 fn foo(c: i32) {
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
443 }
444         "#,
445     )
446 }
447
448 #[test]
449 fn match_diverging_branch_1() {
450     check_types(
451         r#"
452 enum Result<T> { Ok(T), Err }
453 fn parse<T>() -> T { loop {} }
454
455 fn test() -> i32 {
456     let a = match parse() {
457         Ok(val) => val,
458         Err => return 0,
459     };
460     a
461   //^ i32
462 }
463         "#,
464     )
465 }
466
467 #[test]
468 fn match_diverging_branch_2() {
469     // same as 1 except for order of branches
470     check_types(
471         r#"
472 enum Result<T> { Ok(T), Err }
473 fn parse<T>() -> T { loop {} }
474
475 fn test() -> i32 {
476     let a = match parse() {
477         Err => return 0,
478         Ok(val) => val,
479     };
480     a
481   //^ i32
482 }
483         "#,
484     )
485 }
486
487 #[test]
488 fn panic_macro() {
489     check_no_mismatches(
490         r#"
491 mod panic {
492     #[macro_export]
493     pub macro panic_2015 {
494         () => (
495             $crate::panicking::panic()
496         ),
497     }
498 }
499
500 mod panicking {
501     pub fn panic() -> ! { loop {} }
502 }
503
504 #[rustc_builtin_macro = "core_panic"]
505 macro_rules! panic {
506     // Expands to either `$crate::panic::panic_2015` or `$crate::panic::panic_2021`
507     // depending on the edition of the caller.
508     ($($arg:tt)*) => {
509         /* compiler built-in */
510     };
511 }
512
513 fn main() {
514     panic!()
515 }
516         "#,
517     );
518 }
519
520 #[test]
521 fn coerce_unsize_expected_type_1() {
522     check_no_mismatches(
523         r#"
524 //- minicore: coerce_unsized
525 fn main() {
526     let foo: &[u32] = &[1, 2];
527     let foo: &[u32] = match true {
528         true => &[1, 2],
529         false => &[1, 2, 3],
530     };
531     let foo: &[u32] = if true {
532         &[1, 2]
533     } else {
534         &[1, 2, 3]
535     };
536 }
537         "#,
538     );
539 }
540
541 #[test]
542 fn coerce_unsize_expected_type_2() {
543     check_no_mismatches(
544         r#"
545 //- minicore: coerce_unsized
546 struct InFile<T>;
547 impl<T> InFile<T> {
548     fn with_value<U>(self, value: U) -> InFile<U> { InFile }
549 }
550 struct RecordField;
551 trait AstNode {}
552 impl AstNode for RecordField {}
553
554 fn takes_dyn(it: InFile<&dyn AstNode>) {}
555
556 fn test() {
557     let x: InFile<()> = InFile;
558     let n = &RecordField;
559     takes_dyn(x.with_value(n));
560 }
561         "#,
562     );
563 }
564
565 #[test]
566 fn coerce_unsize_expected_type_3() {
567     check_no_mismatches(
568         r#"
569 //- minicore: coerce_unsized
570 enum Option<T> { Some(T), None }
571 struct RecordField;
572 trait AstNode {}
573 impl AstNode for RecordField {}
574
575 fn takes_dyn(it: Option<&dyn AstNode>) {}
576
577 fn test() {
578     let x: InFile<()> = InFile;
579     let n = &RecordField;
580     takes_dyn(Option::Some(n));
581 }
582         "#,
583     );
584 }
585
586 #[test]
587 fn coerce_unsize_expected_type_4() {
588     check_no_mismatches(
589         r#"
590 //- minicore: coerce_unsized
591 use core::{marker::Unsize, ops::CoerceUnsized};
592
593 struct B<T: ?Sized>(*const T);
594 impl<T: ?Sized> B<T> {
595     fn new(t: T) -> Self { B(&t) }
596 }
597
598 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<B<U>> for B<T> {}
599
600 fn test() {
601     let _: B<[isize]> = B::new({ [1, 2, 3] });
602 }
603         "#,
604     );
605 }
606
607 #[test]
608 fn coerce_array_elems_lub() {
609     check_no_mismatches(
610         r#"
611 fn f() {}
612 fn g() {}
613
614 fn test() {
615     [f, g];
616 }
617         "#,
618     );
619 }
620
621 #[test]
622 fn coerce_type_var() {
623     check_types(
624         r#"
625 //- minicore: from, coerce_unsized
626 fn test() {
627     let x = ();
628     let _: &() = &x.into();
629 }               //^^^^^^^^ ()
630 "#,
631     )
632 }
633
634 #[test]
635 fn coerce_overloaded_binary_op_rhs() {
636     check_types(
637         r#"
638 //- minicore: deref, add
639
640 struct String {}
641 impl core::ops::Deref for String { type Target = str; }
642
643 impl core::ops::Add<&str> for String {
644     type Output = String;
645 }
646
647 fn test() {
648     let s1 = String {};
649     let s2 = String {};
650     s1 + &s2;
651   //^^^^^^^^ String
652 }
653
654         "#,
655     );
656 }