aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/tests/traits.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/tests/traits.rs')
-rw-r--r--crates/hir_ty/src/tests/traits.rs309
1 files changed, 114 insertions, 195 deletions
diff --git a/crates/hir_ty/src/tests/traits.rs b/crates/hir_ty/src/tests/traits.rs
index 22e0bfc49..279a1354a 100644
--- a/crates/hir_ty/src/tests/traits.rs
+++ b/crates/hir_ty/src/tests/traits.rs
@@ -567,11 +567,11 @@ fn indexing_arrays() {
567fn infer_ops_index() { 567fn infer_ops_index() {
568 check_types( 568 check_types(
569 r#" 569 r#"
570//- /main.rs crate:main deps:std 570//- minicore: index
571struct Bar; 571struct Bar;
572struct Foo; 572struct Foo;
573 573
574impl std::ops::Index<u32> for Bar { 574impl core::ops::Index<u32> for Bar {
575 type Output = Foo; 575 type Output = Foo;
576} 576}
577 577
@@ -580,15 +580,6 @@ fn test() {
580 let b = a[1u32]; 580 let b = a[1u32];
581 b; 581 b;
582} //^ Foo 582} //^ Foo
583
584//- /std.rs crate:std
585#[prelude_import] use ops::*;
586mod ops {
587 #[lang = "index"]
588 pub trait Index<Idx> {
589 type Output;
590 }
591}
592"#, 583"#,
593 ); 584 );
594} 585}
@@ -597,16 +588,16 @@ mod ops {
597fn infer_ops_index_int() { 588fn infer_ops_index_int() {
598 check_types( 589 check_types(
599 r#" 590 r#"
600//- /main.rs crate:main deps:std 591//- minicore: index
601struct Bar; 592struct Bar;
602struct Foo; 593struct Foo;
603 594
604impl std::ops::Index<u32> for Bar { 595impl core::ops::Index<u32> for Bar {
605 type Output = Foo; 596 type Output = Foo;
606} 597}
607 598
608struct Range; 599struct Range;
609impl std::ops::Index<Range> for Bar { 600impl core::ops::Index<Range> for Bar {
610 type Output = Bar; 601 type Output = Bar;
611} 602}
612 603
@@ -616,15 +607,6 @@ fn test() {
616 b; 607 b;
617 //^ Foo 608 //^ Foo
618} 609}
619
620//- /std.rs crate:std
621#[prelude_import] use ops::*;
622mod ops {
623 #[lang = "index"]
624 pub trait Index<Idx> {
625 type Output;
626 }
627}
628"#, 610"#,
629 ); 611 );
630} 612}
@@ -633,25 +615,12 @@ mod ops {
633fn infer_ops_index_autoderef() { 615fn infer_ops_index_autoderef() {
634 check_types( 616 check_types(
635 r#" 617 r#"
636//- /main.rs crate:main deps:std 618//- minicore: index, slice
637fn test() { 619fn test() {
638 let a = &[1u32, 2, 3]; 620 let a = &[1u32, 2, 3];
639 let b = a[1u32]; 621 let b = a[1];
640 b; 622 b;
641} //^ u32 623} //^ u32
642
643//- /std.rs crate:std
644impl<T> ops::Index<u32> for [T] {
645 type Output = T;
646}
647
648#[prelude_import] use ops::*;
649mod ops {
650 #[lang = "index"]
651 pub trait Index<Idx> {
652 type Output;
653 }
654}
655"#, 624"#,
656 ); 625 );
657} 626}
@@ -884,12 +853,9 @@ fn test<T>(t: T) { t.foo(); }
884fn generic_param_env_deref() { 853fn generic_param_env_deref() {
885 check_types( 854 check_types(
886 r#" 855 r#"
887#[lang = "deref"] 856//- minicore: deref
888trait Deref {
889 type Target;
890}
891trait Trait {} 857trait Trait {}
892impl<T> Deref for T where T: Trait { 858impl<T> core::ops::Deref for T where T: Trait {
893 type Target = i128; 859 type Target = i128;
894} 860}
895fn test<T: Trait>(t: T) { (*t); } 861fn test<T: Trait>(t: T) { (*t); }
@@ -1758,20 +1724,7 @@ fn test() {
1758fn fn_trait_deref_with_ty_default() { 1724fn fn_trait_deref_with_ty_default() {
1759 check_infer( 1725 check_infer(
1760 r#" 1726 r#"
1761#[lang = "deref"] 1727//- minicore: deref, fn
1762trait Deref {
1763 type Target;
1764
1765 fn deref(&self) -> &Self::Target;
1766}
1767
1768#[lang="fn_once"]
1769trait FnOnce<Args> {
1770 type Output;
1771
1772 fn call_once(self, args: Args) -> Self::Output;
1773}
1774
1775struct Foo; 1728struct Foo;
1776 1729
1777impl Foo { 1730impl Foo {
@@ -1784,7 +1737,7 @@ impl<T, F> Lazy<T, F> {
1784 pub fn new(f: F) -> Lazy<T, F> {} 1737 pub fn new(f: F) -> Lazy<T, F> {}
1785} 1738}
1786 1739
1787impl<T, F: FnOnce() -> T> Deref for Lazy<T, F> { 1740impl<T, F: FnOnce() -> T> core::ops::Deref for Lazy<T, F> {
1788 type Target = T; 1741 type Target = T;
1789} 1742}
1790 1743
@@ -1798,32 +1751,29 @@ fn test() {
1798 let r2 = lazy2.foo(); 1751 let r2 = lazy2.foo();
1799}"#, 1752}"#,
1800 expect![[r#" 1753 expect![[r#"
1801 64..68 'self': &Self 1754 36..40 'self': &Foo
1802 165..169 'self': Self 1755 51..53 '{}': ()
1803 171..175 'args': Args 1756 131..132 'f': F
1804 239..243 'self': &Foo 1757 151..153 '{}': ()
1805 254..256 '{}': () 1758 251..497 '{ ...o(); }': ()
1806 334..335 'f': F 1759 261..266 'lazy1': Lazy<Foo, || -> Foo>
1807 354..356 '{}': () 1760 283..292 'Lazy::new': fn new<Foo, || -> Foo>(|| -> Foo) -> Lazy<Foo, || -> Foo>
1808 443..689 '{ ...o(); }': () 1761 283..300 'Lazy::...| Foo)': Lazy<Foo, || -> Foo>
1809 453..458 'lazy1': Lazy<Foo, || -> Foo> 1762 293..299 '|| Foo': || -> Foo
1810 475..484 'Lazy::new': fn new<Foo, || -> Foo>(|| -> Foo) -> Lazy<Foo, || -> Foo> 1763 296..299 'Foo': Foo
1811 475..492 'Lazy::...| Foo)': Lazy<Foo, || -> Foo> 1764 310..312 'r1': usize
1812 485..491 '|| Foo': || -> Foo 1765 315..320 'lazy1': Lazy<Foo, || -> Foo>
1813 488..491 'Foo': Foo 1766 315..326 'lazy1.foo()': usize
1814 502..504 'r1': usize 1767 368..383 'make_foo_fn_ptr': fn() -> Foo
1815 507..512 'lazy1': Lazy<Foo, || -> Foo> 1768 399..410 'make_foo_fn': fn make_foo_fn() -> Foo
1816 507..518 'lazy1.foo()': usize 1769 420..425 'lazy2': Lazy<Foo, fn() -> Foo>
1817 560..575 'make_foo_fn_ptr': fn() -> Foo 1770 442..451 'Lazy::new': fn new<Foo, fn() -> Foo>(fn() -> Foo) -> Lazy<Foo, fn() -> Foo>
1818 591..602 'make_foo_fn': fn make_foo_fn() -> Foo 1771 442..468 'Lazy::...n_ptr)': Lazy<Foo, fn() -> Foo>
1819 612..617 'lazy2': Lazy<Foo, fn() -> Foo> 1772 452..467 'make_foo_fn_ptr': fn() -> Foo
1820 634..643 'Lazy::new': fn new<Foo, fn() -> Foo>(fn() -> Foo) -> Lazy<Foo, fn() -> Foo> 1773 478..480 'r2': usize
1821 634..660 'Lazy::...n_ptr)': Lazy<Foo, fn() -> Foo> 1774 483..488 'lazy2': Lazy<Foo, fn() -> Foo>
1822 644..659 'make_foo_fn_ptr': fn() -> Foo 1775 483..494 'lazy2.foo()': usize
1823 670..672 'r2': usize 1776 357..359 '{}': ()
1824 675..680 'lazy2': Lazy<Foo, fn() -> Foo>
1825 675..686 'lazy2.foo()': usize
1826 549..551 '{}': ()
1827 "#]], 1777 "#]],
1828 ); 1778 );
1829} 1779}
@@ -2731,9 +2681,7 @@ fn test(x: &dyn Foo) {
2731fn builtin_copy() { 2681fn builtin_copy() {
2732 check_infer_with_mismatches( 2682 check_infer_with_mismatches(
2733 r#" 2683 r#"
2734#[lang = "copy"] 2684//- minicore: copy
2735trait Copy {}
2736
2737struct IsCopy; 2685struct IsCopy;
2738impl Copy for IsCopy {} 2686impl Copy for IsCopy {}
2739struct NotCopy; 2687struct NotCopy;
@@ -2748,20 +2696,20 @@ fn test() {
2748 (IsCopy, NotCopy).test(); 2696 (IsCopy, NotCopy).test();
2749}"#, 2697}"#,
2750 expect![[r#" 2698 expect![[r#"
2751 110..114 'self': &Self 2699 78..82 'self': &Self
2752 166..267 '{ ...t(); }': () 2700 134..235 '{ ...t(); }': ()
2753 172..178 'IsCopy': IsCopy 2701 140..146 'IsCopy': IsCopy
2754 172..185 'IsCopy.test()': bool 2702 140..153 'IsCopy.test()': bool
2755 191..198 'NotCopy': NotCopy 2703 159..166 'NotCopy': NotCopy
2756 191..205 'NotCopy.test()': {unknown} 2704 159..173 'NotCopy.test()': {unknown}
2757 211..227 '(IsCop...sCopy)': (IsCopy, IsCopy) 2705 179..195 '(IsCop...sCopy)': (IsCopy, IsCopy)
2758 211..234 '(IsCop...test()': bool 2706 179..202 '(IsCop...test()': bool
2759 212..218 'IsCopy': IsCopy 2707 180..186 'IsCopy': IsCopy
2760 220..226 'IsCopy': IsCopy 2708 188..194 'IsCopy': IsCopy
2761 240..257 '(IsCop...tCopy)': (IsCopy, NotCopy) 2709 208..225 '(IsCop...tCopy)': (IsCopy, NotCopy)
2762 240..264 '(IsCop...test()': {unknown} 2710 208..232 '(IsCop...test()': {unknown}
2763 241..247 'IsCopy': IsCopy 2711 209..215 'IsCopy': IsCopy
2764 249..256 'NotCopy': NotCopy 2712 217..224 'NotCopy': NotCopy
2765 "#]], 2713 "#]],
2766 ); 2714 );
2767} 2715}
@@ -2770,9 +2718,7 @@ fn test() {
2770fn builtin_fn_def_copy() { 2718fn builtin_fn_def_copy() {
2771 check_infer_with_mismatches( 2719 check_infer_with_mismatches(
2772 r#" 2720 r#"
2773#[lang = "copy"] 2721//- minicore: copy
2774trait Copy {}
2775
2776fn foo() {} 2722fn foo() {}
2777fn bar<T: Copy>(T) -> T {} 2723fn bar<T: Copy>(T) -> T {}
2778struct Struct(usize); 2724struct Struct(usize);
@@ -2788,20 +2734,20 @@ fn test() {
2788 Enum::Variant.test(); 2734 Enum::Variant.test();
2789}"#, 2735}"#,
2790 expect![[r#" 2736 expect![[r#"
2791 41..43 '{}': () 2737 9..11 '{}': ()
2792 60..61 'T': {unknown} 2738 28..29 'T': {unknown}
2793 68..70 '{}': () 2739 36..38 '{}': ()
2794 68..70: expected T, got () 2740 36..38: expected T, got ()
2795 145..149 'self': &Self 2741 113..117 'self': &Self
2796 201..281 '{ ...t(); }': () 2742 169..249 '{ ...t(); }': ()
2797 207..210 'foo': fn foo() 2743 175..178 'foo': fn foo()
2798 207..217 'foo.test()': bool 2744 175..185 'foo.test()': bool
2799 223..226 'bar': fn bar<{unknown}>({unknown}) -> {unknown} 2745 191..194 'bar': fn bar<{unknown}>({unknown}) -> {unknown}
2800 223..233 'bar.test()': bool 2746 191..201 'bar.test()': bool
2801 239..245 'Struct': Struct(usize) -> Struct 2747 207..213 'Struct': Struct(usize) -> Struct
2802 239..252 'Struct.test()': bool 2748 207..220 'Struct.test()': bool
2803 258..271 'Enum::Variant': Variant(usize) -> Enum 2749 226..239 'Enum::Variant': Variant(usize) -> Enum
2804 258..278 'Enum::...test()': bool 2750 226..246 'Enum::...test()': bool
2805 "#]], 2751 "#]],
2806 ); 2752 );
2807} 2753}
@@ -2810,9 +2756,7 @@ fn test() {
2810fn builtin_fn_ptr_copy() { 2756fn builtin_fn_ptr_copy() {
2811 check_infer_with_mismatches( 2757 check_infer_with_mismatches(
2812 r#" 2758 r#"
2813#[lang = "copy"] 2759//- minicore: copy
2814trait Copy {}
2815
2816trait Test { fn test(&self) -> bool; } 2760trait Test { fn test(&self) -> bool; }
2817impl<T: Copy> Test for T {} 2761impl<T: Copy> Test for T {}
2818 2762
@@ -2822,17 +2766,17 @@ fn test(f1: fn(), f2: fn(usize) -> u8, f3: fn(u8, u8) -> &u8) {
2822 f3.test(); 2766 f3.test();
2823}"#, 2767}"#,
2824 expect![[r#" 2768 expect![[r#"
2825 54..58 'self': &Self 2769 22..26 'self': &Self
2826 108..110 'f1': fn() 2770 76..78 'f1': fn()
2827 118..120 'f2': fn(usize) -> u8 2771 86..88 'f2': fn(usize) -> u8
2828 139..141 'f3': fn(u8, u8) -> &u8 2772 107..109 'f3': fn(u8, u8) -> &u8
2829 162..210 '{ ...t(); }': () 2773 130..178 '{ ...t(); }': ()
2830 168..170 'f1': fn() 2774 136..138 'f1': fn()
2831 168..177 'f1.test()': bool 2775 136..145 'f1.test()': bool
2832 183..185 'f2': fn(usize) -> u8 2776 151..153 'f2': fn(usize) -> u8
2833 183..192 'f2.test()': bool 2777 151..160 'f2.test()': bool
2834 198..200 'f3': fn(u8, u8) -> &u8 2778 166..168 'f3': fn(u8, u8) -> &u8
2835 198..207 'f3.test()': bool 2779 166..175 'f3.test()': bool
2836 "#]], 2780 "#]],
2837 ); 2781 );
2838} 2782}
@@ -2841,9 +2785,7 @@ fn test(f1: fn(), f2: fn(usize) -> u8, f3: fn(u8, u8) -> &u8) {
2841fn builtin_sized() { 2785fn builtin_sized() {
2842 check_infer_with_mismatches( 2786 check_infer_with_mismatches(
2843 r#" 2787 r#"
2844#[lang = "sized"] 2788//- minicore: sized
2845trait Sized {}
2846
2847trait Test { fn test(&self) -> bool; } 2789trait Test { fn test(&self) -> bool; }
2848impl<T: Sized> Test for T {} 2790impl<T: Sized> Test for T {}
2849 2791
@@ -2854,22 +2796,22 @@ fn test() {
2854 (1u8, *"foo").test(); // not Sized 2796 (1u8, *"foo").test(); // not Sized
2855}"#, 2797}"#,
2856 expect![[r#" 2798 expect![[r#"
2857 56..60 'self': &Self 2799 22..26 'self': &Self
2858 113..228 '{ ...ized }': () 2800 79..194 '{ ...ized }': ()
2859 119..122 '1u8': u8 2801 85..88 '1u8': u8
2860 119..129 '1u8.test()': bool 2802 85..95 '1u8.test()': bool
2861 135..150 '(*"foo").test()': {unknown} 2803 101..116 '(*"foo").test()': {unknown}
2862 136..142 '*"foo"': str 2804 102..108 '*"foo"': str
2863 137..142 '"foo"': &str 2805 103..108 '"foo"': &str
2864 169..179 '(1u8, 1u8)': (u8, u8) 2806 135..145 '(1u8, 1u8)': (u8, u8)
2865 169..186 '(1u8, ...test()': bool 2807 135..152 '(1u8, ...test()': bool
2866 170..173 '1u8': u8 2808 136..139 '1u8': u8
2867 175..178 '1u8': u8 2809 141..144 '1u8': u8
2868 192..205 '(1u8, *"foo")': (u8, str) 2810 158..171 '(1u8, *"foo")': (u8, str)
2869 192..212 '(1u8, ...test()': {unknown} 2811 158..178 '(1u8, ...test()': {unknown}
2870 193..196 '1u8': u8 2812 159..162 '1u8': u8
2871 198..204 '*"foo"': str 2813 164..170 '*"foo"': str
2872 199..204 '"foo"': &str 2814 165..170 '"foo"': &str
2873 "#]], 2815 "#]],
2874 ); 2816 );
2875} 2817}
@@ -2980,28 +2922,13 @@ fn infer_box_fn_arg() {
2980 // The type mismatch is because we don't define Unsize and CoerceUnsized 2922 // The type mismatch is because we don't define Unsize and CoerceUnsized
2981 check_infer_with_mismatches( 2923 check_infer_with_mismatches(
2982 r#" 2924 r#"
2983//- /lib.rs deps:std 2925//- minicore: fn, deref, option
2984
2985#[lang = "fn_once"]
2986pub trait FnOnce<Args> {
2987 type Output;
2988
2989 extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
2990}
2991
2992#[lang = "deref"]
2993pub trait Deref {
2994 type Target: ?Sized;
2995
2996 fn deref(&self) -> &Self::Target;
2997}
2998
2999#[lang = "owned_box"] 2926#[lang = "owned_box"]
3000pub struct Box<T: ?Sized> { 2927pub struct Box<T: ?Sized> {
3001 inner: *mut T, 2928 inner: *mut T,
3002} 2929}
3003 2930
3004impl<T: ?Sized> Deref for Box<T> { 2931impl<T: ?Sized> core::ops::Deref for Box<T> {
3005 type Target = T; 2932 type Target = T;
3006 2933
3007 fn deref(&self) -> &T { 2934 fn deref(&self) -> &T {
@@ -3009,38 +2936,30 @@ impl<T: ?Sized> Deref for Box<T> {
3009 } 2936 }
3010} 2937}
3011 2938
3012enum Option<T> {
3013 None,
3014 Some(T)
3015}
3016
3017fn foo() { 2939fn foo() {
3018 let s = Option::None; 2940 let s = None;
3019 let f: Box<dyn FnOnce(&Option<i32>)> = box (|ps| {}); 2941 let f: Box<dyn FnOnce(&Option<i32>)> = box (|ps| {});
3020 f(&s); 2942 f(&s);
3021}"#, 2943}"#,
3022 expect![[r#" 2944 expect![[r#"
3023 100..104 'self': Self 2945 154..158 'self': &Box<T>
3024 106..110 'args': Args 2946 166..193 '{ ... }': &T
3025 214..218 'self': &Self 2947 176..187 '&self.inner': &*mut T
3026 384..388 'self': &Box<T> 2948 177..181 'self': &Box<T>
3027 396..423 '{ ... }': &T 2949 177..187 'self.inner': *mut T
3028 406..417 '&self.inner': &*mut T 2950 206..296 '{ ...&s); }': ()
3029 407..411 'self': &Box<T> 2951 216..217 's': Option<i32>
3030 407..417 'self.inner': *mut T 2952 220..224 'None': Option<i32>
3031 478..576 '{ ...&s); }': () 2953 234..235 'f': Box<dyn FnOnce(&Option<i32>)>
3032 488..489 's': Option<i32> 2954 269..282 'box (|ps| {})': Box<|{unknown}| -> ()>
3033 492..504 'Option::None': Option<i32> 2955 274..281 '|ps| {}': |{unknown}| -> ()
3034 514..515 'f': Box<dyn FnOnce(&Option<i32>)> 2956 275..277 'ps': {unknown}
3035 549..562 'box (|ps| {})': Box<|{unknown}| -> ()> 2957 279..281 '{}': ()
3036 554..561 '|ps| {}': |{unknown}| -> () 2958 288..289 'f': Box<dyn FnOnce(&Option<i32>)>
3037 555..557 'ps': {unknown} 2959 288..293 'f(&s)': ()
3038 559..561 '{}': () 2960 290..292 '&s': &Option<i32>
3039 568..569 'f': Box<dyn FnOnce(&Option<i32>)> 2961 291..292 's': Option<i32>
3040 568..573 'f(&s)': () 2962 269..282: expected Box<dyn FnOnce(&Option<i32>)>, got Box<|{unknown}| -> ()>
3041 570..572 '&s': &Option<i32>
3042 571..572 's': Option<i32>
3043 549..562: expected Box<dyn FnOnce(&Option<i32>)>, got Box<|{unknown}| -> ()>
3044 "#]], 2963 "#]],
3045 ); 2964 );
3046} 2965}