Skip to content

Commit 474294e

Browse files
committed
Rust: Add type inference test
1 parent 8838104 commit 474294e

File tree

2 files changed

+1332
-1232
lines changed

2 files changed

+1332
-1232
lines changed

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

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,7 @@ mod method_call_type_conversion {
10991099
println!("{:?}", x5.0); // $ fieldof=S
11001100

11011101
let x6 = &S(S2); // $ SPURIOUS: type=x6:&T.&T.S
1102+
11021103
// explicit dereference
11031104
println!("{:?}", (*x6).m1()); // $ method=m1 method=deref
11041105

@@ -1668,17 +1669,18 @@ mod async_ {
16681669
}
16691670

16701671
fn f2() -> impl Future<Output = S1> {
1671-
async {
1672-
S1
1673-
}
1672+
async { S1 }
16741673
}
16751674

16761675
struct S2;
16771676

16781677
impl Future for S2 {
16791678
type Output = S1;
16801679

1681-
fn poll(self: std::pin::Pin<&mut Self>, _cx: &mut std::task::Context<'_>) -> std::task::Poll<Self::Output> {
1680+
fn poll(
1681+
self: std::pin::Pin<&mut Self>,
1682+
_cx: &mut std::task::Context<'_>,
1683+
) -> std::task::Poll<Self::Output> {
16821684
std::task::Poll::Ready(S1)
16831685
}
16841686
}
@@ -1692,14 +1694,11 @@ mod async_ {
16921694
f2().await.f(); // $ method=S1f
16931695
f3().await.f(); // $ method=S1f
16941696
S2.await.f(); // $ method=S1f
1695-
let b = async {
1696-
S1
1697-
};
1697+
let b = async { S1 };
16981698
b.await.f(); // $ method=S1f
16991699
}
17001700
}
17011701

1702-
17031702
mod impl_trait {
17041703
struct S1;
17051704
struct S2;
@@ -1810,6 +1809,44 @@ mod indexers {
18101809
}
18111810
}
18121811

1812+
mod method_determined_by_argument_type {
1813+
trait MyAdd<T> {
1814+
fn my_add(&self, value: T) -> Self;
1815+
}
1816+
1817+
impl MyAdd<i64> for i64 {
1818+
// MyAdd<i64>::my_add
1819+
fn my_add(&self, value: i64) -> Self {
1820+
value
1821+
}
1822+
}
1823+
1824+
impl MyAdd<&i64> for i64 {
1825+
// MyAdd<&i64>::my_add
1826+
fn my_add(&self, value: &i64) -> Self {
1827+
*value // $ method=deref
1828+
}
1829+
}
1830+
1831+
impl MyAdd<bool> for i64 {
1832+
// MyAdd<bool>::my_add
1833+
fn my_add(&self, value: bool) -> Self {
1834+
if value {
1835+
1
1836+
} else {
1837+
0
1838+
}
1839+
}
1840+
}
1841+
1842+
pub fn f() {
1843+
let x: i64 = 73;
1844+
x.my_add(5i64); // $ method=MyAdd<i64>::my_add SPURIOUS: method=MyAdd<bool>::my_add SPURIOUS: method=MyAdd<&i64>::my_add
1845+
x.my_add(&5i64); // $ method=MyAdd<&i64>::my_add SPURIOUS: method=MyAdd<i64>::my_add SPURIOUS: method=MyAdd<bool>::my_add
1846+
x.my_add(true); // $ method=MyAdd<bool>::my_add SPURIOUS: method=MyAdd<i64>::my_add SPURIOUS: method=MyAdd<&i64>::my_add
1847+
}
1848+
}
1849+
18131850
fn main() {
18141851
field_access::f();
18151852
method_impl::f();
@@ -1832,4 +1869,5 @@ fn main() {
18321869
async_::f();
18331870
impl_trait::f();
18341871
indexers::f();
1872+
method_determined_by_argument_type::f();
18351873
}

0 commit comments

Comments
 (0)