Skip to content

Commit 61e2e47

Browse files
committed
Rust: Add type inference tests for index expressions
1 parent 3d395dd commit 61e2e47

File tree

3 files changed

+119
-6
lines changed

3 files changed

+119
-6
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
@@ -1734,6 +1734,58 @@ mod impl_trait {
17341734
}
17351735
}
17361736

1737+
mod indexers {
1738+
use std::ops::Index;
1739+
1740+
#[derive(Debug)]
1741+
struct S;
1742+
1743+
impl S {
1744+
fn foo(&self) -> Self {
1745+
S
1746+
}
1747+
}
1748+
1749+
#[derive(Debug)]
1750+
struct MyVec<T> {
1751+
data: Vec<T>,
1752+
}
1753+
1754+
impl<T> MyVec<T> {
1755+
fn new() -> Self {
1756+
MyVec { data: Vec::new() }
1757+
}
1758+
1759+
fn push(&mut self, value: T) {
1760+
self.data.push(value); // $ fieldof=MyVec
1761+
}
1762+
}
1763+
1764+
impl<T> Index<usize> for MyVec<T> {
1765+
type Output = T;
1766+
1767+
// MyVec::index
1768+
fn index(&self, index: usize) -> &Self::Output {
1769+
&self.data[index] // $ fieldof=MyVec
1770+
}
1771+
}
1772+
1773+
fn analyze_slice(slice: &[S]) {
1774+
let x = slice[0].foo(); // $ MISSING: method=foo MISSING: type=x:S
1775+
}
1776+
1777+
pub fn f() {
1778+
let mut vec = MyVec::new(); // $ type=vec:T.S
1779+
vec.push(S); // $ method=push
1780+
vec[0].foo(); // $ MISSING: method=foo
1781+
1782+
let xs: [S; 1] = [S];
1783+
let x = xs[0].foo(); // $ MISSING: method=foo MISSING: type=x:S
1784+
1785+
analyze_slice(&xs);
1786+
}
1787+
}
1788+
17371789
fn main() {
17381790
field_access::f();
17391791
method_impl::f();
@@ -1755,4 +1807,5 @@ fn main() {
17551807
operators::f();
17561808
async_::f();
17571809
impl_trait::f();
1810+
indexers::f();
17581811
}

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

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2462,9 +2462,66 @@ inferType
24622462
| main.rs:1733:13:1733:13 | d | | main.rs:1681:5:1681:14 | S2 |
24632463
| main.rs:1733:17:1733:34 | uses_my_trait2(...) | | main.rs:1681:5:1681:14 | S2 |
24642464
| main.rs:1733:32:1733:33 | S1 | | main.rs:1680:5:1680:14 | S1 |
2465-
| main.rs:1739:5:1739:20 | ...::f(...) | | main.rs:67:5:67:21 | Foo |
2466-
| main.rs:1740:5:1740:60 | ...::g(...) | | main.rs:67:5:67:21 | Foo |
2467-
| main.rs:1740:20:1740:38 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |
2468-
| main.rs:1740:41:1740:59 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |
2469-
| main.rs:1756:5:1756:15 | ...::f(...) | | {EXTERNAL LOCATION} | trait Future |
2465+
| main.rs:1744:16:1744:20 | SelfParam | | file://:0:0:0:0 | & |
2466+
| main.rs:1744:16:1744:20 | SelfParam | &T | main.rs:1740:5:1741:13 | S |
2467+
| main.rs:1744:31:1746:9 | { ... } | | main.rs:1740:5:1741:13 | S |
2468+
| main.rs:1745:13:1745:13 | S | | main.rs:1740:5:1741:13 | S |
2469+
| main.rs:1755:26:1757:9 | { ... } | | main.rs:1749:5:1752:5 | MyVec |
2470+
| main.rs:1755:26:1757:9 | { ... } | T | main.rs:1754:10:1754:10 | T |
2471+
| main.rs:1756:13:1756:38 | MyVec {...} | | main.rs:1749:5:1752:5 | MyVec |
2472+
| main.rs:1756:13:1756:38 | MyVec {...} | T | main.rs:1754:10:1754:10 | T |
2473+
| main.rs:1756:27:1756:36 | ...::new(...) | | {EXTERNAL LOCATION} | Vec |
2474+
| main.rs:1756:27:1756:36 | ...::new(...) | T | main.rs:1754:10:1754:10 | T |
2475+
| main.rs:1759:17:1759:25 | SelfParam | | file://:0:0:0:0 | & |
2476+
| main.rs:1759:17:1759:25 | SelfParam | &T | main.rs:1749:5:1752:5 | MyVec |
2477+
| main.rs:1759:17:1759:25 | SelfParam | &T.T | main.rs:1754:10:1754:10 | T |
2478+
| main.rs:1759:28:1759:32 | value | | main.rs:1754:10:1754:10 | T |
2479+
| main.rs:1760:13:1760:16 | self | | file://:0:0:0:0 | & |
2480+
| main.rs:1760:13:1760:16 | self | &T | main.rs:1749:5:1752:5 | MyVec |
2481+
| main.rs:1760:13:1760:16 | self | &T.T | main.rs:1754:10:1754:10 | T |
2482+
| main.rs:1760:13:1760:21 | self.data | | {EXTERNAL LOCATION} | Vec |
2483+
| main.rs:1760:13:1760:21 | self.data | T | main.rs:1754:10:1754:10 | T |
2484+
| main.rs:1760:28:1760:32 | value | | main.rs:1754:10:1754:10 | T |
2485+
| main.rs:1768:18:1768:22 | SelfParam | | file://:0:0:0:0 | & |
2486+
| main.rs:1768:18:1768:22 | SelfParam | &T | main.rs:1749:5:1752:5 | MyVec |
2487+
| main.rs:1768:18:1768:22 | SelfParam | &T.T | main.rs:1764:10:1764:10 | T |
2488+
| main.rs:1768:25:1768:29 | index | | {EXTERNAL LOCATION} | usize |
2489+
| main.rs:1768:56:1770:9 | { ... } | | file://:0:0:0:0 | & |
2490+
| main.rs:1768:56:1770:9 | { ... } | &T | main.rs:1764:10:1764:10 | T |
2491+
| main.rs:1769:13:1769:29 | &... | | file://:0:0:0:0 | & |
2492+
| main.rs:1769:13:1769:29 | &... | &T | main.rs:1764:10:1764:10 | T |
2493+
| main.rs:1769:14:1769:17 | self | | file://:0:0:0:0 | & |
2494+
| main.rs:1769:14:1769:17 | self | &T | main.rs:1749:5:1752:5 | MyVec |
2495+
| main.rs:1769:14:1769:17 | self | &T.T | main.rs:1764:10:1764:10 | T |
2496+
| main.rs:1769:14:1769:22 | self.data | | {EXTERNAL LOCATION} | Vec |
2497+
| main.rs:1769:14:1769:22 | self.data | T | main.rs:1764:10:1764:10 | T |
2498+
| main.rs:1769:24:1769:28 | index | | {EXTERNAL LOCATION} | usize |
2499+
| main.rs:1773:22:1773:26 | slice | | file://:0:0:0:0 | & |
2500+
| main.rs:1774:17:1774:21 | slice | | file://:0:0:0:0 | & |
2501+
| main.rs:1774:23:1774:23 | 0 | | {EXTERNAL LOCATION} | i32 |
2502+
| main.rs:1778:13:1778:19 | mut vec | | main.rs:1749:5:1752:5 | MyVec |
2503+
| main.rs:1778:13:1778:19 | mut vec | T | main.rs:1740:5:1741:13 | S |
2504+
| main.rs:1778:23:1778:34 | ...::new(...) | | main.rs:1749:5:1752:5 | MyVec |
2505+
| main.rs:1778:23:1778:34 | ...::new(...) | T | main.rs:1740:5:1741:13 | S |
2506+
| main.rs:1779:9:1779:11 | vec | | main.rs:1749:5:1752:5 | MyVec |
2507+
| main.rs:1779:9:1779:11 | vec | T | main.rs:1740:5:1741:13 | S |
2508+
| main.rs:1779:18:1779:18 | S | | main.rs:1740:5:1741:13 | S |
2509+
| main.rs:1780:9:1780:11 | vec | | main.rs:1749:5:1752:5 | MyVec |
2510+
| main.rs:1780:9:1780:11 | vec | T | main.rs:1740:5:1741:13 | S |
2511+
| main.rs:1780:13:1780:13 | 0 | | {EXTERNAL LOCATION} | i32 |
2512+
| main.rs:1782:13:1782:14 | xs | | file://:0:0:0:0 | [] |
2513+
| main.rs:1782:21:1782:21 | 1 | | {EXTERNAL LOCATION} | i32 |
2514+
| main.rs:1782:26:1782:28 | [...] | | file://:0:0:0:0 | [] |
2515+
| main.rs:1782:27:1782:27 | S | | main.rs:1740:5:1741:13 | S |
2516+
| main.rs:1783:17:1783:18 | xs | | file://:0:0:0:0 | [] |
2517+
| main.rs:1783:20:1783:20 | 0 | | {EXTERNAL LOCATION} | i32 |
2518+
| main.rs:1785:23:1785:25 | &xs | | file://:0:0:0:0 | & |
2519+
| main.rs:1785:23:1785:25 | &xs | &T | file://:0:0:0:0 | [] |
2520+
| main.rs:1785:24:1785:25 | xs | | file://:0:0:0:0 | [] |
2521+
| main.rs:1791:5:1791:20 | ...::f(...) | | main.rs:67:5:67:21 | Foo |
2522+
| main.rs:1792:5:1792:60 | ...::g(...) | | main.rs:67:5:67:21 | Foo |
2523+
| main.rs:1792:20:1792:38 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |
2524+
| main.rs:1792:41:1792:59 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |
2525+
| main.rs:1808:5:1808:15 | ...::f(...) | | {EXTERNAL LOCATION} | trait Future |
24702526
testFailures
2527+
| main.rs:1760:13:1760:33 | ... .push(...) | Unexpected result: method=push |

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ module TypeTest implements TestSig {
5555
exists(AstNode n, TypePath path, Type t |
5656
t = TypeInference::inferType(n, path) and
5757
location = n.getLocation() and
58-
element = n.toString() and
5958
if path.isEmpty()
6059
then value = element + ":" + t
6160
else value = element + ":" + path.toString() + "." + t.toString()
61+
|
62+
element = n.toString()
63+
or
64+
element = n.(IdentPat).getName().getText()
6265
)
6366
}
6467
}

0 commit comments

Comments
 (0)