Skip to content

Commit c62d95a

Browse files
committed
Rust: More type inference tests
1 parent 025f733 commit c62d95a

File tree

3 files changed

+852
-410
lines changed

3 files changed

+852
-410
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
multipleResolvedTargets
22
| main.rs:2723:13:2723:17 | x.f() |
3+
| overloading.rs:269:22:269:30 | x.g(...) |
4+
| overloading.rs:315:9:315:25 | ...::Assoc(...) |
5+
| overloading.rs:316:9:316:26 | ...::Assoc(...) |
6+
| overloading.rs:319:9:319:26 | ...::f(...) |
7+
| overloading.rs:320:9:320:22 | ...::f(...) |

rust/ql/test/library-tests/type-inference/overloading.rs

Lines changed: 183 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,69 @@ mod method_call_trait_path_disambig {
44
fn method(&self) -> bool {
55
true
66
}
7+
8+
fn method2(&self) -> bool;
9+
10+
fn function() -> bool;
711
}
812
trait SecondTrait {
913
// SecondTrait::method
1014
fn method(&self) -> i64 {
1115
1
1216
}
17+
18+
fn method2(&self) -> i64;
1319
}
20+
#[derive(Default)]
1421
struct S;
15-
impl FirstTrait for S {}
16-
impl SecondTrait for S {}
22+
impl FirstTrait for S {
23+
// S::method2
24+
fn method2(&self) -> bool {
25+
true
26+
}
27+
28+
// S::function
29+
fn function() -> bool {
30+
true
31+
}
32+
}
33+
impl SecondTrait for S {
34+
// S::method2
35+
fn method2(&self) -> i64 {
36+
1
37+
}
38+
}
39+
40+
struct S2;
41+
impl FirstTrait for S2 {
42+
// S2::method2
43+
fn method2(&self) -> bool {
44+
false
45+
}
46+
47+
// S2::function
48+
fn function() -> bool {
49+
false
50+
}
51+
}
1752

1853
fn _test() {
1954
let s = S;
2055

2156
let _b1 = FirstTrait::method(&s); // $ type=_b1:bool target=FirstTrait::method
2257
let _b2 = <S as FirstTrait>::method(&s); // $ type=_b2:bool target=FirstTrait::method
58+
let _b3 = <S as FirstTrait>::method(&Default::default()); // $ type=_b3:bool $ MISSING: target=FirstTrait::method target=default
59+
let _b4 = <S as FirstTrait>::method2(&s); // $ type=_b4:bool target=S::method2
60+
let _b5 = <S as FirstTrait>::method2(&Default::default()); // $ type=_b5:bool $ MISSING: target=S::method2 target=default
2361

2462
let _n1 = SecondTrait::method(&s); // $ type=_n1:i64 target=SecondTrait::method
2563
let _n2 = <S as SecondTrait>::method(&s); // $ type=_n2:i64 target=SecondTrait::method
64+
let _n3 = <S as SecondTrait>::method(&Default::default()); // $ type=_n3:i64 $ MISSING: target=SecondTrait::method target=default
65+
let _n4 = <S as SecondTrait>::method2(&s); // $ type=_n4:i64 target=S::method2
66+
let _n5 = <S as SecondTrait>::method2(&Default::default()); // $ type=_n5:i64 $ MISSING: target=S::method2 target=default
67+
68+
<S as FirstTrait>::function(); // $ MISSING: target=S::function
69+
<S2 as FirstTrait>::function(); // $ MISSING: target=S2::function
2670
}
2771
}
2872

