Skip to content

Commit bf8b75d

Browse files
committed
Rust: Add type inference tests
1 parent 7cbaa11 commit bf8b75d

File tree

4 files changed

+1999
-1778
lines changed

4 files changed

+1999
-1778
lines changed
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
multipleCallTargets
2-
| dereference.rs:61:15:61:24 | e1.deref() |
3-
| main.rs:2357:13:2357:31 | ...::from(...) |
4-
| main.rs:2358:13:2358:31 | ...::from(...) |
5-
| main.rs:2359:13:2359:31 | ...::from(...) |
6-
| main.rs:2365:13:2365:31 | ...::from(...) |
7-
| main.rs:2366:13:2366:31 | ...::from(...) |
8-
| main.rs:2367:13:2367:31 | ...::from(...) |
2+
| dereference.rs:69:15:69:24 | e1.deref() |
3+
| dereference.rs:186:17:186:25 | S.bar(...) |
4+
| dereference.rs:187:17:187:29 | S.bar(...) |
5+
| main.rs:2372:13:2372:31 | ...::from(...) |
6+
| main.rs:2373:13:2373:31 | ...::from(...) |
7+
| main.rs:2374:13:2374:31 | ...::from(...) |
8+
| main.rs:2380:13:2380:31 | ...::from(...) |
9+
| main.rs:2381:13:2381:31 | ...::from(...) |
10+
| main.rs:2382:13:2382:31 | ...::from(...) |

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

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ impl<T> Deref for MySmartPointer<T> {
2727
}
2828
}
2929

