Skip to content

Commit 301bd44

Browse files
committed
Rust: Add type inference tests for index expressions
1 parent 75caa18 commit 301bd44

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
@@ -1753,6 +1753,58 @@ mod impl_trait {
17531753
}
17541754
}
17551755

1756+
mod indexers {
1757+
use std::ops::Index;
1758+
1759+
#[derive(Debug)]
1760+
struct S;
1761+
1762+
impl S {
1763+
fn foo(&self) -> Self {
1764+
S
1765+
}
1766+
}
1767+
1768+
#[derive(Debug)]
1769+
struct MyVec<T> {
1770+
data: Vec<T>,
1771+
}
1772+
1773+
impl<T> MyVec<T> {
1774+
fn new() -> Self {
1775+
MyVec { data: Vec::new() }
1776+
}
1777+
1778+
fn push(&mut self, value: T) {
1779+
self.data.push(value); // $ fieldof=MyVec method=push
1780+
}
1781+
}
1782+
1783+
impl<T> Index<usize> for MyVec<T> {
1784+
type Output = T;
1785+
1786+
// MyVec::index
1787+
fn index(&self, index: usize) -> &Self::Output {
1788+
&self.data[index] // $ fieldof=MyVec
1789+
}
1790+
}
1791+
1792+
fn analyze_slice(slice: &[S]) {
1793+
let x = slice[0].foo(); // $ MISSING: method=foo MISSING: type=x:S
1794+
}
1795+
1796+
pub fn f() {
1797+
let mut vec = MyVec::new(); // $ type=vec:T.S
1798+
vec.push(S); // $ method=push
1799+
vec[0].foo(); // $ MISSING: method=foo
1800+
1801+
let xs: [S; 1] = [S];
1802+
let x = xs[0].foo(); // $ MISSING: method=foo MISSING: type=x:S
1803+
1804+
analyze_slice(&xs);
1805+
}
1806+
}
1807+
17561808
fn main() {
17571809
field_access::f();
17581810
method_impl::f();
@@ -1774,4 +1826,5 @@ fn main() {
17741826
operators::f();
17751827
async_::f();
17761828
impl_trait::f();
1829+
indexers::f();
17771830
}

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

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2525,9 +2525,66 @@ inferType
25252525
| main.rs:1752:13:1752:13 | d | | main.rs:1700:5:1700:14 | S2 |
25262526
| main.rs:1752:17:1752:34 | uses_my_trait2(...) | | main.rs:1700:5:1700:14 | S2 |
25272527
| main.rs:1752:32:1752:33 | S1 | | main.rs:1699:5:1699:14 | S1 |
2528-
| main.rs:1758:5:1758:20 | ...::f(...) | | main.rs:67:5:67:21 | Foo |
2529-
| main.rs:1759:5:1759:60 | ...::g(...) | | main.rs:67:5:67:21 | Foo |
2530-
| main.rs:1759:20:1759:38 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |
2531-
| main.rs:1759:41:1759:59 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |
2532-
| main.rs:1775:5:1775:15 | ...::f(...) | | {EXTERNAL LOCATION} | trait Future |
2528+
| main.rs:1763:16:1763:20 | SelfParam | | file://:0:0:0:0 | & |
2529+
| main.rs:1763:16:1763:20 | SelfParam | &T | main.rs:1759:5:1760:13 | S |
2530+
| main.rs:1763:31:1765:9 | { ... } | | main.rs:1759:5:1760:13 | S |
2531+
| main.rs:1764:13:1764:13 | S | | main.rs:1759:5:1760:13 | S |
2532+
| main.rs:1774:26:1776:9 | { ... } | | main.rs:1768:5:1771:5 | MyVec |
2533+
| main.rs:1774:26:1776:9 | { ... } | T | main.rs:1773:10:1773:10 | T |
2534+
| main.rs:1775:13:1775:38 | MyVec {...} | | main.rs:1768:5:1771:5 | MyVec |
2535+
| main.rs:1775:13:1775:38 | MyVec {...} | T | main.rs:1773:10:1773:10 | T |
2536+
| main.rs:1775:27:1775:36 | ...::new(...) | | {EXTERNAL LOCATION} | Vec |
2537+
| main.rs:1775:27:1775:36 | ...::new(...) | T | main.rs:1773:10:1773:10 | T |
2538+
| main.rs:1778:17:1778:25 | SelfParam | | file://:0:0:0:0 | & |
2539+
| main.rs:1778:17:1778:25 | SelfParam | &T | main.rs:1768:5:1771:5 | MyVec |
2540+
| main.rs:1778:17:1778:25 | SelfParam | &T.T | main.rs:1773:10:1773:10 | T |
2541+
| main.rs:1778:28:1778:32 | value | | main.rs:1773:10:1773:10 | T |
2542+
| main.rs:1779:13:1779:16 | self | | file://:0:0:0:0 | & |
2543+
| main.rs:1779:13:1779:16 | self | &T | main.rs:1768:5:1771:5 | MyVec |
2544+
| main.rs:1779:13:1779:16 | self | &T.T | main.rs:1773:10:1773:10 | T |
2545+
| main.rs:1779:13:1779:21 | self.data | | {EXTERNAL LOCATION} | Vec |
2546+
| main.rs:1779:13:1779:21 | self.data | T | main.rs:1773:10:1773:10 | T |
2547+
| main.rs:1779:28:1779:32 | value | | main.rs:1773:10:1773:10 | T |
2548+
| main.rs:1787:18:1787:22 | SelfParam | | file://:0:0:0:0 | & |
2549+
| main.rs:1787:18:1787:22 | SelfParam | &T | main.rs:1768:5:1771:5 | MyVec |
2550+
| main.rs:1787:18:1787:22 | SelfParam | &T.T | main.rs:1783:10:1783:10 | T |
2551+
| main.rs:1787:25:1787:29 | index | | {EXTERNAL LOCATION} | usize |
2552+
| main.rs:1787:56:1789:9 | { ... } | | file://:0:0:0:0 | & |
2553+
| main.rs:1787:56:1789:9 | { ... } | &T | main.rs:1783:10:1783:10 | T |
2554+
| main.rs:1788:13:1788:29 | &... | | file://:0:0:0:0 | & |
2555+
| main.rs:1788:13:1788:29 | &... | &T | main.rs:1783:10:1783:10 | T |
2556+
| main.rs:1788:14:1788:17 | self | | file://:0:0:0:0 | & |
2557+
| main.rs:1788:14:1788:17 | self | &T | main.rs:1768:5:1771:5 | MyVec |
2558+
| main.rs:1788:14:1788:17 | self | &T.T | main.rs:1783:10:1783:10 | T |
2559+
| main.rs:1788:14:1788:22 | self.data | | {EXTERNAL LOCATION} | Vec |
2560+
| main.rs:1788:14:1788:22 | self.data | T | main.rs:1783:10:1783:10 | T |
2561+
| main.rs:1788:14:1788:29 | ...[index] | | main.rs:1783:10:1783:10 | T |
2562+
| main.rs:1788:24:1788:28 | index | | {EXTERNAL LOCATION} | usize |
2563+
| main.rs:1792:22:1792:26 | slice | | file://:0:0:0:0 | & |
2564+
| main.rs:1793:17:1793:21 | slice | | file://:0:0:0:0 | & |
2565+
| main.rs:1793:23:1793:23 | 0 | | {EXTERNAL LOCATION} | i32 |
2566+
| main.rs:1797:13:1797:19 | mut vec | | main.rs:1768:5:1771:5 | MyVec |
2567+
| main.rs:1797:13:1797:19 | mut vec | T | main.rs:1759:5:1760:13 | S |
2568+
| main.rs:1797:23:1797:34 | ...::new(...) | | main.rs:1768:5:1771:5 | MyVec |
2569+
| main.rs:1797:23:1797:34 | ...::new(...) | T | main.rs:1759:5:1760:13 | S |
2570+
| main.rs:1798:9:1798:11 | vec | | main.rs:1768:5:1771:5 | MyVec |
2571+
| main.rs:1798:9:1798:11 | vec | T | main.rs:1759:5:1760:13 | S |
2572+
| main.rs:1798:18:1798:18 | S | | main.rs:1759:5:1760:13 | S |
2573+
| main.rs:1799:9:1799:11 | vec | | main.rs:1768:5:1771:5 | MyVec |
2574+
| main.rs:1799:9:1799:11 | vec | T | main.rs:1759:5:1760:13 | S |
2575+
| main.rs:1799:13:1799:13 | 0 | | {EXTERNAL LOCATION} | i32 |
2576+
| main.rs:1801:13:1801:14 | xs | | file://:0:0:0:0 | [] |
2577+
| main.rs:1801:21:1801:21 | 1 | | {EXTERNAL LOCATION} | i32 |
2578+
| main.rs:1801:26:1801:28 | [...] | | file://:0:0:0:0 | [] |
2579+
| main.rs:1801:27:1801:27 | S | | main.rs:1759:5:1760:13 | S |
2580+
| main.rs:1802:17:1802:18 | xs | | file://:0:0:0:0 | [] |
2581+
| main.rs:1802:20:1802:20 | 0 | | {EXTERNAL LOCATION} | i32 |
2582+
| main.rs:1804:23:1804:25 | &xs | | file://:0:0:0:0 | & |
2583+
| main.rs:1804:23:1804:25 | &xs | &T | file://:0:0:0:0 | [] |
2584+
| main.rs:1804:24:1804:25 | xs | | file://:0:0:0:0 | [] |
2585+
| main.rs:1810:5:1810:20 | ...::f(...) | | main.rs:67:5:67:21 | Foo |
2586+
| main.rs:1811:5:1811:60 | ...::g(...) | | main.rs:67:5:67:21 | Foo |
2587+
| main.rs:1811:20:1811:38 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |
2588+
| main.rs:1811:41:1811:59 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |
2589+
| main.rs:1827:5:1827:15 | ...::f(...) | | {EXTERNAL LOCATION} | trait Future |
25332590
testFailures

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)