Skip to content

Commit 9bdb541

Browse files
committed
Rust: Add type inference tests for dereferencing
1 parent 699c82a commit 9bdb541

File tree

3 files changed

+345
-5
lines changed

3 files changed

+345
-5
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/// This file contains tests for dereferencing with through the `Deref` trait.
2+
use std::ops::Deref;
3+
4+
struct MyIntPointer {
5+
value: i64,
6+
}
7+
8+
impl Deref for MyIntPointer {
9+
type Target = i64;
10+
11+
// MyIntPointer::deref
12+
fn deref(&self) -> &i64 {
13+
&self.value // $ fieldof=MyIntPointer
14+
}
15+
}
16+
17+
struct MySmartPointer<T> {
18+
value: T,
19+
}
20+
21+
impl<T> Deref for MySmartPointer<T> {
22+
type Target = T;
23+
24+
// MySmartPointer::deref
25+
fn deref(&self) -> &T {
26+
&self.value // $ fieldof=MySmartPointer
27+
}
28+
}
29+
30+
fn explicit_monomorphic_dereference() {
31+
// Dereference with method call
32+
let a1 = MyIntPointer { value: 34i64 };
33+
let _b1 = a1.deref(); // $ method=MyIntPointer::deref type=_b1:&T.i64
34+
35+
// Dereference with overloaded dereference operator
36+
let a2 = MyIntPointer { value: 34i64 };
37+
let _b2 = *a2; // $ method=MyIntPointer::deref MISSING: type=_b2:i64
38+
39+
// Call method on explicitly dereferenced value
40+
let a3 = MyIntPointer { value: 34i64 };
41+
let _b3 = (*a3).is_positive(); // $ method=MyIntPointer::deref method=is_positive type=_b3:bool
42+
}
43+
44+
fn explicit_polymorphic_dereference() {
45+
// Explicit dereference with type parameter
46+
let c1 = MySmartPointer { value: 'a' };
47+
let _d1 = c1.deref(); // $ method=MySmartPointer::deref type=_d1:&T.char
48+
49+
// Explicit dereference with type parameter
50+
let c2 = MySmartPointer { value: 'a' };
51+
let _d2 = *c2; // $ method=MySmartPointer::deref MISSING: type=_d2:char
52+
53+
// Call method on explicitly dereferenced value with type parameter
54+
let c3 = MySmartPointer { value: 34i64 };
55+
let _d3 = (*c3).is_positive(); // $ method=MySmartPointer::deref MISSING: method=is_positive type=_d3:bool
56+
}
57+
58+
fn explicit_ref_dereference() {
59+
// Explicit dereference with type parameter
60+
let e1 = &'a';
61+
let _f1 = e1.deref(); // $ MISSING: method=deref type=_f1:&T.char
62+
63+
// Explicit dereference with type parameter
64+
let e2 = &'a';
65+
let _f2 = *e2; // $ method=deref type=_f2:char
66+
67+
// Call method on explicitly dereferenced value with type parameter
68+
let e3 = &34i64;
69+
let _f3 = (*e3).is_positive(); // $ method=deref method=is_positive type=_f3:bool
70+
}
71+
72+
fn explicit_box_dereference() {
73+
// Explicit dereference with type parameter
74+
let g1: Box<char> = Box::new('a');
75+
let _h1 = g1.deref(); // $ method=deref type=_h1:&T.char
76+
77+
// Explicit dereference with type parameter
78+
let g2: Box<char> = Box::new('a');
79+
let _h2 = *g2; // $ method=deref MISSING: type=_h2:char
80+
81+
// Call method on explicitly dereferenced value with type parameter
82+
let g3: Box<i64> = Box::new(34i64);
83+
let _h3 = (*g3).is_positive(); // $ method=deref MISSING: method=is_positive type=_h3:bool
84+
}
85+
86+
fn implicit_dereference() {
87+
// Call method on implicitly dereferenced value
88+
let x = MyIntPointer { value: 34i64 };
89+
let _y = x.is_positive(); // $ MISSING: method=is_positive type=_y:bool
90+
91+
// Call method on implicitly dereferenced value
92+
let x = MySmartPointer { value: 34i64 };
93+
let _y = x.is_positive(); // $ MISSING: method=is_positive type=_y:bool
94+
}
95+
96+
pub fn test() {
97+
explicit_monomorphic_dereference();
98+
explicit_polymorphic_dereference();
99+
explicit_ref_dereference();
100+
explicit_box_dereference();
101+
implicit_dereference();
102+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,6 +1881,8 @@ mod method_determined_by_argument_type {
18811881
}
18821882
}
18831883

1884+
mod dereference;
1885+
18841886
fn main() {
18851887
field_access::f();
18861888
method_impl::f();
@@ -1905,4 +1907,5 @@ fn main() {
19051907
indexers::f();
19061908
macros::f();
19071909
method_determined_by_argument_type::f();
1910+
dereference::test();
19081911
}

0 commit comments

Comments
 (0)