30+
struct S<T>(T);
31+
32+
impl<T> S<T> {
33+
fn foo(&self) -> &T {
34+
&self.0 // $ fieldof=S
35+
}
36+
}
37+
3038
fn explicit_monomorphic_dereference() {
3139
// Dereference with method call
3240
let a1 = MyIntPointer { value: 34i64 };
@@ -91,6 +99,9 @@ fn implicit_dereference() {
9199
// Call method on implicitly dereferenced value
92100
let x = MySmartPointer { value: 34i64 };
93101
let _y = x.is_positive(); // $ MISSING: target=is_positive type=_y:bool
102+
103+
let z = MySmartPointer { value: S(0i64) };
104+
let z_ = z.foo(); // $ MISSING: target=foo type=z_:&T.i64
94105
}
95106

96107
mod implicit_deref_coercion_cycle {
@@ -128,11 +139,90 @@ mod implicit_deref_coercion_cycle {
128139
}
129140
}
130141

142+
mod ref_vs_mut_ref {
143+
trait MyTrait1<T> {
144+
fn foo(self) -> T;
145+
}
146+
147+
struct S;
148+
149+
impl MyTrait1<S> for &S {
150+
// MyTrait1::foo1
151+
fn foo(self) -> S {
152+
S
153+
}
154+
}
155+
156+
impl MyTrait1<i64> for &mut S {
157+
// MyTrait1::foo2
158+
fn foo(self) -> i64 {
159+
42
160+
}
161+
}
162+
163+
trait MyTrait2<T1, T2> {
164+
fn bar(self, arg: T1) -> T2;
165+
}
166+
167+
impl MyTrait2<&S, S> for S {
168+
// MyTrait2::bar1
169+
fn bar(self, arg: &S) -> S {
170+
S
171+
}
172+
}
173+
174+
impl MyTrait2<&mut S, i64> for S {
175+
// MyTrait2::bar2
176+
fn bar(self, arg: &mut S) -> i64 {
177+
42
178+
}
179+
}
180+
181+
pub fn test() {
182+
let x = (&S).foo(); // $ MISSING: target=MyTrait1::foo1 type=x:S
183+
let y = S.foo(); // $ MISSING: target=MyTrait1::foo1 type=y:S
184+
let z = (&mut S).foo(); // $ MISSING: target=MyTrait1::foo2 type=z:i64
185+
186+
let x = S.bar(&S); // $ target=MyTrait2::bar1 type=x:S $ SPURIOUS: target=MyTrait2::bar2
187+
let y = S.bar(&mut S); // $ target=MyTrait2::bar2 type=y:i64 $ SPURIOUS: target=MyTrait2::bar1
188+
}
189+
}
190+
191+
// from https://doc.rust-lang.org/reference/expressions/method-call-expr.html#r-expr.method.candidate-search
192+
mod rust_reference_example {
193+
struct Foo {}
194+
195+
trait Bar {
196+
fn bar(&self);
197+
}
198+
199+
impl Foo {
200+
// bar1
201+
fn bar(&mut self) {
202+
println!("In struct impl!")
203+
}
204+
}
205+
206+
impl Bar for Foo {
207+
// bar2
208+
fn bar(&self) {
209+
println!("In trait impl!")
210+
}
211+
}
212+
213+
pub fn main() {
214+
let mut f = Foo {};
215+
f.bar(); // $ SPURIOUS: target=bar1 $ MISSING: target=bar2
216+
}
217+
}
218+
131219
pub fn test() {
132220
explicit_monomorphic_dereference(); // $ target=explicit_monomorphic_dereference
133221
explicit_polymorphic_dereference(); // $ target=explicit_polymorphic_dereference
134222
explicit_ref_dereference(); // $ target=explicit_ref_dereference
135223
explicit_box_dereference(); // $ target=explicit_box_dereference
136224
implicit_dereference(); // $ target=implicit_dereference
137225
implicit_deref_coercion_cycle::test(); // $ target=test
226+
ref_vs_mut_ref::test(); // $ target=test
227+
rust_reference_example::main(); // $ target=main
138228
}

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -958,36 +958,36 @@ mod generic_enum {
958958
}
959959

960960
mod method_supertraits {
961-
#[derive(Debug)]
961+
#[derive(Debug, Clone, Copy)]
962962
struct MyThing<A> {
963963
a: A,
964964
}
965965

966-
#[derive(Debug)]
966+
#[derive(Debug, Clone, Copy)]
967967
struct MyThing2<A> {
968968
a: A,
969969
}
970970

971-
#[derive(Debug)]
971+
#[derive(Debug, Clone, Copy)]
972972
struct S1;
973-
#[derive(Debug)]
973+
#[derive(Debug, Clone, Copy)]
974974
struct S2;
975975

976976
trait MyTrait1<Tr1> {
977977
// MyTrait1::m1
978978
fn m1(self) -> Tr1;
979979
}
980980

981-
trait MyTrait2<Tr2>: MyTrait1<Tr2> {
981+
trait MyTrait2<Tr2>: MyTrait1<Tr2> + Copy {
982982
#[rustfmt::skip]
983-
fn m2(self) -> Tr2
983+
fn m2(&self) -> Tr2
984984
where
985985
Self: Sized,
986986
{
987987
if 3 > 2 { // $ target=gt
988988
self.m1() // $ target=MyTrait1::m1
989989
} else {
990-
Self::m1(self) // $ target=MyTrait1::m1
990+
Self::m1(*self) // $ target=deref target=MyTrait1::m1
991991
}
992992
}
993993
}
@@ -1001,7 +1001,7 @@ mod method_supertraits {
10011001
if 3 > 2 { // $ target=gt
10021002
self.m2().a // $ target=m2 $ fieldof=MyThing
10031003
} else {
1004-
Self::m2(self).a // $ target=m2 fieldof=MyThing
1004+
Self::m2(&self).a // $ target=m2 fieldof=MyThing
10051005
}
10061006
}
10071007
}
@@ -1013,7 +1013,7 @@ mod method_supertraits {
10131013
}
10141014
}
10151015

1016-
impl<T> MyTrait2<T> for MyThing<T> {}
1016+
impl<T: Copy> MyTrait2<T> for MyThing<T> {}
10171017

10181018
impl<T> MyTrait1<MyThing<T>> for MyThing2<T> {
10191019
// MyThing2::m1
@@ -1022,9 +1022,9 @@ mod method_supertraits {
10221022
}
10231023
}
10241024

1025-
impl<T> MyTrait2<MyThing<T>> for MyThing2<T> {}
1025+
impl<T: Copy> MyTrait2<MyThing<T>> for MyThing2<T> {}
10261026

1027-
impl<T> MyTrait3<T> for MyThing2<T> {}
1027+
impl<T: Copy> MyTrait3<T> for MyThing2<T> {}
10281028

10291029
fn call_trait_m1<T1, T2: MyTrait1<T1>>(x: T2) -> T1 {
10301030
x.m1() // $ target=MyTrait1::m1
@@ -1819,6 +1819,11 @@ mod overloadable_operators {
18191819
self.x >= other.x && self.y >= other.y // $ fieldof=Vec2 target=ge
18201820
}
18211821
}
1822+
1823+
fn param_add<T: Add>(a: T, b: T) -> T::Output {
1824+
a + b // $ target=add
1825+
}
1826+
18221827
pub fn f() {
18231828
// Test for all overloadable operators on `i64`
18241829

@@ -1836,6 +1841,7 @@ mod overloadable_operators {
18361841
let i64_mul = 17i64 * 18i64; // $ type=i64_mul:i64 target=mul
18371842
let i64_div = 19i64 / 20i64; // $ type=i64_div:i64 target=div
18381843
let i64_rem = 21i64 % 22i64; // $ type=i64_rem:i64 target=rem
1844+
let i64_param_add = param_add(1i64, 2i64); // $ target=param_add $ MISSING: type=i64_param_add:i64
18391845

18401846
// Arithmetic assignment operators
18411847
let mut i64_add_assign = 23i64;
@@ -2083,7 +2089,7 @@ mod impl_trait {
20832089
mod indexers {
20842090
use std::ops::Index;
20852091

2086-
#[derive(Debug)]
2092+
#[derive(Debug, Copy, Clone)]
20872093
struct S;
20882094

20892095
impl S {
@@ -2120,6 +2126,13 @@ mod indexers {
21202126
let x = slice[0].foo(); // $ target=foo type=x:S target=index
21212127
}
21222128

2129+
fn param_index<T: Index<usize>>(a: T, b: usize) -> T::Output
2130+
where
2131+
<T as Index<usize>>::Output: Sized + Copy,
2132+
{
2133+
a[b] // $ target=index
2134+
}
2135+
21232136
pub fn f() {
21242137
let mut vec = MyVec::new(); // $ type=vec:T.S target=new
21252138
vec.push(S); // $ target=push
@@ -2128,6 +2141,8 @@ mod indexers {
21282141
let xs: [S; 1] = [S];
21292142
let x = xs[0].foo(); // $ target=foo type=x:S target=index
21302143

2144+
let y = param_index(vec, 0); // $ target=param_index $ MISSING: type=y:S
2145+
21312146
analyze_slice(&xs); // $ target=analyze_slice
21322147
}
21332148
}

0 commit comments

Comments
 (0)