1 use expect_test::expect;
3 use super::{check_infer, check_types};
6 fn infer_slice_method() {
16 #[lang = "slice_alloc"]
29 140..162 '{ ...(x); }': ()
30 146..156 '<[_]>::foo': fn foo<u8>(&[u8]) -> u8
31 146..159 '<[_]>::foo(x)': u8
38 fn infer_associated_method_struct() {
55 58..68 'A { x: 0 }': A
57 87..121 '{ ...a.x; }': ()
59 101..107 'A::new': fn new() -> A
60 101..109 'A::new()': A
68 fn infer_associated_method_enum() {
93 120..177 '{ ... c; }': ()
95 134..138 'A::b': fn b() -> A
99 161..165 'A::c': fn c() -> A
107 fn infer_associated_method_with_modules() {
112 impl A { pub fn thing() -> A { A {} }}
117 impl B { pub fn thing() -> u32 { 99 }}
121 impl C { pub fn thing() -> C { C {} }}
127 let x = a::A::thing();
128 let y = b::B::thing();
129 let z = c::C::thing();
135 125..131 '{ 99 }': u32
137 201..209 '{ C {} }': C
139 240..324 '{ ...g(); }': ()
141 254..265 'a::A::thing': fn thing() -> A
142 254..267 'a::A::thing()': A
144 281..292 'b::B::thing': fn thing() -> u32
145 281..294 'b::B::thing()': u32
147 308..319 'c::C::thing': fn thing() -> C
148 308..321 'c::C::thing()': C
154 fn infer_associated_method_generics() {
162 pub fn make(val: T) -> Gen<T> {
168 let a = Gen::make(0u32);
173 81..108 '{ ... }': Gen<T>
174 91..102 'Gen { val }': Gen<T>
176 122..154 '{ ...32); }': ()
177 132..133 'a': Gen<u32>
178 136..145 'Gen::make': fn make<u32>(u32) -> Gen<u32>
179 136..151 'Gen::make(0u32)': Gen<u32>
186 fn infer_associated_method_generics_without_args() {
194 pub fn make() -> Gen<T> {
200 let a = Gen::<u32>::make();
204 75..99 '{ ... }': Gen<T>
207 113..148 '{ ...e(); }': ()
208 123..124 'a': Gen<u32>
209 127..143 'Gen::<...::make': fn make<u32>() -> Gen<u32>
210 127..145 'Gen::<...make()': Gen<u32>
216 fn infer_associated_method_generics_2_type_params_without_args() {
224 impl<T> Gen<u32, T> {
225 pub fn make() -> Gen<u32,T> {
231 let a = Gen::<u32, u64>::make();
235 101..125 '{ ... }': Gen<u32, T>
236 111..119 'loop { }': !
238 139..179 '{ ...e(); }': ()
239 149..150 'a': Gen<u32, u64>
240 153..174 'Gen::<...::make': fn make<u64>() -> Gen<u32, u64>
241 153..176 'Gen::<...make()': Gen<u32, u64>
247 fn cross_crate_associated_method_call() {
250 //- /main.rs crate:main deps:other_crate
252 let x = other_crate::foo::S::thing();
256 //- /lib.rs crate:other_crate
260 fn thing() -> i128 {}
268 fn infer_trait_method_simple() {
269 // the trait implementation is intentionally incomplete -- it shouldn't matter
273 fn method(&self) -> u32;
276 impl Trait1 for S1 {}
278 fn method(&self) -> i128;
281 impl Trait2 for S2 {}
283 S1.method(); // -> u32
284 S2.method(); // -> i128
289 109..113 'self': &Self
290 169..227 '{ ...i128 }': ()
292 175..186 'S1.method()': u32
294 202..213 'S2.method()': i128
300 fn infer_trait_method_scoped() {
301 // the trait implementation is intentionally incomplete -- it shouldn't matter
307 fn method(&self) -> u32;
309 impl Trait1 for super::S {}
313 fn method(&self) -> i128;
315 impl Trait2 for super::S {}
320 use super::foo::Trait1;
322 S.method(); // -> u32
328 use super::bar::Trait2;
330 S.method(); // -> i128
336 168..172 'self': &Self
337 299..336 '{ ... }': ()
339 309..319 'S.method()': u32
340 415..453 '{ ... }': ()
342 425..435 'S.method()': i128
348 fn infer_trait_method_generic_1() {
349 // the trait implementation is intentionally incomplete -- it shouldn't matter
353 fn method(&self) -> T;
356 impl Trait<u32> for S {}
363 91..110 '{ ...d(); }': ()
365 97..107 'S.method()': u32
371 fn infer_trait_method_generic_more_params() {
372 // the trait implementation is intentionally incomplete -- it shouldn't matter
375 trait Trait<T1, T2, T3> {
376 fn method1(&self) -> (T1, T2, T3);
377 fn method2(&self) -> (T3, T2, T1);
380 impl Trait<u8, u16, u32> for S1 {}
382 impl<T> Trait<i8, i16, T> for S2 {}
384 S1.method1(); // u8, u16, u32
385 S1.method2(); // u32, u16, u8
386 S2.method1(); // i8, i16, {unknown}
387 S2.method2(); // {unknown}, i16, i8
393 209..360 '{ ..., i8 }': ()
395 215..227 'S1.method1()': (u8, u16, u32)
397 249..261 'S1.method2()': (u32, u16, u8)
399 283..295 'S2.method1()': (i8, i16, {unknown})
401 323..335 'S2.method2()': ({unknown}, i16, i8)
407 fn infer_trait_method_generic_2() {
408 // the trait implementation is intentionally incomplete -- it shouldn't matter
412 fn method(&self) -> T;
415 impl<U> Trait<U> for S<U> {}
422 101..126 '{ ...d(); }': ()
423 107..108 'S': S<u32>(u32) -> S<u32>
424 107..114 'S(1u32)': S<u32>
425 107..123 'S(1u32...thod()': u32
432 fn infer_trait_assoc_method() {
436 fn default() -> Self;
439 impl Default for S {}
441 let s1: S = Default::default();
442 let s2 = S::default();
443 let s3 = <S as Default>::default();
447 86..192 '{ ...t(); }': ()
449 104..120 'Defaul...efault': fn default<S>() -> S
450 104..122 'Defaul...ault()': S
452 137..147 'S::default': fn default<S>() -> S
453 137..149 'S::default()': S
455 164..187 '<S as ...efault': fn default<S>() -> S
456 164..189 '<S as ...ault()': S
462 fn infer_trait_assoc_method_generics_1() {
469 impl Trait<u32> for S {}
471 impl<T> Trait<T> for G<T> {}
474 let b = G::<u64>::make();
475 let c: f64 = G::make();
479 126..210 '{ ...e(); }': ()
481 140..147 'S::make': fn make<S, u32>() -> u32
482 140..149 'S::make()': u32
484 163..177 'G::<u64>::make': fn make<G<u64>, u64>() -> u64
485 163..179 'G::<u6...make()': u64
487 198..205 'G::make': fn make<G<f64>, f64>() -> f64
488 198..207 'G::make()': f64
494 fn infer_trait_assoc_method_generics_2() {
498 fn make<U>() -> (T, U);
501 impl Trait<u32> for S {}
503 impl<T> Trait<T> for G<T> {}
505 let a = S::make::<i64>();
506 let b: (_, i64) = S::make();
507 let c = G::<u32>::make::<i64>();
508 let d: (u32, _) = G::make::<i64>();
509 let e: (u32, i64) = G::make();
513 134..312 '{ ...e(); }': ()
514 144..145 'a': (u32, i64)
515 148..162 'S::make::<i64>': fn make<S, u32, i64>() -> (u32, i64)
516 148..164 'S::mak...i64>()': (u32, i64)
517 174..175 'b': (u32, i64)
518 188..195 'S::make': fn make<S, u32, i64>() -> (u32, i64)
519 188..197 'S::make()': (u32, i64)
520 207..208 'c': (u32, i64)
521 211..232 'G::<u3...:<i64>': fn make<G<u32>, u32, i64>() -> (u32, i64)
522 211..234 'G::<u3...i64>()': (u32, i64)
523 244..245 'd': (u32, i64)
524 258..272 'G::make::<i64>': fn make<G<u32>, u32, i64>() -> (u32, i64)
525 258..274 'G::mak...i64>()': (u32, i64)
526 284..285 'e': (u32, i64)
527 300..307 'G::make': fn make<G<u32>, u32, i64>() -> (u32, i64)
528 300..309 'G::make()': (u32, i64)
534 fn infer_trait_assoc_method_generics_3() {
538 fn make() -> (Self, T);
541 impl Trait<i64> for S<i32> {}
547 100..126 '{ ...e(); }': ()
548 110..111 'a': (S<i32>, i64)
549 114..121 'S::make': fn make<S<i32>, i64>() -> (S<i32>, i64)
550 114..123 'S::make()': (S<i32>, i64)
556 fn infer_trait_assoc_method_generics_4() {
560 fn make() -> (Self, T);
563 impl Trait<i64> for S<u64> {}
564 impl Trait<i32> for S<u32> {}
566 let a: (S<u64>, _) = S::make();
567 let b: (_, i32) = S::make();
571 130..202 '{ ...e(); }': ()
572 140..141 'a': (S<u64>, i64)
573 157..164 'S::make': fn make<S<u64>, i64>() -> (S<u64>, i64)
574 157..166 'S::make()': (S<u64>, i64)
575 176..177 'b': (S<u32>, i32)
576 190..197 'S::make': fn make<S<u32>, i32>() -> (S<u32>, i32)
577 190..199 'S::make()': (S<u32>, i32)
583 fn infer_trait_assoc_method_generics_5() {
587 fn make<U>() -> (Self, T, U);
590 impl Trait<i64> for S<u64> {}
592 let a = <S as Trait<i64>>::make::<u8>();
593 let b: (S<u64>, _, _) = Trait::<i64>::make::<u8>();
597 106..210 '{ ...>(); }': ()
598 116..117 'a': (S<u64>, i64, u8)
599 120..149 '<S as ...::<u8>': fn make<S<u64>, i64, u8>() -> (S<u64>, i64, u8)
600 120..151 '<S as ...<u8>()': (S<u64>, i64, u8)
601 161..162 'b': (S<u64>, i64, u8)
602 181..205 'Trait:...::<u8>': fn make<S<u64>, i64, u8>() -> (S<u64>, i64, u8)
603 181..207 'Trait:...<u8>()': (S<u64>, i64, u8)
609 fn infer_call_trait_method_on_generic_param_1() {
613 fn method(&self) -> u32;
615 fn test<T: Trait>(t: T) {
622 69..88 '{ ...d(); }': ()
624 75..85 't.method()': u32
630 fn infer_call_trait_method_on_generic_param_2() {
634 fn method(&self) -> T;
636 fn test<U, T: Trait<U>>(t: T) {
643 76..95 '{ ...d(); }': ()
645 82..92 't.method()': U
651 fn infer_with_multiple_trait_impls() {
658 impl Into<u32> for S {}
659 impl Into<u64> for S {}
661 let x: u32 = S.into();
662 let y: u64 = S.into();
663 let z = Into::<u64>::into(S);
668 110..201 '{ ...(S); }': ()
671 129..137 'S.into()': u32
674 156..164 'S.into()': u64
676 178..195 'Into::...::into': fn into<S, u64>(S) -> u64
677 178..198 'Into::...nto(S)': u64
684 fn method_resolution_unify_impl_self_type() {
688 impl S<u32> { fn foo(&self) -> u8 {} }
689 impl S<i32> { fn foo(&self) -> i8 {} }
690 fn test() { (S::<u32>.foo(), S::<i32>.foo()); }
697 fn method_resolution_trait_before_autoref() {
700 trait Trait { fn foo(self) -> u128; }
702 impl S { fn foo(&self) -> i8 { 0 } }
703 impl Trait for S { fn foo(self) -> u128 { 0 } }
704 fn test() { S.foo(); }
711 fn method_resolution_by_value_before_autoref() {
714 trait Clone { fn clone(&self) -> Self; }
718 fn test() { (S.clone(), (&S).clone(), (&&S).clone()); }
725 fn method_resolution_trait_before_autoderef() {
728 trait Trait { fn foo(self) -> u128; }
730 impl S { fn foo(self) -> i8 { 0 } }
731 impl Trait for &S { fn foo(self) -> u128 { 0 } }
732 fn test() { (&S).foo(); }
739 fn method_resolution_impl_before_trait() {
742 trait Trait { fn foo(self) -> u128; }
744 impl S { fn foo(self) -> i8 { 0 } }
745 impl Trait for S { fn foo(self) -> u128 { 0 } }
746 fn test() { S.foo(); }
753 fn method_resolution_impl_ref_before_trait() {
756 trait Trait { fn foo(self) -> u128; }
758 impl S { fn foo(&self) -> i8 { 0 } }
759 impl Trait for &S { fn foo(self) -> u128 { 0 } }
760 fn test() { S.foo(); }
767 fn method_resolution_trait_autoderef() {
770 trait Trait { fn foo(self) -> u128; }
772 impl Trait for S { fn foo(self) -> u128 { 0 } }
773 fn test() { (&S).foo(); }
780 fn method_resolution_unsize_array() {
785 fn len(&self) -> usize { loop {} }
796 fn method_resolution_trait_from_prelude() {
799 //- /main.rs crate:main deps:other_crate
808 //- /lib.rs crate:other_crate
809 #[prelude_import] use foo::*;
813 fn clone(&self) -> Self;
821 fn method_resolution_where_clause_for_unknown_trait() {
822 // The blanket impl currently applies because we ignore the unresolved where clause
825 trait Trait { fn foo(self) -> u128; }
827 impl<T> Trait for T where T: UnknownTrait {}
828 fn test() { (&S).foo(); }
835 fn method_resolution_where_clause_not_met() {
836 // The blanket impl shouldn't apply because we can't prove S: Clone
837 // This is also to make sure that we don't resolve to the foo method just
838 // because that's the only method named foo we can find, which would make
839 // the below tests not work
843 trait Trait { fn foo(self) -> u128; }
845 impl<T> Trait for T where T: Clone {}
846 fn test() { (&S).foo(); }
853 fn method_resolution_where_clause_inline_not_met() {
854 // The blanket impl shouldn't apply because we can't prove S: Clone
858 trait Trait { fn foo(self) -> u128; }
860 impl<T: Clone> Trait for T {}
861 fn test() { (&S).foo(); }
868 fn method_resolution_where_clause_1() {
872 trait Trait { fn foo(self) -> u128; }
875 impl<T> Trait for T where T: Clone {}
876 fn test() { S.foo(); }
883 fn method_resolution_where_clause_2() {
886 trait Into<T> { fn into(self) -> T; }
887 trait From<T> { fn from(other: T) -> Self; }
890 impl From<S2> for S1 {}
891 impl<T, U> Into<U> for T where U: From<T> {}
892 fn test() { S2.into(); }
899 fn method_resolution_where_clause_inline() {
902 trait Into<T> { fn into(self) -> T; }
903 trait From<T> { fn from(other: T) -> Self; }
906 impl From<S2> for S1 {}
907 impl<T, U: From<T>> Into<U> for T {}
908 fn test() { S2.into(); }
915 fn method_resolution_overloaded_method() {
916 cov_mark::check!(impl_self_type_match_without_receiver);
919 struct Wrapper<T>(T);
923 impl<T> Wrapper<Foo<T>> {
924 pub fn new(foo_: T) -> Self {
929 impl<T> Wrapper<Bar<T>> {
930 pub fn new(bar_: T) -> Self {
936 let a = Wrapper::<Foo<f32>>::new(1.0);
937 let b = Wrapper::<Bar<f32>>::new(1.0);
939 //^ (Wrapper<Foo<f32>>, Wrapper<Bar<f32>>)
946 fn method_resolution_encountering_fn_type() {
951 trait FnOnce { fn call(self); }
952 fn test() { foo.call(); }
959 fn super_trait_impl_return_trait_method_resolution() {
963 fn foo(self) -> usize;
966 trait Super : Base {}
968 fn base1() -> impl Base { loop {} }
969 fn super1() -> impl Super { loop {} }
971 fn test(base2: impl Base, super2: impl Super) {
980 90..101 '{ loop {} }': !
983 128..139 '{ loop {} }': !
984 130..137 'loop {}': !
986 149..154 'base2': impl Base
987 167..173 'super2': impl Super
988 187..264 '{ ...o(); }': ()
989 193..198 'base1': fn base1() -> impl Base
990 193..200 'base1()': impl Base
991 193..206 'base1().foo()': usize
992 212..218 'super1': fn super1() -> impl Super
993 212..220 'super1()': impl Super
994 212..226 'super1().foo()': usize
995 232..237 'base2': impl Base
996 232..243 'base2.foo()': usize
997 249..255 'super2': impl Super
998 249..261 'super2.foo()': usize
1004 fn method_resolution_non_parameter_type() {
1013 struct Wrapper<T>(T);
1014 fn foo<T>(t: Wrapper<T>)
1025 fn method_resolution_3373() {
1031 fn from(v: i32) -> A<i32> { A(v) }
1042 fn method_resolution_slow() {
1043 // this can get quite slow if we set the solver size limit too high
1048 struct S1; impl SendX for S1 {}
1049 struct S2; impl SendX for S2 {}
1052 trait Trait { fn method(self); }
1055 impl<A, B> SendX for X1<A, B> where A: SendX, B: SendX {}
1061 impl<B, C> Trait for S<B, C> where C: FnX, B: SendX {}
1063 fn test() { (S {}).method(); }
1070 fn dyn_trait_super_trait_not_in_scope() {
1074 pub trait SuperTrait {
1075 fn foo(&self) -> u32 { 0 }
1078 trait Trait: m::SuperTrait {}
1081 impl m::SuperTrait for S {}
1084 fn test(d: &dyn Trait) {
1089 51..55 'self': &Self
1092 176..177 'd': &dyn Trait
1093 191..207 '{ ...o(); }': ()
1094 197..198 'd': &dyn Trait
1095 197..204 'd.foo()': u32
1101 fn method_resolution_foreign_opaque_type() {
1106 fn f() -> &'static S;
1110 fn foo(&self) -> bool {
1116 let s = unsafe { f() };
1122 89..109 '{ ... }': bool
1123 99..103 'true': bool
1124 123..167 '{ ...o(); }': ()
1126 137..151 'unsafe { f() }': &S
1127 144..151 '{ f() }': &S
1128 146..147 'f': fn f() -> &S
1131 157..164 's.foo()': bool
1137 fn method_with_allocator_box_self_type() {
1144 pub fn into_vec<A>(self: Box<Self, A>) { }
1148 let foo: Slice<u32>;
1149 (foo.into_vec()); // we don't actually support arbitrary self types, but we shouldn't crash at least
1156 fn method_on_dyn_impl() {
1163 pub fn dyn_foo(&self) -> u32 {
1169 let f = &42u32 as &dyn Foo;
1178 fn autoderef_visibility_field() {
1184 fn deref(&self) -> &Self::Target;
1187 pub struct Foo(pub char);
1188 pub struct Bar(i32);
1190 pub fn new() -> Self {
1194 impl super::Deref for Bar {
1196 fn deref(&self) -> &Foo {
1203 let x = super::a::Bar::new().0;
1208 67..71 'self': &Self
1209 200..231 '{ ... }': Bar
1210 214..218 'Self': Bar(i32) -> Bar
1211 214..221 'Self(0)': Bar
1213 315..319 'self': &Bar
1214 329..362 '{ ... }': &Foo
1215 343..352 '&Foo('z')': &Foo
1216 344..347 'Foo': Foo(char) -> Foo
1217 344..352 'Foo('z')': Foo
1218 348..351 ''z'': char
1219 392..439 '{ ... }': ()
1221 410..428 'super:...r::new': fn new() -> Bar
1222 410..430 'super:...:new()': Bar
1223 410..432 'super:...ew().0': char
1229 fn autoderef_visibility_method() {
1235 fn deref(&self) -> &Self::Target;
1238 pub struct Foo(pub char);
1240 pub fn mango(&self) -> char {
1244 pub struct Bar(i32);
1246 pub fn new() -> Self {
1249 fn mango(&self) -> i32 {
1253 impl super::Deref for Bar {
1255 fn deref(&self) -> &Foo {
1262 let x = super::a::Bar::new().mango();
1267 67..71 'self': &Self
1268 168..172 'self': &Foo
1269 182..212 '{ ... }': char
1270 196..200 'self': &Foo
1271 196..202 'self.0': char
1272 288..319 '{ ... }': Bar
1273 302..306 'Self': Bar(i32) -> Bar
1274 302..309 'Self(0)': Bar
1276 338..342 'self': &Bar
1277 351..381 '{ ... }': i32
1278 365..369 'self': &Bar
1279 365..371 'self.0': i32
1280 465..469 'self': &Bar
1281 479..512 '{ ... }': &Foo
1282 493..502 '&Foo('z')': &Foo
1283 494..497 'Foo': Foo(char) -> Foo
1284 494..502 'Foo('z')': Foo
1285 498..501 ''z'': char
1286 542..595 '{ ... }': ()
1288 560..578 'super:...r::new': fn new() -> Bar
1289 560..580 'super:...:new()': Bar
1290 560..588 'super:...ango()': char