Skip to content

Commit 6337374

Browse files
committed
Rust: Add type inference tests
1 parent 1130595 commit 6337374

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:2308:13:2308:31 | ...::from(...) |
4-
| main.rs:2309:13:2309:31 | ...::from(...) |
5-
| main.rs:2310:13:2310:31 | ...::from(...) |
6-
| main.rs:2316:13:2316:31 | ...::from(...) |
7-
| main.rs:2317:13:2317:31 | ...::from(...) |
8-
| main.rs:2318:13:2318: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:2323:13:2323:31 | ...::from(...) |
6+
| main.rs:2324:13:2324:31 | ...::from(...) |
7+
| main.rs:2325:13:2325:31 | ...::from(...) |
8+
| main.rs:2331:13:2331:31 | ...::from(...) |
9+
| main.rs:2332:13:2332:31 | ...::from(...) |
10+
| main.rs:2333:13:2333: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
@@ -909,36 +909,36 @@ mod generic_enum {
909909
}
910910

911911
mod method_supertraits {
912-
#[derive(Debug)]
912+
#[derive(Debug, Clone, Copy)]
913913
struct MyThing<A> {
914914
a: A,
915915
}
916916

917-
#[derive(Debug)]
917+
#[derive(Debug, Clone, Copy)]
918918
struct MyThing2<A> {
919919
a: A,
920920
}
921921

922-
#[derive(Debug)]
922+
#[derive(Debug, Clone, Copy)]
923923
struct S1;
924-
#[derive(Debug)]
924+
#[derive(Debug, Clone, Copy)]
925925
struct S2;
926926

927927
trait MyTrait1<Tr1> {
928928
// MyTrait1::m1
929929
fn m1(self) -> Tr1;
930930
}
931931

932-
trait MyTrait2<Tr2>: MyTrait1<Tr2> {
932+
trait MyTrait2<Tr2>: MyTrait1<Tr2> + Copy {
933933
#[rustfmt::skip]
934-
fn m2(self) -> Tr2
934+
fn m2(&self) -> Tr2
935935
where
936936
Self: Sized,
937937
{
938938
if 3 > 2 { // $ target=gt
939939
self.m1() // $ target=MyTrait1::m1
940940
} else {
941-
Self::m1(self) // $ target=MyTrait1::m1
941+
Self::m1(*self) // $ target=deref target=MyTrait1::m1
942942
}
943943
}
944944
}
@@ -952,7 +952,7 @@ mod method_supertraits {
952952
if 3 > 2 { // $ target=gt
953953
self.m2().a // $ target=m2 $ fieldof=MyThing
954954
} else {
955-
Self::m2(self).a // $ target=m2 fieldof=MyThing
955+
Self::m2(&self).a // $ target=m2 fieldof=MyThing
956956
}
957957
}
958958
}
@@ -964,7 +964,7 @@ mod method_supertraits {
964964
}
965965
}
966966

967-
impl<T> MyTrait2<T> for MyThing<T> {}
967+
impl<T: Copy> MyTrait2<T> for MyThing<T> {}
968968

969969
impl<T> MyTrait1<MyThing<T>> for MyThing2<T> {
970970
// MyThing2::m1
@@ -973,9 +973,9 @@ mod method_supertraits {
973973
}
974974
}
975975

976-
impl<T> MyTrait2<MyThing<T>> for MyThing2<T> {}
976+
impl<T: Copy> MyTrait2<MyThing<T>> for MyThing2<T> {}
977977

978-
impl<T> MyTrait3<T> for MyThing2<T> {}
978+
impl<T: Copy> MyTrait3<T> for MyThing2<T> {}
979979

980980
fn call_trait_m1<T1, T2: MyTrait1<T1>>(x: T2) -> T1 {
981981
x.m1() // $ target=MyTrait1::m1
@@ -1770,6 +1770,11 @@ mod overloadable_operators {
17701770
self.x >= other.x && self.y >= other.y // $ fieldof=Vec2 target=ge
17711771
}
17721772
}
1773+
1774+
fn param_add<T: Add>(a: T, b: T) -> T::Output {
1775+
a + b // $ target=add
1776+
}
1777+
17731778
pub fn f() {
17741779
// Test for all overloadable operators on `i64`
17751780

@@ -1787,6 +1792,7 @@ mod overloadable_operators {
17871792
let i64_mul = 17i64 * 18i64; // $ type=i64_mul:i64 target=mul
17881793
let i64_div = 19i64 / 20i64; // $ type=i64_div:i64 target=div
17891794
let i64_rem = 21i64 % 22i64; // $ type=i64_rem:i64 target=rem
1795+
let i64_param_add = param_add(1i64, 2i64); // $ target=param_add $ MISSING: type=i64_param_add:i64
17901796

17911797
// Arithmetic assignment operators
17921798
let mut i64_add_assign = 23i64;
@@ -2034,7 +2040,7 @@ mod impl_trait {
20342040
mod indexers {
20352041
use std::ops::Index;
20362042

2037-
#[derive(Debug)]
2043+
#[derive(Debug, Copy, Clone)]
20382044
struct S;
20392045

20402046
impl S {
@@ -2071,6 +2077,13 @@ mod indexers {
20712077
let x = slice[0].foo(); // $ target=foo type=x:S target=index
20722078
}
20732079

2080+
fn param_index<T: Index<usize>>(a: T, b: usize) -> T::Output
2081+
where
2082+
<T as Index<usize>>::Output: Sized + Copy,
2083+
{
2084+
a[b] // $ target=index
2085+
}
2086+
20742087
pub fn f() {
20752088
let mut vec = MyVec::new(); // $ type=vec:T.S target=new
20762089
vec.push(S); // $ target=push
@@ -2079,6 +2092,8 @@ mod indexers {
20792092
let xs: [S; 1] = [S];
20802093
let x = xs[0].foo(); // $ target=foo type=x:S target=index
20812094

2095+
let y = param_index(vec, 0); // $ target=param_index $ MISSING: type=y:S
2096+
20822097
analyze_slice(&xs); // $ target=analyze_slice
20832098
}
20842099
}

0 commit comments

Comments
 (0)