Skip to content

Commit 16613d1

Browse files
committed
Rust: Add type inference test
1 parent 8e19b05 commit 16613d1

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
@@ -449,3 +449,75 @@ mod inherent_before_trait {
449449
}
450450
}
451451
}
452+
453+
mod trait_bound_impl_overlap {
454+
trait MyTrait<T> {
455+
fn f(&self) -> T;
456+
}
457+
458+
trait MyTrait2<T = Self> {
459+
type Output;
460+
461+
fn f(&self, x: T) -> Self::Output;
462+
}
463+
464+
struct S<T>(T);
465+
466+
impl MyTrait<i32> for S<i32> {
467+
fn f(&self) -> i32 {
468+
0
469+
}
470+
}
471+
472+
impl MyTrait<i64> for S<i32> {
473+
fn f(&self) -> i64 {
474+
0
475+
}
476+
}
477+
478+
impl MyTrait2<S<i32>> for S<i32> {
479+
type Output = i32;
480+
481+
fn f(&self, x: S<i32>) -> Self::Output {
482+
0
483+
}
484+
}
485+
486+
impl MyTrait2<S<i64>> for S<i32> {
487+
type Output = <Self as MyTrait2<S<bool>>>::Output;
488+
489+
fn f(&self, x: S<i64>) -> Self::Output {
490+
0
491+
}
492+
}
493+
494+
impl MyTrait2<S<bool>> for S<i32> {
495+
type Output = i64;
496+
497+
fn f(&self, x: S<bool>) -> Self::Output {
498+
0
499+
}
500+
}
501+
502+
fn call_f<T1, T2: MyTrait<T1>>(x: T2) -> T1 {
503+
x.f() // $ target=f
504+
}
505+
506+
fn call_f2<T1, T2: MyTrait2<T1>>(x: T1, y: T2) -> T2::Output {
507+
y.f(x) // $ target=f
508+
}
509+
510+
fn test() {
511+
let x = S(0);
512+
let y = call_f(x); // $ target=call_f type=y:i32 $ SPURIOUS: type=y:i64
513+
let z: i32 = y;
514+
515+
let x = S(0);
516+
let y = call_f::<i32, _>(x); // $ target=call_f type=y:i32
517+
518+
let x = S(0);
519+
let y = call_f2(S(0i32), x); // $ target=call_f2 type=y:i32 $ SPURIOUS: type=y:i64
520+
let x = S(0);
521+
let y = call_f2(S(0i64), x); // $ target=call_f2 type=y:i64 $ SPURIOUS: type=y:i32
522+
}
523+
}

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

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4079,6 +4079,51 @@ inferCertainType
40794079
| overloading.rs:448:13:448:16 | self | | {EXTERNAL LOCATION} | & |
40804080
| overloading.rs:448:13:448:16 | self | TRef | overloading.rs:405:5:405:19 | S |
40814081
| overloading.rs:448:13:448:16 | self | TRef.T | {EXTERNAL LOCATION} | i64 |
4082+
| overloading.rs:455:14:455:18 | SelfParam | | {EXTERNAL LOCATION} | & |
4083+
| overloading.rs:455:14:455:18 | SelfParam | TRef | overloading.rs:454:5:456:5 | Self [trait MyTrait] |
4084+
| overloading.rs:461:14:461:18 | SelfParam | | {EXTERNAL LOCATION} | & |
4085+
| overloading.rs:461:14:461:18 | SelfParam | TRef | overloading.rs:458:5:462:5 | Self [trait MyTrait2] |
4086+
| overloading.rs:461:21:461:21 | x | | overloading.rs:458:20:458:27 | T |
4087+
| overloading.rs:467:14:467:18 | SelfParam | | {EXTERNAL LOCATION} | & |
4088+
| overloading.rs:467:14:467:18 | SelfParam | TRef | overloading.rs:464:5:464:19 | S |
4089+
| overloading.rs:467:14:467:18 | SelfParam | TRef.T | {EXTERNAL LOCATION} | i32 |
4090+
| overloading.rs:467:28:469:9 | { ... } | | {EXTERNAL LOCATION} | i32 |
4091+
| overloading.rs:473:14:473:18 | SelfParam | | {EXTERNAL LOCATION} | & |
4092+
| overloading.rs:473:14:473:18 | SelfParam | TRef | overloading.rs:464:5:464:19 | S |
4093+
| overloading.rs:473:14:473:18 | SelfParam | TRef.T | {EXTERNAL LOCATION} | i32 |
4094+
| overloading.rs:473:28:475:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
4095+
| overloading.rs:481:14:481:18 | SelfParam | | {EXTERNAL LOCATION} | & |
4096+
| overloading.rs:481:14:481:18 | SelfParam | TRef | overloading.rs:464:5:464:19 | S |
4097+
| overloading.rs:481:14:481:18 | SelfParam | TRef.T | {EXTERNAL LOCATION} | i32 |
4098+
| overloading.rs:481:21:481:21 | x | | overloading.rs:464:5:464:19 | S |
4099+
| overloading.rs:481:21:481:21 | x | T | {EXTERNAL LOCATION} | i32 |
4100+
| overloading.rs:481:48:483:9 | { ... } | | {EXTERNAL LOCATION} | i32 |
4101+
| overloading.rs:489:14:489:18 | SelfParam | | {EXTERNAL LOCATION} | & |
4102+
| overloading.rs:489:14:489:18 | SelfParam | TRef | overloading.rs:464:5:464:19 | S |
4103+
| overloading.rs:489:14:489:18 | SelfParam | TRef.T | {EXTERNAL LOCATION} | i32 |
4104+
| overloading.rs:489:21:489:21 | x | | overloading.rs:464:5:464:19 | S |
4105+
| overloading.rs:489:21:489:21 | x | T | {EXTERNAL LOCATION} | i64 |
4106+
| overloading.rs:489:48:491:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
4107+
| overloading.rs:497:14:497:18 | SelfParam | | {EXTERNAL LOCATION} | & |
4108+
| overloading.rs:497:14:497:18 | SelfParam | TRef | overloading.rs:464:5:464:19 | S |
4109+
| overloading.rs:497:14:497:18 | SelfParam | TRef.T | {EXTERNAL LOCATION} | i32 |
4110+
| overloading.rs:497:21:497:21 | x | | overloading.rs:464:5:464:19 | S |
4111+
| overloading.rs:497:21:497:21 | x | T | {EXTERNAL LOCATION} | bool |
4112+
| overloading.rs:497:49:499:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
4113+
| overloading.rs:502:36:502:36 | x | | overloading.rs:502:19:502:33 | T2 |
4114+
| overloading.rs:502:49:504:5 | { ... } | | overloading.rs:502:15:502:16 | T1 |
4115+
| overloading.rs:503:9:503:9 | x | | overloading.rs:502:19:502:33 | T2 |
4116+
| overloading.rs:506:38:506:38 | x | | overloading.rs:506:16:506:17 | T1 |
4117+
| overloading.rs:506:45:506:45 | y | | overloading.rs:506:20:506:35 | T2 |
4118+
| overloading.rs:506:66:508:5 | { ... } | | overloading.rs:506:20:506:35 | T2::Output[MyTrait2] |
4119+
| overloading.rs:507:9:507:9 | y | | overloading.rs:506:20:506:35 | T2 |
4120+
| overloading.rs:507:13:507:13 | x | | overloading.rs:506:16:506:17 | T1 |
4121+
| overloading.rs:510:15:522:5 | { ... } | | {EXTERNAL LOCATION} | () |
4122+
| overloading.rs:513:13:513:13 | z | | {EXTERNAL LOCATION} | i32 |
4123+
| overloading.rs:516:13:516:13 | y | | {EXTERNAL LOCATION} | i32 |
4124+
| overloading.rs:516:17:516:35 | call_f::<...>(...) | | {EXTERNAL LOCATION} | i32 |
4125+
| overloading.rs:519:27:519:30 | 0i32 | | {EXTERNAL LOCATION} | i32 |
4126+
| overloading.rs:521:27:521:30 | 0i64 | | {EXTERNAL LOCATION} | i64 |
40824127
| pattern_matching.rs:13:26:133:1 | { ... } | | {EXTERNAL LOCATION} | Option |
40834128
| pattern_matching.rs:13:26:133:1 | { ... } | T | {EXTERNAL LOCATION} | () |
40844129
| pattern_matching.rs:15:5:18:5 | if ... {...} | | {EXTERNAL LOCATION} | () |
@@ -12830,6 +12875,107 @@ inferType
1283012875
| overloading.rs:448:13:448:16 | self | TRef | overloading.rs:405:5:405:19 | S |
1283112876
| overloading.rs:448:13:448:16 | self | TRef.T | {EXTERNAL LOCATION} | i64 |
1283212877
| overloading.rs:448:13:448:22 | self.bar() | | {EXTERNAL LOCATION} | () |
12878+
| overloading.rs:455:14:455:18 | SelfParam | | {EXTERNAL LOCATION} | & |
12879+
| overloading.rs:455:14:455:18 | SelfParam | TRef | overloading.rs:454:5:456:5 | Self [trait MyTrait] |
12880+
| overloading.rs:461:14:461:18 | SelfParam | | {EXTERNAL LOCATION} | & |
12881+
| overloading.rs:461:14:461:18 | SelfParam | TRef | overloading.rs:458:5:462:5 | Self [trait MyTrait2] |
12882+
| overloading.rs:461:21:461:21 | x | | overloading.rs:458:20:458:27 | T |
12883+
| overloading.rs:467:14:467:18 | SelfParam | | {EXTERNAL LOCATION} | & |
12884+
| overloading.rs:467:14:467:18 | SelfParam | TRef | overloading.rs:464:5:464:19 | S |
12885+
| overloading.rs:467:14:467:18 | SelfParam | TRef.T | {EXTERNAL LOCATION} | i32 |
12886+
| overloading.rs:467:28:469:9 | { ... } | | {EXTERNAL LOCATION} | i32 |
12887+
| overloading.rs:468:13:468:13 | 0 | | {EXTERNAL LOCATION} | i32 |
12888+
| overloading.rs:473:14:473:18 | SelfParam | | {EXTERNAL LOCATION} | & |
12889+
| overloading.rs:473:14:473:18 | SelfParam | TRef | overloading.rs:464:5:464:19 | S |
12890+
| overloading.rs:473:14:473:18 | SelfParam | TRef.T | {EXTERNAL LOCATION} | i32 |
12891+
| overloading.rs:473:28:475:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
12892+
| overloading.rs:474:13:474:13 | 0 | | {EXTERNAL LOCATION} | i32 |
12893+
| overloading.rs:474:13:474:13 | 0 | | {EXTERNAL LOCATION} | i64 |
12894+
| overloading.rs:481:14:481:18 | SelfParam | | {EXTERNAL LOCATION} | & |
12895+
| overloading.rs:481:14:481:18 | SelfParam | TRef | overloading.rs:464:5:464:19 | S |
12896+
| overloading.rs:481:14:481:18 | SelfParam | TRef.T | {EXTERNAL LOCATION} | i32 |
12897+
| overloading.rs:481:21:481:21 | x | | overloading.rs:464:5:464:19 | S |
12898+
| overloading.rs:481:21:481:21 | x | T | {EXTERNAL LOCATION} | i32 |
12899+
| overloading.rs:481:48:483:9 | { ... } | | {EXTERNAL LOCATION} | i32 |
12900+
| overloading.rs:482:13:482:13 | 0 | | {EXTERNAL LOCATION} | i32 |
12901+
| overloading.rs:489:14:489:18 | SelfParam | | {EXTERNAL LOCATION} | & |
12902+
| overloading.rs:489:14:489:18 | SelfParam | TRef | overloading.rs:464:5:464:19 | S |
12903+
| overloading.rs:489:14:489:18 | SelfParam | TRef.T | {EXTERNAL LOCATION} | i32 |
12904+
| overloading.rs:489:21:489:21 | x | | overloading.rs:464:5:464:19 | S |
12905+
| overloading.rs:489:21:489:21 | x | T | {EXTERNAL LOCATION} | i64 |
12906+
| overloading.rs:489:48:491:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
12907+
| overloading.rs:490:13:490:13 | 0 | | {EXTERNAL LOCATION} | i32 |
12908+
| overloading.rs:490:13:490:13 | 0 | | {EXTERNAL LOCATION} | i64 |
12909+
| overloading.rs:497:14:497:18 | SelfParam | | {EXTERNAL LOCATION} | & |
12910+
| overloading.rs:497:14:497:18 | SelfParam | TRef | overloading.rs:464:5:464:19 | S |
12911+
| overloading.rs:497:14:497:18 | SelfParam | TRef.T | {EXTERNAL LOCATION} | i32 |
12912+
| overloading.rs:497:21:497:21 | x | | overloading.rs:464:5:464:19 | S |
12913+
| overloading.rs:497:21:497:21 | x | T | {EXTERNAL LOCATION} | bool |
12914+
| overloading.rs:497:49:499:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
12915+
| overloading.rs:498:13:498:13 | 0 | | {EXTERNAL LOCATION} | i32 |
12916+
| overloading.rs:498:13:498:13 | 0 | | {EXTERNAL LOCATION} | i64 |
12917+
| overloading.rs:502:36:502:36 | x | | overloading.rs:502:19:502:33 | T2 |
12918+
| overloading.rs:502:49:504:5 | { ... } | | overloading.rs:502:15:502:16 | T1 |
12919+
| overloading.rs:503:9:503:9 | x | | overloading.rs:502:19:502:33 | T2 |
12920+
| overloading.rs:503:9:503:13 | x.f() | | overloading.rs:502:15:502:16 | T1 |
12921+
| overloading.rs:506:38:506:38 | x | | overloading.rs:506:16:506:17 | T1 |
12922+
| overloading.rs:506:45:506:45 | y | | overloading.rs:506:20:506:35 | T2 |
12923+
| overloading.rs:506:66:508:5 | { ... } | | overloading.rs:506:20:506:35 | T2::Output[MyTrait2] |
12924+
| overloading.rs:507:9:507:9 | y | | overloading.rs:506:20:506:35 | T2 |
12925+
| overloading.rs:507:9:507:14 | y.f(...) | | overloading.rs:506:20:506:35 | T2::Output[MyTrait2] |
12926+
| overloading.rs:507:13:507:13 | x | | overloading.rs:506:16:506:17 | T1 |
12927+
| overloading.rs:510:15:522:5 | { ... } | | {EXTERNAL LOCATION} | () |
12928+
| overloading.rs:511:13:511:13 | x | | overloading.rs:464:5:464:19 | S |
12929+
| overloading.rs:511:13:511:13 | x | T | {EXTERNAL LOCATION} | i32 |
12930+
| overloading.rs:511:17:511:20 | S(...) | | overloading.rs:464:5:464:19 | S |
12931+
| overloading.rs:511:17:511:20 | S(...) | T | {EXTERNAL LOCATION} | i32 |
12932+
| overloading.rs:511:19:511:19 | 0 | | {EXTERNAL LOCATION} | i32 |
12933+
| overloading.rs:512:13:512:13 | y | | {EXTERNAL LOCATION} | i32 |
12934+
| overloading.rs:512:13:512:13 | y | | {EXTERNAL LOCATION} | i64 |
12935+
| overloading.rs:512:17:512:25 | call_f(...) | | {EXTERNAL LOCATION} | i32 |
12936+
| overloading.rs:512:17:512:25 | call_f(...) | | {EXTERNAL LOCATION} | i64 |
12937+
| overloading.rs:512:24:512:24 | x | | overloading.rs:464:5:464:19 | S |
12938+
| overloading.rs:512:24:512:24 | x | T | {EXTERNAL LOCATION} | i32 |
12939+
| overloading.rs:513:13:513:13 | z | | {EXTERNAL LOCATION} | i32 |
12940+
| overloading.rs:513:22:513:22 | y | | {EXTERNAL LOCATION} | i32 |
12941+
| overloading.rs:513:22:513:22 | y | | {EXTERNAL LOCATION} | i64 |
12942+
| overloading.rs:515:13:515:13 | x | | overloading.rs:464:5:464:19 | S |
12943+
| overloading.rs:515:13:515:13 | x | T | {EXTERNAL LOCATION} | i32 |
12944+
| overloading.rs:515:17:515:20 | S(...) | | overloading.rs:464:5:464:19 | S |
12945+
| overloading.rs:515:17:515:20 | S(...) | T | {EXTERNAL LOCATION} | i32 |
12946+
| overloading.rs:515:19:515:19 | 0 | | {EXTERNAL LOCATION} | i32 |
12947+
| overloading.rs:516:13:516:13 | y | | {EXTERNAL LOCATION} | i32 |
12948+
| overloading.rs:516:17:516:35 | call_f::<...>(...) | | {EXTERNAL LOCATION} | i32 |
12949+
| overloading.rs:516:34:516:34 | x | | overloading.rs:464:5:464:19 | S |
12950+
| overloading.rs:516:34:516:34 | x | T | {EXTERNAL LOCATION} | i32 |
12951+
| overloading.rs:518:13:518:13 | x | | overloading.rs:464:5:464:19 | S |
12952+
| overloading.rs:518:13:518:13 | x | T | {EXTERNAL LOCATION} | i32 |
12953+
| overloading.rs:518:17:518:20 | S(...) | | overloading.rs:464:5:464:19 | S |
12954+
| overloading.rs:518:17:518:20 | S(...) | T | {EXTERNAL LOCATION} | i32 |
12955+
| overloading.rs:518:19:518:19 | 0 | | {EXTERNAL LOCATION} | i32 |
12956+
| overloading.rs:519:13:519:13 | y | | {EXTERNAL LOCATION} | i32 |
12957+
| overloading.rs:519:13:519:13 | y | | {EXTERNAL LOCATION} | i64 |
12958+
| overloading.rs:519:17:519:35 | call_f2(...) | | {EXTERNAL LOCATION} | i32 |
12959+
| overloading.rs:519:17:519:35 | call_f2(...) | | {EXTERNAL LOCATION} | i64 |
12960+
| overloading.rs:519:25:519:31 | S(...) | | overloading.rs:464:5:464:19 | S |
12961+
| overloading.rs:519:25:519:31 | S(...) | T | {EXTERNAL LOCATION} | i32 |
12962+
| overloading.rs:519:27:519:30 | 0i32 | | {EXTERNAL LOCATION} | i32 |
12963+
| overloading.rs:519:34:519:34 | x | | overloading.rs:464:5:464:19 | S |
12964+
| overloading.rs:519:34:519:34 | x | T | {EXTERNAL LOCATION} | i32 |
12965+
| overloading.rs:520:13:520:13 | x | | overloading.rs:464:5:464:19 | S |
12966+
| overloading.rs:520:13:520:13 | x | T | {EXTERNAL LOCATION} | i32 |
12967+
| overloading.rs:520:17:520:20 | S(...) | | overloading.rs:464:5:464:19 | S |
12968+
| overloading.rs:520:17:520:20 | S(...) | T | {EXTERNAL LOCATION} | i32 |
12969+
| overloading.rs:520:19:520:19 | 0 | | {EXTERNAL LOCATION} | i32 |
12970+
| overloading.rs:521:13:521:13 | y | | {EXTERNAL LOCATION} | i32 |
12971+
| overloading.rs:521:13:521:13 | y | | {EXTERNAL LOCATION} | i64 |
12972+
| overloading.rs:521:17:521:35 | call_f2(...) | | {EXTERNAL LOCATION} | i32 |
12973+
| overloading.rs:521:17:521:35 | call_f2(...) | | {EXTERNAL LOCATION} | i64 |
12974+
| overloading.rs:521:25:521:31 | S(...) | | overloading.rs:464:5:464:19 | S |
12975+
| overloading.rs:521:25:521:31 | S(...) | T | {EXTERNAL LOCATION} | i64 |
12976+
| overloading.rs:521:27:521:30 | 0i64 | | {EXTERNAL LOCATION} | i64 |
12977+
| overloading.rs:521:34:521:34 | x | | overloading.rs:464:5:464:19 | S |
12978+
| overloading.rs:521:34:521:34 | x | T | {EXTERNAL LOCATION} | i32 |
1283312979
| pattern_matching.rs:13:26:133:1 | { ... } | | {EXTERNAL LOCATION} | Option |
1283412980
| pattern_matching.rs:13:26:133:1 | { ... } | T | {EXTERNAL LOCATION} | () |
1283512981
| pattern_matching.rs:14:9:14:13 | value | | {EXTERNAL LOCATION} | Option |

0 commit comments

Comments
 (0)