Skip to content

Commit 9eee802

Browse files
committed
Rust: Add type inference test
1 parent f2e7dca commit 9eee802

File tree

2 files changed

+218
-0
lines changed

2 files changed

+218
-0
lines changed

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,75 @@ mod from_default {
400400
x
401401
}
402402
}
403+
404+
mod trait_bound_impl_overlap {
405+
trait MyTrait<T> {
406+
fn f(&self) -> T;
407+
}
408+
409+
trait MyTrait2<T = Self> {
410+
type Output;
411+
412+
fn f(&self, x: T) -> Self::Output;
413+
}
414+
415+
struct S<T>(T);
416+
417+
impl MyTrait<i32> for S<i32> {
418+
fn f(&self) -> i32 {
419+
0
420+
}
421+
}
422+
423+
impl MyTrait<i64> for S<i32> {
424+
fn f(&self) -> i64 {
425+
0
426+
}
427+
}
428+
429+
impl MyTrait2<S<i32>> for S<i32> {
430+
type Output = i32;
431+
432+
fn f(&self, x: S<i32>) -> Self::Output {
433+
0
434+
}
435+
}
436+
437+
impl MyTrait2<S<i64>> for S<i32> {
438+
type Output = <Self as MyTrait2<S<bool>>>::Output;
439+
440+
fn f(&self, x: S<i64>) -> Self::Output {
441+
0
442+
}
443+
}
444+
445+
impl MyTrait2<S<bool>> for S<i32> {
446+
type Output = i64;
447+
448+
fn f(&self, x: S<bool>) -> Self::Output {
449+
0
450+
}
451+
}
452+
453+
fn call_f<T1, T2: MyTrait<T1>>(x: T2) -> T1 {
454+
x.f() // $ target=f
455+
}
456+
457+
fn call_f2<T1, T2: MyTrait2<T1>>(x: T1, y: T2) -> T2::Output {
458+
y.f(x) // $ target=f
459+
}
460+
461+
fn test() {
462+
let x = S(0);
463+
let y = call_f(x); // $ target=call_f type=y:i32 $ SPURIOUS: type=y:i64
464+
let z: i32 = y;
465+
466+
let x = S(0);
467+
let y = call_f::<i32, _>(x); // $ target=call_f type=y:i32
468+
469+
let x = S(0);
470+
let y = call_f2(S(0i32), x); // $ target=call_f2 type=y:i32 $ SPURIOUS: type=y:i64
471+
let x = S(0);
472+
let y = call_f2(S(0i64), x); // $ target=call_f2 type=y:i64 $ SPURIOUS: type=y:i32
473+
}
474+
}

