Skip to content

Commit d1aee7f

Browse files
committed
Rust: Add type inference test
1 parent 6cca016 commit d1aee7f

File tree

2 files changed

+1293
-1193
lines changed

2 files changed

+1293
-1193
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;
@@ -1816,6 +1815,44 @@ mod macros {
18161815
}
18171816
}
18181817

1818+
mod method_determined_by_argument_type {
1819+
trait MyAdd<T> {
1820+
fn my_add(&self, value: T) -> Self;
1821+
}
1822+
1823+
impl MyAdd<i64> for i64 {
1824+
// MyAdd<i64>::my_add
1825+
fn my_add(&self, value: i64) -> Self {
1826+
value
1827+
}
1828+
}
1829+
1830+
impl MyAdd<&i64> for i64 {
1831+
// MyAdd<&i64>::my_add
1832+
fn my_add(&self, value: &i64) -> Self {
1833+
*value // $ method=deref
1834+
}
1835+
}
1836+
1837+
impl MyAdd<bool> for i64 {
1838+
// MyAdd<bool>::my_add
1839+
fn my_add(&self, value: bool) -> Self {
1840+
if value {
1841+
1
1842+
} else {
1843+
0
1844+
}
1845+
}
1846+
}
1847+
1848+
pub fn f() {
1849+
let x: i64 = 73;
1850+
x.my_add(5i64); // $ method=MyAdd<i64>::my_add SPURIOUS: method=MyAdd<bool>::my_add SPURIOUS: method=MyAdd<&i64>::my_add
1851+
x.my_add(&5i64); // $ method=MyAdd<&i64>::my_add SPURIOUS: method=MyAdd<i64>::my_add SPURIOUS: method=MyAdd<bool>::my_add
1852+
x.my_add(true); // $ method=MyAdd<bool>::my_add SPURIOUS: method=MyAdd<i64>::my_add SPURIOUS: method=MyAdd<&i64>::my_add
1853+
}
1854+
}
1855+
18191856
fn main() {
18201857
field_access::f();
18211858
method_impl::f();
@@ -1839,4 +1876,5 @@ fn main() {
18391876
impl_trait::f();
18401877
indexers::f();
18411878
macros::f();
1879+
method_determined_by_argument_type::f();
18421880
}

0 commit comments

Comments
 (0)