Skip to content

Commit 89774c7

Browse files
committed
Rust: Add type inference tests
1 parent 6453b71 commit 89774c7

File tree

4 files changed

+440
-260
lines changed

4 files changed

+440
-260
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
multipleCallTargets
2-
| dereference.rs:61:15:61:24 | e1.deref() |
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(...) |
35
| main.rs:2308:13:2308:31 | ...::from(...) |
46
| main.rs:2309:13:2309:31 | ...::from(...) |
57
| main.rs:2310:13:2310: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: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2558,25 +2558,23 @@ pub mod exec {
25582558
pub mod path_buf {
25592559
// a highly simplified model of `PathBuf::canonicalize`
25602560

2561-
pub struct Path {
2562-
}
2561+
pub struct Path {}
25632562

25642563
impl Path {
25652564
pub const fn new() -> Path {
2566-
Path { }
2565+
Path {}
25672566
}
25682567

25692568
pub fn canonicalize(&self) -> Result<PathBuf, ()> {
25702569
Ok(PathBuf::new()) // $ target=new
25712570
}
25722571
}
25732572

2574-
pub struct PathBuf {
2575-
}
2573+
pub struct PathBuf {}
25762574

25772575
impl PathBuf {
25782576
pub const fn new() -> PathBuf {
2579-
PathBuf { }
2577+
PathBuf {}
25802578
}
25812579
}
25822580

@@ -2587,7 +2585,7 @@ pub mod path_buf {
25872585
#[inline]
25882586
fn deref(&self) -> &Path {
25892587
// (very much not a real implementation)
2590-
static path : Path = Path::new(); // $ target=new
2588+
static path: Path = Path::new(); // $ target=new
25912589
&path
25922590
}
25932591
}

0 commit comments

Comments
 (0)