@@ -178,3 +222,140 @@ pub mod impl_overlap {
178222
S5::m(&S5(true)); // $ target=MyTrait1::m
179223
}
180224
}
225+
226+
mod impl_overlap2 {
227+
trait Trait1<T1> {
228+
fn f(self, x: T1) -> T1;
229+
}
230+
231+
impl Trait1<i32> for i32 {
232+
// f1
233+
fn f(self, x: i32) -> i32 {
234+
0
235+
}
236+
}
237+
238+
impl Trait1<i64> for i32 {
239+
// f2
240+
fn f(self, x: i64) -> i64 {
241+
0
242+
}
243+
}
244+
245+
trait Trait2<T1, T2> {
246+
fn g(self, x: T1) -> T2;
247+
}
248+
249+
impl Trait2<i32, i32> for i32 {
250+
// g3
251+
fn g(self, x: i32) -> i32 {
252+
0
253+
}
254+
}
255+
256+
impl Trait2<i32, i64> for i32 {
257+
// g4
258+
fn g(self, x: i32) -> i64 {
259+
0
260+
}
261+
}
262+
263+
fn f() {
264+
let x = 0;
265+
let y = x.f(0i32); // $ target=f1
266+
let z: i32 = x.f(Default::default()); // $ MISSING: target=f1 target=default
267+
let z = x.f(0i64); // $ target=f2
268+
let z: i64 = x.f(Default::default()); // $ MISSING: target=f2 target=default
269+
let z: i64 = x.g(0i32); // $ target=g4 $ SPURIOUS: target=g3
270+
}
271+
}
272+
273+
mod impl_overlap3 {
274+
trait Trait {
275+
type Assoc;
276+
277+
fn Assoc() -> Self::Assoc;
278+
}
279+
280+
struct S<T>(T);
281+
282+
impl Trait for S<i32> {
283+
type Assoc = i32;
284+
285+
// S3i32AssocFunc
286+
fn Assoc() -> Self::Assoc {
287+
0
288+
}
289+
}
290+
291+
impl Trait for S<bool> {
292+
type Assoc = bool;
293+
294+
// S3boolAssocFunc
295+
fn Assoc() -> Self::Assoc {
296+
true
297+
}
298+
}
299+
300+
impl S<i32> {
301+
// S3i32f
302+
fn f(x: i32) -> i32 {
303+
0
304+
}
305+
}
306+
307+
impl S<bool> {
308+
// S3boolf
309+
fn f(x: bool) -> bool {
310+
true
311+
}
312+
}
313+
314+
fn f() {
315+
S::<i32>::Assoc(); // $ target=S3i32AssocFunc $ SPURIOUS: target=S3boolAssocFunc
316+
S::<bool>::Assoc(); // $ target=S3boolAssocFunc $ SPURIOUS: target=S3i32AssocFunc
317+
318+
// `S::f(true)` results in "multiple applicable items in scope", even though the argument is actually enough to disambiguate
319+
S::<bool>::f(true); // $ target=S3boolf $ SPURIOUS: target=S3i32f
320+
S::<i32>::f(0); // $ target=S3i32f $ SPURIOUS: target=S3boolf
321+
}
322+
}
323+
324+
mod default_type_args {
325+
struct S<T = i64>(T);
326+
327+
trait MyTrait {
328+
type AssocType;
329+
330+
fn g(self) -> Self::AssocType;
331+
}
332+
333+
impl S {
334+
fn f(self) -> i64 {
335+
self.0 // $ fieldof=S
336+
}
337+
338+
fn g(self) -> i64 {
339+
self.0 // $ fieldof=S
340+
}
341+
}
342+
343+
impl S<bool> {
344+
fn g(self) -> bool {
345+
self.0 // $ fieldof=S
346+
}
347+
}
348+
349+
impl<T> MyTrait for S<T> {
350+
type AssocType = S;
351+
352+
fn g(self) -> S {
353+
let x = S::f(S(Default::default())); // $ type=x:i64 $ MISSING: target=f target=default
354+
let x = Self::AssocType::f(S(Default::default())); // $ target=f target=default type=x:i64
355+
let x = S::<bool>::g(S(Default::default())); // $ target=g target=default type=x:bool
356+
let x = S::<i64>::g(S(Default::default())); // $ target=g target=default type=x:i64
357+
let x = Self::AssocType::g(S(Default::default())); // $ target=g target=default type=x:i64
358+
S(0)
359+
}
360+
}
361+
}

0 commit comments

Comments
 (0)