rust/ql/test/library-tests/type-inference/type-inference.expected

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4012,6 +4012,51 @@ inferCertainType
40124012
| overloading.rs:397:10:397:10 | b | | {EXTERNAL LOCATION} | bool |
40134013
| overloading.rs:397:25:401:5 | { ... } | | overloading.rs:372:5:372:14 | S1 |
40144014
| overloading.rs:398:20:398:20 | b | | {EXTERNAL LOCATION} | bool |
4015+
| overloading.rs:406:14:406:18 | SelfParam | | {EXTERNAL LOCATION} | & |
4016+
| overloading.rs:406:14:406:18 | SelfParam | TRef | overloading.rs:405:5:407:5 | Self [trait MyTrait] |
4017+
| overloading.rs:412:14:412:18 | SelfParam | | {EXTERNAL LOCATION} | & |
4018+
| overloading.rs:412:14:412:18 | SelfParam | TRef | overloading.rs:409:5:413:5 | Self [trait MyTrait2] |
4019+
| overloading.rs:412:21:412:21 | x | | overloading.rs:409:20:409:27 | T |
4020+
| overloading.rs:418:14:418:18 | SelfParam | | {EXTERNAL LOCATION} | & |
4021+
| overloading.rs:418:14:418:18 | SelfParam | TRef | overloading.rs:415:5:415:19 | S |
4022+
| overloading.rs:418:14:418:18 | SelfParam | TRef.T | {EXTERNAL LOCATION} | i32 |
4023+
| overloading.rs:418:28:420:9 | { ... } | | {EXTERNAL LOCATION} | i32 |
4024+
| overloading.rs:424:14:424:18 | SelfParam | | {EXTERNAL LOCATION} | & |
4025+
| overloading.rs:424:14:424:18 | SelfParam | TRef | overloading.rs:415:5:415:19 | S |
4026+
| overloading.rs:424:14:424:18 | SelfParam | TRef.T | {EXTERNAL LOCATION} | i32 |
4027+
| overloading.rs:424:28:426:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
4028+
| overloading.rs:432:14:432:18 | SelfParam | | {EXTERNAL LOCATION} | & |
4029+
| overloading.rs:432:14:432:18 | SelfParam | TRef | overloading.rs:415:5:415:19 | S |
4030+
| overloading.rs:432:14:432:18 | SelfParam | TRef.T | {EXTERNAL LOCATION} | i32 |
4031+
| overloading.rs:432:21:432:21 | x | | overloading.rs:415:5:415:19 | S |
4032+
| overloading.rs:432:21:432:21 | x | T | {EXTERNAL LOCATION} | i32 |
4033+
| overloading.rs:432:48:434:9 | { ... } | | {EXTERNAL LOCATION} | i32 |
4034+
| overloading.rs:440:14:440:18 | SelfParam | | {EXTERNAL LOCATION} | & |
4035+
| overloading.rs:440:14:440:18 | SelfParam | TRef | overloading.rs:415:5:415:19 | S |
4036+
| overloading.rs:440:14:440:18 | SelfParam | TRef.T | {EXTERNAL LOCATION} | i32 |
4037+
| overloading.rs:440:21:440:21 | x | | overloading.rs:415:5:415:19 | S |
4038+
| overloading.rs:440:21:440:21 | x | T | {EXTERNAL LOCATION} | i64 |
4039+
| overloading.rs:440:48:442:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
4040+
| overloading.rs:448:14:448:18 | SelfParam | | {EXTERNAL LOCATION} | & |
4041+
| overloading.rs:448:14:448:18 | SelfParam | TRef | overloading.rs:415:5:415:19 | S |
4042+
| overloading.rs:448:14:448:18 | SelfParam | TRef.T | {EXTERNAL LOCATION} | i32 |
4043+
| overloading.rs:448:21:448:21 | x | | overloading.rs:415:5:415:19 | S |
4044+
| overloading.rs:448:21:448:21 | x | T | {EXTERNAL LOCATION} | bool |
4045+
| overloading.rs:448:49:450:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
4046+
| overloading.rs:453:36:453:36 | x | | overloading.rs:453:19:453:33 | T2 |
4047+
| overloading.rs:453:49:455:5 | { ... } | | overloading.rs:453:15:453:16 | T1 |
4048+
| overloading.rs:454:9:454:9 | x | | overloading.rs:453:19:453:33 | T2 |
4049+
| overloading.rs:457:38:457:38 | x | | overloading.rs:457:16:457:17 | T1 |
4050+
| overloading.rs:457:45:457:45 | y | | overloading.rs:457:20:457:35 | T2 |
4051+
| overloading.rs:457:66:459:5 | { ... } | | overloading.rs:457:20:457:35 | T2::Output[MyTrait2] |
4052+
| overloading.rs:458:9:458:9 | y | | overloading.rs:457:20:457:35 | T2 |
4053+
| overloading.rs:458:13:458:13 | x | | overloading.rs:457:16:457:17 | T1 |
4054+
| overloading.rs:461:15:473:5 | { ... } | | {EXTERNAL LOCATION} | () |
4055+
| overloading.rs:464:13:464:13 | z | | {EXTERNAL LOCATION} | i32 |
4056+
| overloading.rs:467:13:467:13 | y | | {EXTERNAL LOCATION} | i32 |
4057+
| overloading.rs:467:17:467:35 | call_f::<...>(...) | | {EXTERNAL LOCATION} | i32 |
4058+
| overloading.rs:470:27:470:30 | 0i32 | | {EXTERNAL LOCATION} | i32 |
4059+
| overloading.rs:472:27:472:30 | 0i64 | | {EXTERNAL LOCATION} | i64 |
40154060
| pattern_matching.rs:13:26:133:1 | { ... } | | {EXTERNAL LOCATION} | Option |
40164061
| pattern_matching.rs:13:26:133:1 | { ... } | T | {EXTERNAL LOCATION} | () |
40174062
| pattern_matching.rs:15:5:18:5 | if ... {...} | | {EXTERNAL LOCATION} | () |
@@ -12675,6 +12720,107 @@ inferType
1267512720
| overloading.rs:399:17:399:29 | ...::from(...) | | overloading.rs:372:5:372:14 | S1 |
1267612721
| overloading.rs:399:28:399:28 | s | | overloading.rs:364:5:365:13 | S |
1267712722
| overloading.rs:400:9:400:9 | x | | overloading.rs:372:5:372:14 | S1 |
12723+
| overloading.rs:406:14:406:18 | SelfParam | | {EXTERNAL LOCATION} | & |
12724+
| overloading.rs:406:14:406:18 | SelfParam | TRef | overloading.rs:405:5:407:5 | Self [trait MyTrait] |
12725+
| overloading.rs:412:14:412:18 | SelfParam | | {EXTERNAL LOCATION} | & |
12726+
| overloading.rs:412:14:412:18 | SelfParam | TRef | overloading.rs:409:5:413:5 | Self [trait MyTrait2] |
12727+
| overloading.rs:412:21:412:21 | x | | overloading.rs:409:20:409:27 | T |
12728+
| overloading.rs:418:14:418:18 | SelfParam | | {EXTERNAL LOCATION} | & |
12729+
| overloading.rs:418:14:418:18 | SelfParam | TRef | overloading.rs:415:5:415:19 | S |
12730+
| overloading.rs:418:14:418:18 | SelfParam | TRef.T | {EXTERNAL LOCATION} | i32 |
12731+
| overloading.rs:418:28:420:9 | { ... } | | {EXTERNAL LOCATION} | i32 |
12732+
| overloading.rs:419:13:419:13 | 0 | | {EXTERNAL LOCATION} | i32 |
12733+
| overloading.rs:424:14:424:18 | SelfParam | | {EXTERNAL LOCATION} | & |
12734+
| overloading.rs:424:14:424:18 | SelfParam | TRef | overloading.rs:415:5:415:19 | S |
12735+
| overloading.rs:424:14:424:18 | SelfParam | TRef.T | {EXTERNAL LOCATION} | i32 |
12736+
| overloading.rs:424:28:426:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
12737+
| overloading.rs:425:13:425:13 | 0 | | {EXTERNAL LOCATION} | i32 |
12738+
| overloading.rs:425:13:425:13 | 0 | | {EXTERNAL LOCATION} | i64 |
12739+
| overloading.rs:432:14:432:18 | SelfParam | | {EXTERNAL LOCATION} | & |
12740+
| overloading.rs:432:14:432:18 | SelfParam | TRef | overloading.rs:415:5:415:19 | S |
12741+
| overloading.rs:432:14:432:18 | SelfParam | TRef.T | {EXTERNAL LOCATION} | i32 |
12742+
| overloading.rs:432:21:432:21 | x | | overloading.rs:415:5:415:19 | S |
12743+
| overloading.rs:432:21:432:21 | x | T | {EXTERNAL LOCATION} | i32 |
12744+
| overloading.rs:432:48:434:9 | { ... } | | {EXTERNAL LOCATION} | i32 |
12745+
| overloading.rs:433:13:433:13 | 0 | | {EXTERNAL LOCATION} | i32 |
12746+
| overloading.rs:440:14:440:18 | SelfParam | | {EXTERNAL LOCATION} | & |
12747+
| overloading.rs:440:14:440:18 | SelfParam | TRef | overloading.rs:415:5:415:19 | S |
12748+
| overloading.rs:440:14:440:18 | SelfParam | TRef.T | {EXTERNAL LOCATION} | i32 |
12749+
| overloading.rs:440:21:440:21 | x | | overloading.rs:415:5:415:19 | S |
12750+
| overloading.rs:440:21:440:21 | x | T | {EXTERNAL LOCATION} | i64 |
12751+
| overloading.rs:440:48:442:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
12752+
| overloading.rs:441:13:441:13 | 0 | | {EXTERNAL LOCATION} | i32 |
12753+
| overloading.rs:441:13:441:13 | 0 | | {EXTERNAL LOCATION} | i64 |
12754+
| overloading.rs:448:14:448:18 | SelfParam | | {EXTERNAL LOCATION} | & |
12755+
| overloading.rs:448:14:448:18 | SelfParam | TRef | overloading.rs:415:5:415:19 | S |
12756+
| overloading.rs:448:14:448:18 | SelfParam | TRef.T | {EXTERNAL LOCATION} | i32 |
12757+
| overloading.rs:448:21:448:21 | x | | overloading.rs:415:5:415:19 | S |
12758+
| overloading.rs:448:21:448:21 | x | T | {EXTERNAL LOCATION} | bool |
12759+
| overloading.rs:448:49:450:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
12760+
| overloading.rs:449:13:449:13 | 0 | | {EXTERNAL LOCATION} | i32 |
12761+
| overloading.rs:449:13:449:13 | 0 | | {EXTERNAL LOCATION} | i64 |
12762+
| overloading.rs:453:36:453:36 | x | | overloading.rs:453:19:453:33 | T2 |
12763+
| overloading.rs:453:49:455:5 | { ... } | | overloading.rs:453:15:453:16 | T1 |
12764+
| overloading.rs:454:9:454:9 | x | | overloading.rs:453:19:453:33 | T2 |
12765+
| overloading.rs:454:9:454:13 | x.f() | | overloading.rs:453:15:453:16 | T1 |
12766+
| overloading.rs:457:38:457:38 | x | | overloading.rs:457:16:457:17 | T1 |
12767+
| overloading.rs:457:45:457:45 | y | | overloading.rs:457:20:457:35 | T2 |
12768+
| overloading.rs:457:66:459:5 | { ... } | | overloading.rs:457:20:457:35 | T2::Output[MyTrait2] |
12769+
| overloading.rs:458:9:458:9 | y | | overloading.rs:457:20:457:35 | T2 |
12770+
| overloading.rs:458:9:458:14 | y.f(...) | | overloading.rs:457:20:457:35 | T2::Output[MyTrait2] |
12771+
| overloading.rs:458:13:458:13 | x | | overloading.rs:457:16:457:17 | T1 |
12772+
| overloading.rs:461:15:473:5 | { ... } | | {EXTERNAL LOCATION} | () |
12773+
| overloading.rs:462:13:462:13 | x | | overloading.rs:415:5:415:19 | S |
12774+
| overloading.rs:462:13:462:13 | x | T | {EXTERNAL LOCATION} | i32 |
12775+
| overloading.rs:462:17:462:20 | S(...) | | overloading.rs:415:5:415:19 | S |
12776+
| overloading.rs:462:17:462:20 | S(...) | T | {EXTERNAL LOCATION} | i32 |
12777+
| overloading.rs:462:19:462:19 | 0 | | {EXTERNAL LOCATION} | i32 |
12778+
| overloading.rs:463:13:463:13 | y | | {EXTERNAL LOCATION} | i32 |
12779+
| overloading.rs:463:13:463:13 | y | | {EXTERNAL LOCATION} | i64 |
12780+
| overloading.rs:463:17:463:25 | call_f(...) | | {EXTERNAL LOCATION} | i32 |
12781+
| overloading.rs:463:17:463:25 | call_f(...) | | {EXTERNAL LOCATION} | i64 |
12782+
| overloading.rs:463:24:463:24 | x | | overloading.rs:415:5:415:19 | S |
12783+
| overloading.rs:463:24:463:24 | x | T | {EXTERNAL LOCATION} | i32 |
12784+
| overloading.rs:464:13:464:13 | z | | {EXTERNAL LOCATION} | i32 |
12785+
| overloading.rs:464:22:464:22 | y | | {EXTERNAL LOCATION} | i32 |
12786+
| overloading.rs:464:22:464:22 | y | | {EXTERNAL LOCATION} | i64 |
12787+
| overloading.rs:466:13:466:13 | x | | overloading.rs:415:5:415:19 | S |
12788+
| overloading.rs:466:13:466:13 | x | T | {EXTERNAL LOCATION} | i32 |
12789+
| overloading.rs:466:17:466:20 | S(...) | | overloading.rs:415:5:415:19 | S |
12790+
| overloading.rs:466:17:466:20 | S(...) | T | {EXTERNAL LOCATION} | i32 |
12791+
| overloading.rs:466:19:466:19 | 0 | | {EXTERNAL LOCATION} | i32 |
12792+
| overloading.rs:467:13:467:13 | y | | {EXTERNAL LOCATION} | i32 |
12793+
| overloading.rs:467:17:467:35 | call_f::<...>(...) | | {EXTERNAL LOCATION} | i32 |
12794+
| overloading.rs:467:34:467:34 | x | | overloading.rs:415:5:415:19 | S |
12795+
| overloading.rs:467:34:467:34 | x | T | {EXTERNAL LOCATION} | i32 |
12796+
| overloading.rs:469:13:469:13 | x | | overloading.rs:415:5:415:19 | S |
12797+
| overloading.rs:469:13:469:13 | x | T | {EXTERNAL LOCATION} | i32 |
12798+
| overloading.rs:469:17:469:20 | S(...) | | overloading.rs:415:5:415:19 | S |
12799+
| overloading.rs:469:17:469:20 | S(...) | T | {EXTERNAL LOCATION} | i32 |
12800+
| overloading.rs:469:19:469:19 | 0 | | {EXTERNAL LOCATION} | i32 |
12801+
| overloading.rs:470:13:470:13 | y | | {EXTERNAL LOCATION} | i32 |
12802+
| overloading.rs:470:13:470:13 | y | | {EXTERNAL LOCATION} | i64 |
12803+
| overloading.rs:470:17:470:35 | call_f2(...) | | {EXTERNAL LOCATION} | i32 |
12804+
| overloading.rs:470:17:470:35 | call_f2(...) | | {EXTERNAL LOCATION} | i64 |
12805+
| overloading.rs:470:25:470:31 | S(...) | | overloading.rs:415:5:415:19 | S |
12806+
| overloading.rs:470:25:470:31 | S(...) | T | {EXTERNAL LOCATION} | i32 |
12807+
| overloading.rs:470:27:470:30 | 0i32 | | {EXTERNAL LOCATION} | i32 |
12808+
| overloading.rs:470:34:470:34 | x | | overloading.rs:415:5:415:19 | S |
12809+
| overloading.rs:470:34:470:34 | x | T | {EXTERNAL LOCATION} | i32 |
12810+
| overloading.rs:471:13:471:13 | x | | overloading.rs:415:5:415:19 | S |
12811+
| overloading.rs:471:13:471:13 | x | T | {EXTERNAL LOCATION} | i32 |
12812+
| overloading.rs:471:17:471:20 | S(...) | | overloading.rs:415:5:415:19 | S |
12813+
| overloading.rs:471:17:471:20 | S(...) | T | {EXTERNAL LOCATION} | i32 |
12814+
| overloading.rs:471:19:471:19 | 0 | | {EXTERNAL LOCATION} | i32 |
12815+
| overloading.rs:472:13:472:13 | y | | {EXTERNAL LOCATION} | i32 |
12816+
| overloading.rs:472:13:472:13 | y | | {EXTERNAL LOCATION} | i64 |
12817+
| overloading.rs:472:17:472:35 | call_f2(...) | | {EXTERNAL LOCATION} | i32 |
12818+
| overloading.rs:472:17:472:35 | call_f2(...) | | {EXTERNAL LOCATION} | i64 |
12819+
| overloading.rs:472:25:472:31 | S(...) | | overloading.rs:415:5:415:19 | S |
12820+
| overloading.rs:472:25:472:31 | S(...) | T | {EXTERNAL LOCATION} | i64 |
12821+
| overloading.rs:472:27:472:30 | 0i64 | | {EXTERNAL LOCATION} | i64 |
12822+
| overloading.rs:472:34:472:34 | x | | overloading.rs:415:5:415:19 | S |
12823+
| overloading.rs:472:34:472:34 | x | T | {EXTERNAL LOCATION} | i32 |
1267812824
| pattern_matching.rs:13:26:133:1 | { ... } | | {EXTERNAL LOCATION} | Option |
1267912825
| pattern_matching.rs:13:26:133:1 | { ... } | T | {EXTERNAL LOCATION} | () |
1268012826
| pattern_matching.rs:14:9:14:13 | value | | {EXTERNAL LOCATION} | Option |

0 commit comments

Comments
 (0)