Skip to content

Commit 67deeb5

Browse files
committed
wip
1 parent 9c78e26 commit 67deeb5

File tree

3 files changed

+116
-26
lines changed

3 files changed

+116
-26
lines changed

rust/ql/lib/codeql/rust/elements/internal/TypeInference.qll

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -998,12 +998,6 @@ private module FunctionMatchingInput implements MatchingInputSig {
998998

999999
private module FunctionMatching = Matching<FunctionMatchingInput>;
10001000

1001-
private Type resolveFunctionCall(CallExprBase call, TypePath path) {
1002-
result = resolveFunctionReturnType(call.getStaticTarget(), path)
1003-
or
1004-
result = FunctionMatching::resolveAccess(call, -2, path)
1005-
}
1006-
10071001
pragma[nomagic]
10081002
private Type resolveFunctionReturnType(Function f, TypePath path) {
10091003
result = f.getRetType().getTypeRepr().(TypeRepr_).resolveTypeAt(path) and
@@ -1022,12 +1016,15 @@ private Type resolvePathExprType(PathExpr pe, TypePath path) {
10221016
)
10231017
)
10241018
or
1025-
result = pe.getPath().(Path_).resolveTypeAt(path)
1019+
result = pe.getPath().(Path_).resolveTypeAt(path) and
1020+
path.isEmpty()
10261021
}
10271022

10281023
pragma[nomagic]
10291024
private Type resolveCallExprType(CallExpr ce, TypePath path) {
10301025
result = resolvePathExprType(ce.getFunction(), path)
1026+
or
1027+
result = FunctionMatching::resolveAccess(ce, -2, path)
10311028
}
10321029

10331030
pragma[nomagic]
@@ -1049,6 +1046,8 @@ private Type resolveMethodCallExprType(MethodCallExpr mce, TypePath path) {
10491046
f = resolveMethodCallExpr(mce) and
10501047
result = resolveFunctionReturnType(f, path)
10511048
)
1049+
or
1050+
result = FunctionMatching::resolveAccess(mce, -2, path)
10521051
}
10531052

10541053
private module FieldExprMatchingInput implements MatchingInputSig {

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

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ mod m3 {
9292
}
9393

9494
fn call_trait_m1<T1, T2: MyTrait<T1>>(x: T2) -> T1 {
95-
x.m1() // `x.m1` missing type
95+
x.m1() // missing
9696
}
9797

9898
impl MyTrait<S1> for MyThing<S1> {
@@ -117,8 +117,8 @@ mod m3 {
117117
let x = MyThing { a: S1 };
118118
let y = MyThing { a: S2 };
119119

120-
println!("{:?}", call_trait_m1(x)); // `call_trait_m1(x)` missing type
121-
println!("{:?}", call_trait_m1(y).a); // `call_trait_m1(y)` missing type
120+
println!("{:?}", call_trait_m1(x)); // missing
121+
println!("{:?}", call_trait_m1(y).a); // missing
122122
}
123123
}
124124

@@ -145,7 +145,7 @@ mod m4 {
145145
}
146146

147147
fn call_trait_m1<T1, T2: MyTrait<T1>>(x: T2) -> T1 {
148-
x.m1() // `x.m1` missing type
148+
x.m1() // missing
149149
}
150150

151151
impl<T> MyTrait<T> for MyThing<T> {
@@ -158,14 +158,14 @@ mod m4 {
158158
let x = MyThing { a: S1 };
159159
let y = MyThing { a: S2 };
160160

161-
println!("{:?}", x.m1()); // `x.m1` missing type
162-
println!("{:?}", y.m1()); // `y.m1` missing type
161+
println!("{:?}", x.m1()); // missing
162+
println!("{:?}", y.m1()); // missing
163163

164164
let x = MyThing { a: S1 };
165165
let y = MyThing { a: S2 };
166166

167-
println!("{:?}", call_trait_m1(x)); // `call_trait_m1(x)` missing type
168-
println!("{:?}", call_trait_m1(y)); // `call_trait_m1(y)` missing type
167+
println!("{:?}", call_trait_m1(x)); // missing
168+
println!("{:?}", call_trait_m1(y)); // missing
169169
}
170170
}
171171

@@ -200,7 +200,7 @@ mod m5 {
200200
println!("{:?}", x.m1());
201201

202202
let x = S;
203-
println!("{:?}", x.m2()); // `x.m2` missing type
203+
println!("{:?}", x.m2()); // missing
204204
}
205205
}
206206

