1 use expect_test::expect;
3 use crate::tests::check;
5 use super::{check_infer, check_types};
8 fn infer_slice_method() {
18 #[lang = "slice_alloc"]
31 140..162 '{ ...(x); }': ()
32 146..156 '<[_]>::foo': fn foo<u8>(&[u8]) -> u8
33 146..159 '<[_]>::foo(x)': u8
40 fn infer_array_inherent_impl() {
44 impl<T, const N: usize> [T; N] {
49 fn test(x: &[u8; 0]) {
58 fn infer_associated_method_struct() {
75 58..68 'A { x: 0 }': A
77 87..121 '{ ...a.x; }': ()
79 101..107 'A::new': fn new() -> A
80 101..109 'A::new()': A
88 fn infer_associated_method_struct_in_local_scope() {
95 fn from(_: i32, _: i32) -> Self {
100 let _a = A::from(1, 2);
104 14..146 '{ ... 2); }': ()
106 130..137 'A::from': fn from(i32, i32) -> A
107 130..143 'A::from(1, 2)': A
119 fn infer_associated_method_enum() {
144 120..177 '{ ... c; }': ()
146 134..138 'A::b': fn b() -> A
150 161..165 'A::c': fn c() -> A
158 fn infer_associated_method_with_modules() {
163 impl A { pub fn thing() -> A { A {} }}
168 impl B { pub fn thing() -> u32 { 99 }}
172 impl C { pub fn thing() -> C { C {} }}
178 let x = a::A::thing();
179 let y = b::B::thing();
180 let z = c::C::thing();
186 125..131 '{ 99 }': u32
188 201..209 '{ C {} }': C
190 240..324 '{ ...g(); }': ()
192 254..265 'a::A::thing': fn thing() -> A
193 254..267 'a::A::thing()': A
195 281..292 'b::B::thing': fn thing() -> u32
196 281..294 'b::B::thing()': u32
198 308..319 'c::C::thing': fn thing() -> C
199 308..321 'c::C::thing()': C
205 fn infer_associated_method_generics() {
213 pub fn make(val: T) -> Gen<T> {
219 let a = Gen::make(0u32);
224 81..108 '{ ... }': Gen<T>
225 91..102 'Gen { val }': Gen<T>
227 122..154 '{ ...32); }': ()
228 132..133 'a': Gen<u32>
229 136..145 'Gen::make': fn make<u32>(u32) -> Gen<u32>
230 136..151 'Gen::make(0u32)': Gen<u32>
237 fn infer_associated_method_generics_without_args() {
245 pub fn make() -> Gen<T> {
251 let a = Gen::<u32>::make();
255 75..99 '{ ... }': Gen<T>
258 113..148 '{ ...e(); }': ()
259 123..124 'a': Gen<u32>
260 127..143 'Gen::<...::make': fn make<u32>() -> Gen<u32>
261 127..145 'Gen::<...make()': Gen<u32>
267 fn infer_associated_method_generics_2_type_params_without_args() {
275 impl<T> Gen<u32, T> {
276 pub fn make() -> Gen<u32,T> {
282 let a = Gen::<u32, u64>::make();
286 101..125 '{ ... }': Gen<u32, T>
287 111..119 'loop { }': !
289 139..179 '{ ...e(); }': ()
290 149..150 'a': Gen<u32, u64>
291 153..174 'Gen::<...::make': fn make<u64>() -> Gen<u32, u64>
292 153..176 'Gen::<...make()': Gen<u32, u64>
298 fn cross_crate_associated_method_call() {
301 //- /main.rs crate:main deps:other_crate
303 let x = other_crate::foo::S::thing();
307 //- /lib.rs crate:other_crate
311 fn thing() -> i128 { 0 }
319 fn infer_trait_method_simple() {
320 // the trait implementation is intentionally incomplete -- it shouldn't matter
324 fn method(&self) -> u32;
327 impl Trait1 for S1 {}
329 fn method(&self) -> i128;
332 impl Trait2 for S2 {}
336 S2.method(); // -> i128
344 fn infer_trait_method_scoped() {
345 // the trait implementation is intentionally incomplete -- it shouldn't matter
351 fn method(&self) -> u32;
353 impl Trait1 for super::S {}
357 fn method(&self) -> i128;
359 impl Trait2 for super::S {}
364 use super::foo::Trait1;
373 use super::bar::Trait2;
384 fn infer_trait_method_generic_1() {
385 // the trait implementation is intentionally incomplete -- it shouldn't matter
389 fn method(&self) -> T;
392 impl Trait<u32> for S {}
402 fn infer_trait_method_generic_more_params() {
403 // the trait implementation is intentionally incomplete -- it shouldn't matter
406 trait Trait<T1, T2, T3> {
407 fn method1(&self) -> (T1, T2, T3);
408 fn method2(&self) -> (T3, T2, T1);
411 impl Trait<u8, u16, u32> for S1 {}
413 impl<T> Trait<i8, i16, T> for S2 {}
416 //^^^^^^^^^^^^ (u8, u16, u32)
418 //^^^^^^^^^^^^ (u32, u16, u8)
420 //^^^^^^^^^^^^ (i8, i16, {unknown})
422 //^^^^^^^^^^^^ ({unknown}, i16, i8)
429 fn infer_trait_method_generic_2() {
430 // the trait implementation is intentionally incomplete -- it shouldn't matter
434 fn method(&self) -> T;
437 impl<U> Trait<U> for S<U> {}
440 //^^^^^^^^^^^^^^^^ u32
447 fn infer_trait_assoc_method() {
451 fn default() -> Self;
454 impl Default for S {}
456 let s1: S = Default::default();
457 let s2 = S::default();
458 let s3 = <S as Default>::default();
462 86..192 '{ ...t(); }': ()
464 104..120 'Defaul...efault': fn default<S>() -> S
465 104..122 'Defaul...ault()': S
467 137..147 'S::default': fn default<S>() -> S
468 137..149 'S::default()': S
470 164..187 '<S as ...efault': fn default<S>() -> S
471 164..189 '<S as ...ault()': S
477 fn infer_trait_assoc_method_generics_1() {
484 impl Trait<u32> for S {}
486 impl<T> Trait<T> for G<T> {}
489 let b = G::<u64>::make();
490 let c: f64 = G::make();
494 126..210 '{ ...e(); }': ()
496 140..147 'S::make': fn make<S, u32>() -> u32
497 140..149 'S::make()': u32
499 163..177 'G::<u64>::make': fn make<G<u64>, u64>() -> u64
500 163..179 'G::<u6...make()': u64
502 198..205 'G::make': fn make<G<f64>, f64>() -> f64
503 198..207 'G::make()': f64
509 fn infer_trait_assoc_method_generics_2() {
513 fn make<U>() -> (T, U);
516 impl Trait<u32> for S {}
518 impl<T> Trait<T> for G<T> {}
520 let a = S::make::<i64>();
521 let b: (_, i64) = S::make();
522 let c = G::<u32>::make::<i64>();
523 let d: (u32, _) = G::make::<i64>();
524 let e: (u32, i64) = G::make();
528 134..312 '{ ...e(); }': ()
529 144..145 'a': (u32, i64)
530 148..162 'S::make::<i64>': fn make<S, u32, i64>() -> (u32, i64)
531 148..164 'S::mak...i64>()': (u32, i64)
532 174..175 'b': (u32, i64)
533 188..195 'S::make': fn make<S, u32, i64>() -> (u32, i64)
534 188..197 'S::make()': (u32, i64)
535 207..208 'c': (u32, i64)
536 211..232 'G::<u3...:<i64>': fn make<G<u32>, u32, i64>() -> (u32, i64)
537 211..234 'G::<u3...i64>()': (u32, i64)
538 244..245 'd': (u32, i64)
539 258..272 'G::make::<i64>': fn make<G<u32>, u32, i64>() -> (u32, i64)
540 258..274 'G::mak...i64>()': (u32, i64)
541 284..285 'e': (u32, i64)
542 300..307 'G::make': fn make<G<u32>, u32, i64>() -> (u32, i64)
543 300..309 'G::make()': (u32, i64)
549 fn infer_trait_assoc_method_generics_3() {
553 fn make() -> (Self, T);
556 impl Trait<i64> for S<i32> {}
562 100..126 '{ ...e(); }': ()
563 110..111 'a': (S<i32>, i64)
564 114..121 'S::make': fn make<S<i32>, i64>() -> (S<i32>, i64)
565 114..123 'S::make()': (S<i32>, i64)
571 fn infer_trait_assoc_method_generics_4() {
575 fn make() -> (Self, T);
578 impl Trait<i64> for S<u64> {}
579 impl Trait<i32> for S<u32> {}
581 let a: (S<u64>, _) = S::make();
582 let b: (_, i32) = S::make();
586 130..202 '{ ...e(); }': ()
587 140..141 'a': (S<u64>, i64)
588 157..164 'S::make': fn make<S<u64>, i64>() -> (S<u64>, i64)
589 157..166 'S::make()': (S<u64>, i64)
590 176..177 'b': (S<u32>, i32)
591 190..197 'S::make': fn make<S<u32>, i32>() -> (S<u32>, i32)
592 190..199 'S::make()': (S<u32>, i32)
598 fn infer_trait_assoc_method_generics_5() {
602 fn make<U>() -> (Self, T, U);
605 impl Trait<i64> for S<u64> {}
607 let a = <S as Trait<i64>>::make::<u8>();
608 let b: (S<u64>, _, _) = Trait::<i64>::make::<u8>();
612 106..210 '{ ...>(); }': ()
613 116..117 'a': (S<u64>, i64, u8)
614 120..149 '<S as ...::<u8>': fn make<S<u64>, i64, u8>() -> (S<u64>, i64, u8)
615 120..151 '<S as ...<u8>()': (S<u64>, i64, u8)
616 161..162 'b': (S<u64>, i64, u8)
617 181..205 'Trait:...::<u8>': fn make<S<u64>, i64, u8>() -> (S<u64>, i64, u8)
618 181..207 'Trait:...<u8>()': (S<u64>, i64, u8)
624 fn infer_call_trait_method_on_generic_param_1() {
628 fn method(&self) -> u32;
630 fn test<T: Trait>(t: T) {
637 69..88 '{ ...d(); }': ()
639 75..85 't.method()': u32
645 fn infer_call_trait_method_on_generic_param_2() {
649 fn method(&self) -> T;
651 fn test<U, T: Trait<U>>(t: T) {
658 76..95 '{ ...d(); }': ()
660 82..92 't.method()': U
666 fn infer_with_multiple_trait_impls() {
673 impl Into<u32> for S {}
674 impl Into<u64> for S {}
676 let x: u32 = S.into();
677 let y: u64 = S.into();
678 let z = Into::<u64>::into(S);
683 110..201 '{ ...(S); }': ()
686 129..137 'S.into()': u32
689 156..164 'S.into()': u64
691 178..195 'Into::...::into': fn into<S, u64>(S) -> u64
692 178..198 'Into::...nto(S)': u64
699 fn method_resolution_unify_impl_self_type() {
703 impl S<u32> { fn foo(&self) -> u8 { 0 } }
704 impl S<i32> { fn foo(&self) -> i8 { 0 } }
705 fn test() { (S::<u32>.foo(), S::<i32>.foo()); }
706 //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ (u8, i8)
712 fn method_resolution_trait_before_autoref() {
715 trait Trait { fn foo(self) -> u128; }
717 impl S { fn foo(&self) -> i8 { 0 } }
718 impl Trait for S { fn foo(self) -> u128 { 0 } }
719 fn test() { S.foo(); }
726 fn method_resolution_by_value_before_autoref() {
729 trait Clone { fn clone(&self) -> Self; }
733 fn test() { (S.clone(), (&S).clone(), (&&S).clone()); }
734 //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ (S, S, &S)
740 fn method_resolution_trait_before_autoderef() {
743 trait Trait { fn foo(self) -> u128; }
745 impl S { fn foo(self) -> i8 { 0 } }
746 impl Trait for &S { fn foo(self) -> u128 { 0 } }
747 fn test() { (&S).foo(); }
754 fn method_resolution_impl_before_trait() {
757 trait Trait { fn foo(self) -> u128; }
759 impl S { fn foo(self) -> i8 { 0 } }
760 impl Trait for S { fn foo(self) -> u128 { 0 } }
761 fn test() { S.foo(); }
768 fn method_resolution_impl_ref_before_trait() {
771 trait Trait { fn foo(self) -> u128; }
773 impl S { fn foo(&self) -> i8 { 0 } }
774 impl Trait for &S { fn foo(self) -> u128 { 0 } }
775 fn test() { S.foo(); }
782 fn method_resolution_trait_autoderef() {
785 trait Trait { fn foo(self) -> u128; }
787 impl Trait for S { fn foo(self) -> u128 { 0 } }
788 fn test() { (&S).foo(); }
795 fn method_resolution_unsize_array() {
808 fn method_resolution_trait_from_prelude() {
811 //- /main.rs crate:main deps:core
820 //- /lib.rs crate:core
824 fn clone(&self) -> Self;
833 fn method_resolution_where_clause_for_unknown_trait() {
834 // The blanket impl currently applies because we ignore the unresolved where clause
837 trait Trait { fn foo(self) -> u128; }
839 impl<T> Trait for T where T: UnknownTrait {}
840 fn test() { (&S).foo(); }
847 fn method_resolution_where_clause_not_met() {
848 // The blanket impl shouldn't apply because we can't prove S: Clone
849 // This is also to make sure that we don't resolve to the foo method just
850 // because that's the only method named foo we can find, which would make
851 // the below tests not work
855 trait Trait { fn foo(self) -> u128; }
857 impl<T> Trait for T where T: Clone {}
858 fn test() { (&S).foo(); }
859 //^^^^^^^^^^ {unknown}
865 fn method_resolution_where_clause_inline_not_met() {
866 // The blanket impl shouldn't apply because we can't prove S: Clone
870 trait Trait { fn foo(self) -> u128; }
872 impl<T: Clone> Trait for T {}
873 fn test() { (&S).foo(); }
874 //^^^^^^^^^^ {unknown}
880 fn method_resolution_where_clause_1() {
884 trait Trait { fn foo(self) -> u128; }
887 impl<T> Trait for T where T: Clone {}
888 fn test() { S.foo(); }
895 fn method_resolution_where_clause_2() {
898 trait Into<T> { fn into(self) -> T; }
899 trait From<T> { fn from(other: T) -> Self; }
902 impl From<S2> for S1 {}
903 impl<T, U> Into<U> for T where U: From<T> {}
904 fn test() { S2.into(); }
905 //^^^^^^^^^ {unknown}
911 fn method_resolution_where_clause_inline() {
914 trait Into<T> { fn into(self) -> T; }
915 trait From<T> { fn from(other: T) -> Self; }
918 impl From<S2> for S1 {}
919 impl<T, U: From<T>> Into<U> for T {}
920 fn test() { S2.into(); }
921 //^^^^^^^^^ {unknown}
927 fn method_resolution_overloaded_method() {
928 cov_mark::check!(impl_self_type_match_without_receiver);
931 struct Wrapper<T>(T);
935 impl<T> Wrapper<Foo<T>> {
936 pub fn new(foo_: T) -> Self {
941 impl<T> Wrapper<Bar<T>> {
942 pub fn new(bar_: T) -> Self {
948 let a = Wrapper::<Foo<f32>>::new(1.0);
949 let b = Wrapper::<Bar<f32>>::new(1.0);
951 //^^^^^^ (Wrapper<Foo<f32>>, Wrapper<Bar<f32>>)
958 fn method_resolution_encountering_fn_type() {
963 trait FnOnce { fn call(self); }
964 fn test() { foo.call(); }
965 //^^^^^^^^^^ {unknown}
971 fn super_trait_impl_return_trait_method_resolution() {
976 fn foo(self) -> usize;
979 trait Super : Base {}
981 fn base1() -> impl Base { loop {} }
982 fn super1() -> impl Super { loop {} }
984 fn test(base2: impl Base, super2: impl Super) {
993 90..101 '{ loop {} }': !
996 128..139 '{ loop {} }': !
997 130..137 'loop {}': !
999 149..154 'base2': impl Base
1000 167..173 'super2': impl Super
1001 187..264 '{ ...o(); }': ()
1002 193..198 'base1': fn base1() -> impl Base
1003 193..200 'base1()': impl Base
1004 193..206 'base1().foo()': usize
1005 212..218 'super1': fn super1() -> impl Super
1006 212..220 'super1()': impl Super
1007 212..226 'super1().foo()': usize
1008 232..237 'base2': impl Base
1009 232..243 'base2.foo()': usize
1010 249..255 'super2': impl Super
1011 249..261 'super2.foo()': usize
1017 fn method_resolution_non_parameter_type() {
1026 struct Wrapper<T>(T);
1027 fn foo<T>(t: Wrapper<T>)
1032 } //^^^^^^^ {unknown}
1038 fn method_resolution_3373() {
1044 fn from(v: i32) -> A<i32> { A(v) }
1049 } //^^^^^^^^^^ A<i32>
1055 fn method_resolution_slow() {
1056 // this can get quite slow if we set the solver size limit too high
1061 struct S1; impl SendX for S1 {}
1062 struct S2; impl SendX for S2 {}
1065 trait Trait { fn method(self); }
1068 impl<A, B> SendX for X1<A, B> where A: SendX, B: SendX {}
1074 impl<B, C> Trait for S<B, C> where C: FnX, B: SendX {}
1076 fn test() { (S {}).method(); }
1077 //^^^^^^^^^^^^^^^ ()
1083 fn dyn_trait_super_trait_not_in_scope() {
1087 pub trait SuperTrait {
1088 fn foo(&self) -> u32 { 0 }
1091 trait Trait: m::SuperTrait {}
1094 impl m::SuperTrait for S {}
1097 fn test(d: &dyn Trait) {
1102 51..55 'self': &Self
1105 176..177 'd': &dyn Trait
1106 191..207 '{ ...o(); }': ()
1107 197..198 'd': &dyn Trait
1108 197..204 'd.foo()': u32
1114 fn method_resolution_foreign_opaque_type() {
1119 fn f() -> &'static S;
1123 fn foo(&self) -> bool {
1129 let s = unsafe { f() };
1135 89..109 '{ ... }': bool
1136 99..103 'true': bool
1137 123..167 '{ ...o(); }': ()
1139 137..151 'unsafe { f() }': &S
1140 137..151 'unsafe { f() }': &S
1141 146..147 'f': fn f() -> &S
1144 157..164 's.foo()': bool
1150 fn method_with_allocator_box_self_type() {
1157 pub fn into_vec<A>(self: Box<Self, A>) { }
1161 let foo: Slice<u32>;
1162 foo.into_vec(); // we shouldn't crash on this at least
1163 } //^^^^^^^^^^^^^^ {unknown}
1169 fn method_on_dyn_impl() {
1176 pub fn dyn_foo(&self) -> u32 {
1182 let f = &42u32 as &dyn Foo;
1191 fn autoderef_visibility_field() {
1196 pub struct Foo(pub char);
1197 pub struct Bar(i32);
1199 pub fn new() -> Self {
1203 impl core::ops::Deref for Bar {
1205 fn deref(&self) -> &Foo {
1212 let x = super::a::Bar::new().0;
1213 // ^^^^^^^^^^^^^^^^^^^^ adjustments: Deref(Some(OverloadedDeref(Not)))
1214 // ^^^^^^^^^^^^^^^^^^^^^^ type: char
1222 fn autoderef_visibility_method() {
1223 cov_mark::check!(autoderef_candidate_not_visible);
1228 pub struct Foo(pub char);
1230 pub fn mango(&self) -> char {
1234 pub struct Bar(i32);
1236 pub fn new() -> Self {
1239 fn mango(&self) -> i32 {
1243 impl core::ops::Deref for Bar {
1245 fn deref(&self) -> &Foo {
1252 let x = super::a::Bar::new().mango();
1253 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type: char
1261 fn trait_impl_in_unnamed_const() {
1267 fn method(&self) -> u16;
1283 fn trait_impl_in_synstructure_const() {
1289 fn method(&self) -> u16;
1292 const _DERIVE_Tr_: () = {
1305 fn inherent_impl_in_unnamed_const() {
1312 fn method(&self) -> u16 { 0 }
1314 pub(super) fn super_method(&self) -> u16 { 0 }
1316 pub(crate) fn crate_method(&self) -> u16 { 0 }
1318 pub fn pub_method(&self) -> u16 { 0 }
1327 //^^^^^^^^^^^^^^^^ u16
1330 //^^^^^^^^^^^^^^^^ u16
1333 //^^^^^^^^^^^^^^ u16
1340 fn resolve_const_generic_array_methods() {
1344 impl<T, const N: usize> [T; N] {
1345 pub fn map<F, U>(self, f: F) -> [U; N]
1353 pub fn map<F, U>(self, f: F) -> &[U]
1360 let v = [1, 2].map::<_, usize>(|x| -> x * 2);
1369 fn skip_array_during_method_dispatch() {
1372 //- /main2018.rs crate:main2018 deps:core
1373 use core::IntoIterator;
1376 let v = [4].into_iter();
1380 let a = [0, 1].into_iter();
1385 //- /main2021.rs crate:main2021 deps:core edition:2021
1386 use core::IntoIterator;
1389 let v = [4].into_iter();
1393 let a = [0, 1].into_iter();
1398 //- /core.rs crate:core
1399 #[rustc_skip_array_during_method_dispatch]
1400 pub trait IntoIterator {
1402 fn into_iter(self) -> Self::Out;
1405 impl<T> IntoIterator for [T; 1] {
1407 fn into_iter(self) -> Self::Out { loop {} }
1409 impl<'a, T> IntoIterator for &'a [T] {
1411 fn into_iter(self) -> Self::Out { loop {} }
1418 fn sized_blanket_impl() {
1422 trait Foo { fn foo() -> u8; }
1423 impl<T: Sized> Foo for T {}
1424 fn f<S: Sized, T, U: ?Sized>() {
1433 89..160 '{ ...foo; }': ()
1434 95..103 'u32::foo': fn foo<u32>() -> u8
1435 109..115 'S::foo': fn foo<S>() -> u8
1436 121..127 'T::foo': fn foo<T>() -> u8
1437 133..139 'U::foo': {unknown}
1438 145..157 '<[u32]>::foo': {unknown}
1448 struct SomeStruct(i32);
1451 fn is_even(&self) -> bool {
1456 let o = SomeStruct(3);
1457 let is_even = o.is_even();