Skip to content

Commit 7c9d15b

Browse files
committed
Rust: Add test with method on borrow
1 parent 9bdb541 commit 7c9d15b

File tree

2 files changed

+1473
-1374
lines changed

2 files changed

+1473
-1374
lines changed

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,11 @@ mod method_call_type_conversion {
10791079
#[derive(Debug, Copy, Clone)]
10801080
struct S2;
10811081

1082+
#[derive(Debug, Copy, Clone, Default)]
1083+
struct MyInt {
1084+
a: i64,
1085+
}
1086+
10821087
impl<T> S<T> {
10831088
fn m1(self) -> T {
10841089
self.0 // $ fieldof=S
@@ -1093,6 +1098,24 @@ mod method_call_type_conversion {
10931098
}
10941099
}
10951100

1101+
trait ATrait {
1102+
fn method_on_borrow(&self) -> i64;
1103+
fn method_not_on_borrow(self) -> i64;
1104+
}
1105+
1106+
// Trait implementation on a borrow.
1107+
impl ATrait for &MyInt {
1108+
// MyInt::method_on_borrow
1109+
fn method_on_borrow(&self) -> i64 {
1110+
(*(*self)).a // $ method=deref fieldof=MyInt
1111+
}
1112+
1113+
// MyInt::method_not_on_borrow
1114+
fn method_not_on_borrow(self) -> i64 {
1115+
(*self).a // $ method=deref fieldof=MyInt
1116+
}
1117+
}
1118+
10961119
pub fn f() {
10971120
let x1 = S(S2);
10981121
println!("{:?}", x1.m1()); // $ method=m1
@@ -1128,10 +1151,21 @@ mod method_call_type_conversion {
11281151
let t = x7.m1(); // $ method=m1 type=t:& type=t:&T.S2
11291152
println!("{:?}", x7);
11301153

1131-
let x9 : String = "Hello".to_string(); // $ type=x9:String
1154+
let x9: String = "Hello".to_string(); // $ type=x9:String
1155+
11321156
// Implicit `String` -> `str` conversion happens via the `Deref` trait:
11331157
// https://doc.rust-lang.org/std/string/struct.String.html#deref.
11341158
let u = x9.parse::<u32>(); // $ method=parse type=u:T.u32
1159+
1160+
let my_thing = &MyInt { a: 37 };
1161+
// implicit borrow of a `&`
1162+
let a = my_thing.method_on_borrow(); // $ MISSING: method=MyInt::method_on_borrow
1163+
println!("{:?}", a);
1164+
1165+
// no implicit borrow
1166+
let my_thing = &MyInt { a: 38 };
1167+
let a = my_thing.method_not_on_borrow(); // $ MISSING: method=MyInt::method_not_on_borrow
1168+
println!("{:?}", a);
11351169
}
11361170
}
11371171

0 commit comments

Comments
 (0)