aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/hir_ty/src/tests/simple.rs507
1 files changed, 252 insertions, 255 deletions
diff --git a/crates/hir_ty/src/tests/simple.rs b/crates/hir_ty/src/tests/simple.rs
index 81d0215cf..2687c6a44 100644
--- a/crates/hir_ty/src/tests/simple.rs
+++ b/crates/hir_ty/src/tests/simple.rs
@@ -175,16 +175,17 @@ fn test() {
175fn infer_basics() { 175fn infer_basics() {
176 check_infer( 176 check_infer(
177 r#" 177 r#"
178 fn test(a: u32, b: isize, c: !, d: &str) { 178fn test(a: u32, b: isize, c: !, d: &str) {
179 a; 179 a;
180 b; 180 b;
181 c; 181 c;
182 d; 182 d;
183 1usize; 183 1usize;
184 1isize; 184 1isize;
185 "test"; 185 "test";
186 1.0f32; 186 1.0f32;
187 }"#, 187}
188"#,
188 expect![[r#" 189 expect![[r#"
189 8..9 'a': u32 190 8..9 'a': u32
190 16..17 'b': isize 191 16..17 'b': isize
@@ -207,15 +208,15 @@ fn infer_basics() {
207fn infer_let() { 208fn infer_let() {
208 check_infer( 209 check_infer(
209 r#" 210 r#"
210 fn test() { 211fn test() {
211 let a = 1isize; 212 let a = 1isize;
212 let b: usize = 1; 213 let b: usize = 1;
213 let c = b; 214 let c = b;
214 let d: u32; 215 let d: u32;
215 let e; 216 let e;
216 let f: i32 = e; 217 let f: i32 = e;
217 } 218}
218 "#, 219"#,
219 expect![[r#" 220 expect![[r#"
220 10..117 '{ ...= e; }': () 221 10..117 '{ ...= e; }': ()
221 20..21 'a': isize 222 20..21 'a': isize
@@ -236,17 +237,17 @@ fn infer_let() {
236fn infer_paths() { 237fn infer_paths() {
237 check_infer( 238 check_infer(
238 r#" 239 r#"
239 fn a() -> u32 { 1 } 240fn a() -> u32 { 1 }
240 241
241 mod b { 242mod b {
242 fn c() -> u32 { 1 } 243 fn c() -> u32 { 1 }
243 } 244}
244 245
245 fn test() { 246fn test() {
246 a(); 247 a();
247 b::c(); 248 b::c();
248 } 249}
249 "#, 250"#,
250 expect![[r#" 251 expect![[r#"
251 14..19 '{ 1 }': u32 252 14..19 '{ 1 }': u32
252 16..17 '1': u32 253 16..17 '1': u32
@@ -265,17 +266,17 @@ fn infer_paths() {
265fn infer_path_type() { 266fn infer_path_type() {
266 check_infer( 267 check_infer(
267 r#" 268 r#"
268 struct S; 269struct S;
269 270
270 impl S { 271impl S {
271 fn foo() -> i32 { 1 } 272 fn foo() -> i32 { 1 }
272 } 273}
273 274
274 fn test() { 275fn test() {
275 S::foo(); 276 S::foo();
276 <S>::foo(); 277 <S>::foo();
277 } 278}
278 "#, 279"#,
279 expect![[r#" 280 expect![[r#"
280 40..45 '{ 1 }': i32 281 40..45 '{ 1 }': i32
281 42..43 '1': i32 282 42..43 '1': i32
@@ -292,21 +293,21 @@ fn infer_path_type() {
292fn infer_struct() { 293fn infer_struct() {
293 check_infer( 294 check_infer(
294 r#" 295 r#"
295 struct A { 296struct A {
296 b: B, 297 b: B,
297 c: C, 298 c: C,
298 } 299}
299 struct B; 300struct B;
300 struct C(usize); 301struct C(usize);
301 302
302 fn test() { 303fn test() {
303 let c = C(1); 304 let c = C(1);
304 B; 305 B;
305 let a: A = A { b: B, c: C(1) }; 306 let a: A = A { b: B, c: C(1) };
306 a.b; 307 a.b;
307 a.c; 308 a.c;
308 } 309}
309 "#, 310"#,
310 expect![[r#" 311 expect![[r#"
311 71..153 '{ ...a.c; }': () 312 71..153 '{ ...a.c; }': ()
312 81..82 'c': C 313 81..82 'c': C
@@ -332,14 +333,15 @@ fn infer_struct() {
332fn infer_enum() { 333fn infer_enum() {
333 check_infer( 334 check_infer(
334 r#" 335 r#"
335 enum E { 336enum E {
336 V1 { field: u32 }, 337 V1 { field: u32 },
337 V2 338 V2
338 } 339}
339 fn test() { 340fn test() {
340 E::V1 { field: 1 }; 341 E::V1 { field: 1 };
341 E::V2; 342 E::V2;
342 }"#, 343}
344"#,
343 expect![[r#" 345 expect![[r#"
344 51..89 '{ ...:V2; }': () 346 51..89 '{ ...:V2; }': ()
345 57..75 'E::V1 ...d: 1 }': E 347 57..75 'E::V1 ...d: 1 }': E
@@ -353,23 +355,23 @@ fn infer_enum() {
353fn infer_union() { 355fn infer_union() {
354 check_infer( 356 check_infer(
355 r#" 357 r#"
356 union MyUnion { 358union MyUnion {
357 foo: u32, 359 foo: u32,
358 bar: f32, 360 bar: f32,
359 } 361}
360 362
361 fn test() { 363fn test() {
362 let u = MyUnion { foo: 0 }; 364 let u = MyUnion { foo: 0 };
363 unsafe { baz(u); } 365 unsafe { baz(u); }
364 let u = MyUnion { bar: 0.0 }; 366 let u = MyUnion { bar: 0.0 };
365 unsafe { baz(u); } 367 unsafe { baz(u); }
366 } 368}
367 369
368 unsafe fn baz(u: MyUnion) { 370unsafe fn baz(u: MyUnion) {
369 let inner = u.foo; 371 let inner = u.foo;
370 let inner = u.bar; 372 let inner = u.bar;
371 } 373}
372 "#, 374"#,
373 expect![[r#" 375 expect![[r#"
374 57..172 '{ ...); } }': () 376 57..172 '{ ...); } }': ()
375 67..68 'u': MyUnion 377 67..68 'u': MyUnion
@@ -404,19 +406,19 @@ fn infer_union() {
404fn infer_refs() { 406fn infer_refs() {
405 check_infer( 407 check_infer(
406 r#" 408 r#"
407 fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) { 409fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) {
408 a; 410 a;
409 *a; 411 *a;
410 &a; 412 &a;
411 &mut a; 413 &mut a;
412 b; 414 b;
413 *b; 415 *b;
414 &b; 416 &b;
415 c; 417 c;
416 *c; 418 *c;
417 d; 419 d;
418 *d; 420 *d;
419 } 421}
420 "#, 422 "#,
421 expect![[r#" 423 expect![[r#"
422 8..9 'a': &u32 424 8..9 'a': &u32
@@ -450,11 +452,11 @@ fn infer_refs() {
450fn infer_raw_ref() { 452fn infer_raw_ref() {
451 check_infer( 453 check_infer(
452 r#" 454 r#"
453 fn test(a: i32) { 455fn test(a: i32) {
454 &raw mut a; 456 &raw mut a;
455 &raw const a; 457 &raw const a;
456 } 458}
457 "#, 459"#,
458 expect![[r#" 460 expect![[r#"
459 8..9 'a': i32 461 8..9 'a': i32
460 16..53 '{ ...t a; }': () 462 16..53 '{ ...t a; }': ()
@@ -524,26 +526,26 @@ h";
524fn infer_unary_op() { 526fn infer_unary_op() {
525 check_infer( 527 check_infer(
526 r#" 528 r#"
527 enum SomeType {} 529enum SomeType {}
528 530
529 fn test(x: SomeType) { 531fn test(x: SomeType) {
530 let b = false; 532 let b = false;
531 let c = !b; 533 let c = !b;
532 let a = 100; 534 let a = 100;
533 let d: i128 = -a; 535 let d: i128 = -a;
534 let e = -100; 536 let e = -100;
535 let f = !!!true; 537 let f = !!!true;
536 let g = !42; 538 let g = !42;
537 let h = !10u32; 539 let h = !10u32;
538 let j = !a; 540 let j = !a;
539 -3.14; 541 -3.14;
540 !3; 542 !3;
541 -x; 543 -x;
542 !x; 544 !x;
543 -"hello"; 545 -"hello";
544 !"hello"; 546 !"hello";
545 } 547}
546 "#, 548"#,
547 expect![[r#" 549 expect![[r#"
548 26..27 'x': SomeType 550 26..27 'x': SomeType
549 39..271 '{ ...lo"; }': () 551 39..271 '{ ...lo"; }': ()
@@ -594,19 +596,19 @@ fn infer_unary_op() {
594fn infer_backwards() { 596fn infer_backwards() {
595 check_infer( 597 check_infer(
596 r#" 598 r#"
597 fn takes_u32(x: u32) {} 599fn takes_u32(x: u32) {}
598 600
599 struct S { i32_field: i32 } 601struct S { i32_field: i32 }
600 602
601 fn test() -> &mut &f64 { 603fn test() -> &mut &f64 {
602 let a = unknown_function(); 604 let a = unknown_function();
603 takes_u32(a); 605 takes_u32(a);
604 let b = unknown_function(); 606 let b = unknown_function();
605 S { i32_field: b }; 607 S { i32_field: b };
606 let c = unknown_function(); 608 let c = unknown_function();
607 &mut &c 609 &mut &c
608 } 610}
609 "#, 611"#,
610 expect![[r#" 612 expect![[r#"
611 13..14 'x': u32 613 13..14 'x': u32
612 21..23 '{}': () 614 21..23 '{}': ()
@@ -636,23 +638,23 @@ fn infer_backwards() {
636fn infer_self() { 638fn infer_self() {
637 check_infer( 639 check_infer(
638 r#" 640 r#"
639 struct S; 641struct S;
640 642
641 impl S { 643impl S {
642 fn test(&self) { 644 fn test(&self) {
643 self; 645 self;
644 } 646 }
645 fn test2(self: &Self) { 647 fn test2(self: &Self) {
646 self; 648 self;
647 } 649 }
648 fn test3() -> Self { 650 fn test3() -> Self {
649 S {} 651 S {}
650 } 652 }
651 fn test4() -> Self { 653 fn test4() -> Self {
652 Self {} 654 Self {}
653 } 655 }
654 } 656}
655 "#, 657"#,
656 expect![[r#" 658 expect![[r#"
657 33..37 'self': &S 659 33..37 'self': &S
658 39..60 '{ ... }': () 660 39..60 '{ ... }': ()
@@ -672,30 +674,30 @@ fn infer_self() {
672fn infer_self_as_path() { 674fn infer_self_as_path() {
673 check_infer( 675 check_infer(
674 r#" 676 r#"
675 struct S1; 677struct S1;
676 struct S2(isize); 678struct S2(isize);
677 enum E { 679enum E {
678 V1, 680 V1,
679 V2(u32), 681 V2(u32),
680 } 682}
681 683
682 impl S1 { 684impl S1 {
683 fn test() { 685 fn test() {
684 Self; 686 Self;
685 } 687 }
686 } 688}
687 impl S2 { 689impl S2 {
688 fn test() { 690 fn test() {
689 Self(1); 691 Self(1);
690 } 692 }
691 } 693}
692 impl E { 694impl E {
693 fn test() { 695 fn test() {
694 Self::V1; 696 Self::V1;
695 Self::V2(1); 697 Self::V2(1);
696 } 698 }
697 } 699}
698 "#, 700"#,
699 expect![[r#" 701 expect![[r#"
700 86..107 '{ ... }': () 702 86..107 '{ ... }': ()
701 96..100 'Self': S1 703 96..100 'Self': S1
@@ -716,26 +718,26 @@ fn infer_self_as_path() {
716fn infer_binary_op() { 718fn infer_binary_op() {
717 check_infer( 719 check_infer(
718 r#" 720 r#"
719 fn f(x: bool) -> i32 { 721fn f(x: bool) -> i32 {
720 0i32 722 0i32
721 } 723}
722 724
723 fn test() -> bool { 725fn test() -> bool {
724 let x = a && b; 726 let x = a && b;
725 let y = true || false; 727 let y = true || false;
726 let z = x == y; 728 let z = x == y;
727 let t = x != y; 729 let t = x != y;
728 let minus_forty: isize = -40isize; 730 let minus_forty: isize = -40isize;
729 let h = minus_forty <= CONST_2; 731 let h = minus_forty <= CONST_2;
730 let c = f(z || y) + 5; 732 let c = f(z || y) + 5;
731 let d = b; 733 let d = b;
732 let g = minus_forty ^= i; 734 let g = minus_forty ^= i;
733 let ten: usize = 10; 735 let ten: usize = 10;
734 let ten_is_eleven = ten == some_num; 736 let ten_is_eleven = ten == some_num;
735 737
736 ten < 3 738 ten < 3
737 } 739}
738 "#, 740"#,
739 expect![[r#" 741 expect![[r#"
740 5..6 'x': bool 742 5..6 'x': bool
741 21..33 '{ 0i32 }': i32 743 21..33 '{ 0i32 }': i32
@@ -795,11 +797,11 @@ fn infer_binary_op() {
795fn infer_shift_op() { 797fn infer_shift_op() {
796 check_infer( 798 check_infer(
797 r#" 799 r#"
798 fn test() { 800fn test() {
799 1u32 << 5u8; 801 1u32 << 5u8;
800 1u32 >> 5u8; 802 1u32 >> 5u8;
801 } 803}
802 "#, 804"#,
803 expect![[r#" 805 expect![[r#"
804 10..47 '{ ...5u8; }': () 806 10..47 '{ ...5u8; }': ()
805 16..20 '1u32': u32 807 16..20 '1u32': u32
@@ -816,29 +818,29 @@ fn infer_shift_op() {
816fn infer_field_autoderef() { 818fn infer_field_autoderef() {
817 check_infer( 819 check_infer(
818 r#" 820 r#"
819 struct A { 821struct A {
820 b: B, 822 b: B,
821 } 823}
822 struct B; 824struct B;
823
824 fn test1(a: A) {
825 let a1 = a;
826 a1.b;
827 let a2 = &a;
828 a2.b;
829 let a3 = &mut a;
830 a3.b;
831 let a4 = &&&&&&&a;
832 a4.b;
833 let a5 = &mut &&mut &&mut a;
834 a5.b;
835 }
836 825
837 fn test2(a1: *const A, a2: *mut A) { 826fn test1(a: A) {
838 a1.b; 827 let a1 = a;
839 a2.b; 828 a1.b;
840 } 829 let a2 = &a;
841 "#, 830 a2.b;
831 let a3 = &mut a;
832 a3.b;
833 let a4 = &&&&&&&a;
834 a4.b;
835 let a5 = &mut &&mut &&mut a;
836 a5.b;
837}
838
839fn test2(a1: *const A, a2: *mut A) {
840 a1.b;
841 a2.b;
842}
843"#,
842 expect![[r#" 844 expect![[r#"
843 43..44 'a': A 845 43..44 'a': A
844 49..212 '{ ...5.b; }': () 846 49..212 '{ ...5.b; }': ()
@@ -891,58 +893,53 @@ fn infer_field_autoderef() {
891fn infer_argument_autoderef() { 893fn infer_argument_autoderef() {
892 check_infer( 894 check_infer(
893 r#" 895 r#"
894 #[lang = "deref"] 896//- minicore: deref
895 pub trait Deref { 897use core::ops::Deref;
896 type Target; 898struct A<T>(T);
897 fn deref(&self) -> &Self::Target;
898 }
899
900 struct A<T>(T);
901 899
902 impl<T> A<T> { 900impl<T> A<T> {
903 fn foo(&self) -> &T { 901 fn foo(&self) -> &T {
904 &self.0 902 &self.0
905 } 903 }
906 } 904}
907 905
908 struct B<T>(T); 906struct B<T>(T);
909 907
910 impl<T> Deref for B<T> { 908impl<T> Deref for B<T> {
911 type Target = T; 909 type Target = T;
912 fn deref(&self) -> &Self::Target { 910 fn deref(&self) -> &Self::Target {
913 &self.0 911 &self.0
914 } 912 }
915 } 913}
916 914
917 fn test() { 915fn test() {
918 let t = A::foo(&&B(B(A(42)))); 916 let t = A::foo(&&B(B(A(42))));
919 } 917}
920 "#, 918"#,
921 expect![[r#" 919 expect![[r#"
922 67..71 'self': &Self 920 66..70 'self': &A<T>
923 138..142 'self': &A<T> 921 78..101 '{ ... }': &T
924 150..173 '{ ... }': &T 922 88..95 '&self.0': &T
925 160..167 '&self.0': &T 923 89..93 'self': &A<T>
926 161..165 'self': &A<T> 924 89..95 'self.0': T
927 161..167 'self.0': T 925 182..186 'self': &B<T>
928 254..258 'self': &B<T> 926 205..228 '{ ... }': &T
929 277..300 '{ ... }': &T 927 215..222 '&self.0': &T
930 287..294 '&self.0': &T 928 216..220 'self': &B<T>
931 288..292 'self': &B<T> 929 216..222 'self.0': T
932 288..294 'self.0': T 930 242..280 '{ ...))); }': ()
933 314..352 '{ ...))); }': () 931 252..253 't': &i32
934 324..325 't': &i32 932 256..262 'A::foo': fn foo<i32>(&A<i32>) -> &i32
935 328..334 'A::foo': fn foo<i32>(&A<i32>) -> &i32 933 256..277 'A::foo...42))))': &i32
936 328..349 'A::foo...42))))': &i32 934 263..276 '&&B(B(A(42)))': &&B<B<A<i32>>>
937 335..348 '&&B(B(A(42)))': &&B<B<A<i32>>> 935 264..276 '&B(B(A(42)))': &B<B<A<i32>>>
938 336..348 '&B(B(A(42)))': &B<B<A<i32>>> 936 265..266 'B': B<B<A<i32>>>(B<A<i32>>) -> B<B<A<i32>>>
939 337..338 'B': B<B<A<i32>>>(B<A<i32>>) -> B<B<A<i32>>> 937 265..276 'B(B(A(42)))': B<B<A<i32>>>
940 337..348 'B(B(A(42)))': B<B<A<i32>>> 938 267..268 'B': B<A<i32>>(A<i32>) -> B<A<i32>>
941 339..340 'B': B<A<i32>>(A<i32>) -> B<A<i32>> 939 267..275 'B(A(42))': B<A<i32>>
942 339..347 'B(A(42))': B<A<i32>> 940 269..270 'A': A<i32>(i32) -> A<i32>
943 341..342 'A': A<i32>(i32) -> A<i32> 941 269..274 'A(42)': A<i32>
944 341..346 'A(42)': A<i32> 942 271..273 '42': i32
945 343..345 '42': i32
946 "#]], 943 "#]],
947 ); 944 );
948} 945}