Skip to content

Commit 9b815c2

Browse files
committed
Rust: Add type inference tests for index expressions
1 parent 0ef17ba commit 9b815c2

File tree

3 files changed

+111
-5
lines changed

3 files changed

+111
-5
lines changed

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,6 +1630,58 @@ mod overloadable_operators {
16301630
}
16311631
}
16321632

1633+
mod indexers {
1634+
use std::ops::Index;
1635+
1636+
#[derive(Debug)]
1637+
struct S;
1638+
1639+
impl S {
1640+
fn foo(&self) -> Self {
1641+
S
1642+
}
1643+
}
1644+
1645+
#[derive(Debug)]
1646+
struct MyVec<T> {
1647+
data: Vec<T>,
1648+
}
1649+
1650+
impl<T> MyVec<T> {
1651+
fn new() -> Self {
1652+
MyVec { data: Vec::new() }
1653+
}
1654+
1655+
fn push(&mut self, value: T) {
1656+
self.data.push(value); // $ fieldof=MyVec
1657+
}
1658+
}
1659+
1660+
impl<T> Index<usize> for MyVec<T> {
1661+
type Output = T;
1662+
1663+
// MyVec::index
1664+
fn index(&self, index: usize) -> &Self::Output {
1665+
&self.data[index] // $ fieldof=MyVec
1666+
}
1667+
}
1668+
1669+
fn analyze_slice(slice: &[S]) {
1670+
let x = slice[0].foo(); // $ MISSING: method=foo MISSING: type=x:S
1671+
}
1672+
1673+
pub fn f() {
1674+
let mut vec = MyVec::new(); // $ type=vec:T.S
1675+
vec.push(S); // $ method=push
1676+
vec[0].foo(); // $ MISSING: method=foo
1677+
1678+
let xs: [S; 1] = [S];
1679+
let x = xs[0].foo(); // $ MISSING: method=foo MISSING: type=x:S
1680+
1681+
analyze_slice(&xs);
1682+
}
1683+
}
1684+
16331685
fn main() {
16341686
field_access::f();
16351687
method_impl::f();
@@ -1649,4 +1701,5 @@ fn main() {
16491701
try_expressions::f();
16501702
builtins::f();
16511703
operators::f();
1704+
indexers::f();
16521705
}

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

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,7 +2375,57 @@ inferType
23752375
| main.rs:1629:13:1629:20 | vec2_not | | main.rs:1278:5:1283:5 | Vec2 |
23762376
| main.rs:1629:24:1629:26 | ! ... | | main.rs:1278:5:1283:5 | Vec2 |
23772377
| main.rs:1629:25:1629:26 | v1 | | main.rs:1278:5:1283:5 | Vec2 |
2378-
| main.rs:1635:5:1635:20 | ...::f(...) | | main.rs:67:5:67:21 | Foo |
2379-
| main.rs:1636:5:1636:60 | ...::g(...) | | main.rs:67:5:67:21 | Foo |
2380-
| main.rs:1636:20:1636:38 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |
2381-
| main.rs:1636:41:1636:59 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |
2378+
| main.rs:1640:16:1640:20 | SelfParam | | file://:0:0:0:0 | & |
2379+
| main.rs:1640:16:1640:20 | SelfParam | &T | main.rs:1636:5:1637:13 | S |
2380+
| main.rs:1640:31:1642:9 | { ... } | | main.rs:1636:5:1637:13 | S |
2381+
| main.rs:1641:13:1641:13 | S | | main.rs:1636:5:1637:13 | S |
2382+
| main.rs:1651:26:1653:9 | { ... } | | main.rs:1645:5:1648:5 | MyVec |
2383+
| main.rs:1651:26:1653:9 | { ... } | T | main.rs:1650:10:1650:10 | T |
2384+
| main.rs:1652:13:1652:38 | MyVec {...} | | main.rs:1645:5:1648:5 | MyVec |
2385+
| main.rs:1652:13:1652:38 | MyVec {...} | T | main.rs:1650:10:1650:10 | T |
2386+
| main.rs:1655:17:1655:25 | SelfParam | | file://:0:0:0:0 | & |
2387+
| main.rs:1655:17:1655:25 | SelfParam | &T | main.rs:1645:5:1648:5 | MyVec |
2388+
| main.rs:1655:17:1655:25 | SelfParam | &T.T | main.rs:1650:10:1650:10 | T |
2389+
| main.rs:1655:28:1655:32 | value | | main.rs:1650:10:1650:10 | T |
2390+
| main.rs:1656:13:1656:16 | self | | file://:0:0:0:0 | & |
2391+
| main.rs:1656:13:1656:16 | self | &T | main.rs:1645:5:1648:5 | MyVec |
2392+
| main.rs:1656:13:1656:16 | self | &T.T | main.rs:1650:10:1650:10 | T |
2393+
| main.rs:1656:28:1656:32 | value | | main.rs:1650:10:1650:10 | T |
2394+
| main.rs:1664:18:1664:22 | SelfParam | | file://:0:0:0:0 | & |
2395+
| main.rs:1664:18:1664:22 | SelfParam | &T | main.rs:1645:5:1648:5 | MyVec |
2396+
| main.rs:1664:18:1664:22 | SelfParam | &T.T | main.rs:1660:10:1660:10 | T |
2397+
| main.rs:1664:25:1664:29 | index | | file:///BUILTINS/types.rs:20:1:21:17 | usize |
2398+
| main.rs:1664:56:1666:9 | { ... } | | file://:0:0:0:0 | & |
2399+
| main.rs:1664:56:1666:9 | { ... } | &T | main.rs:1660:10:1660:10 | T |
2400+
| main.rs:1665:13:1665:29 | &... | | file://:0:0:0:0 | & |
2401+
| main.rs:1665:13:1665:29 | &... | &T | main.rs:1660:10:1660:10 | T |
2402+
| main.rs:1665:14:1665:17 | self | | file://:0:0:0:0 | & |
2403+
| main.rs:1665:14:1665:17 | self | &T | main.rs:1645:5:1648:5 | MyVec |
2404+
| main.rs:1665:14:1665:17 | self | &T.T | main.rs:1660:10:1660:10 | T |
2405+
| main.rs:1665:24:1665:28 | index | | file:///BUILTINS/types.rs:20:1:21:17 | usize |
2406+
| main.rs:1669:22:1669:26 | slice | | file://:0:0:0:0 | & |
2407+
| main.rs:1670:17:1670:21 | slice | | file://:0:0:0:0 | & |
2408+
| main.rs:1670:23:1670:23 | 0 | | file:///BUILTINS/types.rs:12:1:12:15 | i32 |
2409+
| main.rs:1674:13:1674:19 | mut vec | | main.rs:1645:5:1648:5 | MyVec |
2410+
| main.rs:1674:13:1674:19 | mut vec | T | main.rs:1636:5:1637:13 | S |
2411+
| main.rs:1674:23:1674:34 | ...::new(...) | | main.rs:1645:5:1648:5 | MyVec |
2412+
| main.rs:1674:23:1674:34 | ...::new(...) | T | main.rs:1636:5:1637:13 | S |
2413+
| main.rs:1675:9:1675:11 | vec | | main.rs:1645:5:1648:5 | MyVec |
2414+
| main.rs:1675:9:1675:11 | vec | T | main.rs:1636:5:1637:13 | S |
2415+
| main.rs:1675:18:1675:18 | S | | main.rs:1636:5:1637:13 | S |
2416+
| main.rs:1676:9:1676:11 | vec | | main.rs:1645:5:1648:5 | MyVec |
2417+
| main.rs:1676:9:1676:11 | vec | T | main.rs:1636:5:1637:13 | S |
2418+
| main.rs:1676:13:1676:13 | 0 | | file:///BUILTINS/types.rs:12:1:12:15 | i32 |
2419+
| main.rs:1678:13:1678:14 | xs | | file://:0:0:0:0 | [] |
2420+
| main.rs:1678:21:1678:21 | 1 | | file:///BUILTINS/types.rs:12:1:12:15 | i32 |
2421+
| main.rs:1678:26:1678:28 | [...] | | file://:0:0:0:0 | [] |
2422+
| main.rs:1678:27:1678:27 | S | | main.rs:1636:5:1637:13 | S |
2423+
| main.rs:1679:17:1679:18 | xs | | file://:0:0:0:0 | [] |
2424+
| main.rs:1679:20:1679:20 | 0 | | file:///BUILTINS/types.rs:12:1:12:15 | i32 |
2425+
| main.rs:1681:23:1681:25 | &xs | | file://:0:0:0:0 | & |
2426+
| main.rs:1681:23:1681:25 | &xs | &T | file://:0:0:0:0 | [] |
2427+
| main.rs:1681:24:1681:25 | xs | | file://:0:0:0:0 | [] |
2428+
| main.rs:1687:5:1687:20 | ...::f(...) | | main.rs:67:5:67:21 | Foo |
2429+
| main.rs:1688:5:1688:60 | ...::g(...) | | main.rs:67:5:67:21 | Foo |
2430+
| main.rs:1688:20:1688:38 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |
2431+
| main.rs:1688:41:1688:59 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,13 @@ module TypeTest implements TestSig {
7070
exists(AstNode n, TypePath path, Type t |
7171
t = TypeInference::inferType(n, path) and
7272
location = n.getLocation() and
73-
element = n.toString() and
7473
if path.isEmpty()
7574
then value = element + ":" + t
7675
else value = element + ":" + path.toString() + "." + t.toString()
76+
|
77+
element = n.toString()
78+
or
79+
element = n.(IdentPat).getName().getText()
7780
)
7881
}
7982
}

0 commit comments

Comments
 (0)