@@ -219,8 +219,8 @@ mod m6 {
219219
impl<T> MyEnum<T> {
220220
fn m1(self) -> T {
221221
match self {
222-
MyEnum::C1(a) => a, // `a` missing type
223-
MyEnum::C2 { a } => a, // `a` missing type
222+
MyEnum::C1(a) => a, // missing
223+
MyEnum::C2 { a } => a, // missing
224224
}
225225
}
226226
}
@@ -255,9 +255,9 @@ mod m7 {
255255
Self: Sized,
256256
{
257257
if 1 + 1 > 2 {
258-
self.m1() // missing
258+
self.m1()
259259
} else {
260-
Self::m1(self) // missing
260+
Self::m1(self)
261261
}
262262
}
263263
}
@@ -285,6 +285,55 @@ mod m7 {
285285
}
286286
}
287287

288+
mod m8 {
289+
use std::convert::From;
290+
use std::fmt::Debug;
291+
292+
#[derive(Debug)]
293+
struct S1;
294+
295+
#[derive(Debug)]
296+
struct S2;
297+
298+
trait Trait: Debug {}
299+
300+
impl Trait for S1 {}
301+
302+
fn id<T: ?Sized>(x: &T) -> &T {
303+
x
304+
}
305+
306+
impl Into<S2> for S1 {
307+
fn into(self) -> S2 {
308+
S2
309+
}
310+
}
311+
312+
fn into<T1, T2>(x: T1) -> T2
313+
where
314+
T1: Into<T2>,
315+
{
316+
x.into() // missing
317+
}
318+
319+
pub fn f() {
320+
let x = S1;
321+
println!("{:?}", id(&x));
322+
323+
let x = S1;
324+
println!("{:?}", id::<S1>(&x));
325+
326+
let x = S1;
327+
println!("{:?}", id::<dyn Trait>(&x)); // missing
328+
329+
let x = S1;
330+
into::<S1, S2>(x);
331+
332+
let x = S1;
333+
let y: S2 = into(x); // missing
334+
}
335+
}
336+
288337
fn main() {
289338
m1::f();
290339
m1::g(m1::Foo {}, m1::Foo {});
@@ -294,4 +343,5 @@ fn main() {
294343
m5::f();
295344
m6::f();
296345
m7::f();
346+
m8::f();
297347
}

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

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@
177177
| main.rs:253:15:253:18 | SelfParam | 0 | main.rs:252:20:252:20 | A |
178178
| main.rs:258:17:258:20 | self | | main.rs:252:5:263:5 | trait MyTrait2 |
179179
| main.rs:258:17:258:20 | self | 0 | main.rs:252:20:252:20 | A |
180+
| main.rs:258:17:258:25 | self.m1(...) | | main.rs:252:20:252:20 | A |
181+
| main.rs:260:17:260:30 | ...::m1(...) | | main.rs:252:20:252:20 | A |
180182
| main.rs:260:26:260:29 | self | | main.rs:252:5:263:5 | trait MyTrait2 |
181183
| main.rs:260:26:260:29 | self | 0 | main.rs:252:20:252:20 | A |
182184
| main.rs:266:15:266:18 | SelfParam | | main.rs:238:5:241:5 | struct MyThing |
@@ -212,9 +214,48 @@
212214
| main.rs:283:26:283:26 | x | 0 | main.rs:243:5:244:14 | struct S1 |
213215
| main.rs:284:26:284:26 | y | | main.rs:238:5:241:5 | struct MyThing |
214216
| main.rs:284:26:284:26 | y | 0 | main.rs:245:5:246:14 | struct S2 |
215-
| main.rs:289:5:289:9 | ...::f | | main.rs:2:5:2:21 | struct Foo |
216-
| main.rs:289:5:289:11 | ...::f(...) | | main.rs:2:5:2:21 | struct Foo |
217-
| main.rs:290:5:290:9 | ...::g | | main.rs:2:5:2:21 | struct Foo |
218-
| main.rs:290:5:290:33 | ...::g(...) | | main.rs:2:5:2:21 | struct Foo |
219-
| main.rs:290:11:290:20 | ...::Foo {...} | | main.rs:2:5:2:21 | struct Foo |
220-
| main.rs:290:23:290:32 | ...::Foo {...} | | main.rs:2:5:2:21 | struct Foo |
217+
| main.rs:302:22:302:22 | x | | file://:0:0:0:0 | & |
218+
| main.rs:302:22:302:22 | x | 0 | main.rs:302:11:302:19 | T |
219+
| main.rs:303:9:303:9 | x | | file://:0:0:0:0 | & |
220+
| main.rs:303:9:303:9 | x | 0 | main.rs:302:11:302:19 | T |
221+
| main.rs:307:17:307:20 | SelfParam | | main.rs:292:5:293:14 | struct S1 |
222+
| main.rs:308:13:308:14 | S2 | | main.rs:295:5:296:14 | struct S2 |
223+
| main.rs:312:21:312:21 | x | | main.rs:312:13:312:14 | T1 |
224+
| main.rs:316:9:316:9 | x | | main.rs:312:13:312:14 | T1 |
225+
| main.rs:320:13:320:13 | x | | main.rs:292:5:293:14 | struct S1 |
226+
| main.rs:320:17:320:18 | S1 | | main.rs:292:5:293:14 | struct S1 |
227+
| main.rs:321:26:321:27 | id | | file://:0:0:0:0 | & |
228+
| main.rs:321:26:321:31 | id(...) | | file://:0:0:0:0 | & |
229+
| main.rs:321:26:321:31 | id(...) | 0 | main.rs:292:5:293:14 | struct S1 |
230+
| main.rs:321:29:321:30 | &x | | file://:0:0:0:0 | & |
231+
| main.rs:321:29:321:30 | &x | 0 | main.rs:292:5:293:14 | struct S1 |
232+
| main.rs:321:30:321:30 | x | | main.rs:292:5:293:14 | struct S1 |
233+
| main.rs:323:13:323:13 | x | | main.rs:292:5:293:14 | struct S1 |
234+
| main.rs:323:17:323:18 | S1 | | main.rs:292:5:293:14 | struct S1 |
235+
| main.rs:324:26:324:33 | id::<...> | | file://:0:0:0:0 | & |
236+
| main.rs:324:26:324:37 | id::<...>(...) | | file://:0:0:0:0 | & |
237+
| main.rs:324:26:324:37 | id::<...>(...) | 0 | main.rs:292:5:293:14 | struct S1 |
238+
| main.rs:324:35:324:36 | &x | | file://:0:0:0:0 | & |
239+
| main.rs:324:35:324:36 | &x | 0 | main.rs:292:5:293:14 | struct S1 |
240+
| main.rs:324:36:324:36 | x | | main.rs:292:5:293:14 | struct S1 |
241+
| main.rs:326:13:326:13 | x | | main.rs:292:5:293:14 | struct S1 |
242+
| main.rs:326:17:326:18 | S1 | | main.rs:292:5:293:14 | struct S1 |
243+
| main.rs:327:26:327:40 | id::<...> | | file://:0:0:0:0 | & |
244+
| main.rs:327:26:327:44 | id::<...>(...) | | file://:0:0:0:0 | & |
245+
| main.rs:327:42:327:43 | &x | | file://:0:0:0:0 | & |
246+
| main.rs:327:42:327:43 | &x | 0 | main.rs:292:5:293:14 | struct S1 |
247+
| main.rs:327:43:327:43 | x | | main.rs:292:5:293:14 | struct S1 |
248+
| main.rs:329:13:329:13 | x | | main.rs:292:5:293:14 | struct S1 |
249+
| main.rs:329:17:329:18 | S1 | | main.rs:292:5:293:14 | struct S1 |
250+
| main.rs:330:9:330:25 | into::<...>(...) | | main.rs:295:5:296:14 | struct S2 |
251+
| main.rs:330:24:330:24 | x | | main.rs:292:5:293:14 | struct S1 |
252+
| main.rs:332:13:332:13 | x | | main.rs:292:5:293:14 | struct S1 |
253+
| main.rs:332:17:332:18 | S1 | | main.rs:292:5:293:14 | struct S1 |
254+
| main.rs:333:13:333:13 | y | | main.rs:295:5:296:14 | struct S2 |
255+
| main.rs:333:26:333:26 | x | | main.rs:292:5:293:14 | struct S1 |
256+
| main.rs:338:5:338:9 | ...::f | | main.rs:2:5:2:21 | struct Foo |
257+
| main.rs:338:5:338:11 | ...::f(...) | | main.rs:2:5:2:21 | struct Foo |
258+
| main.rs:339:5:339:9 | ...::g | | main.rs:2:5:2:21 | struct Foo |
259+
| main.rs:339:5:339:33 | ...::g(...) | | main.rs:2:5:2:21 | struct Foo |
260+
| main.rs:339:11:339:20 | ...::Foo {...} | | main.rs:2:5:2:21 | struct Foo |
261+
| main.rs:339:23:339:32 | ...::Foo {...} | | main.rs:2:5:2:21 | struct Foo |

0 commit comments

Comments
 (0)