Skip to content

Commit 9c78e26

Browse files
committed
wf
1 parent 2c7993d commit 9c78e26

File tree

4 files changed

+132
-19
lines changed

4 files changed

+132
-19
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ private class VariantItemNode extends ItemNode instanceof Variant {
215215
override Visibility getVisibility() { result = Variant.super.getVisibility() }
216216
}
217217

218-
private class FunctionItemNode extends ItemNode instanceof Function {
218+
class FunctionItemNode extends ItemNode instanceof Function {
219219
override string getName() { result = Function.super.getName().getText() }
220220

221221
override Namespace getNamespace() { result.isValue() }

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

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,22 @@ class TraitType extends Type, TTrait {
202202

203203
TraitType() { this = TTrait(trait) }
204204

205-
override Function getMethod(string name) { result = trait.getASuccessor(name) }
205+
override Function getMethod(string name) {
206+
result = trait.getASuccessor(name)
207+
or
208+
result = this.getABound().resolveType().getMethod(name)
209+
}
206210

207211
override RecordField getRecordField(string name) { none() }
208212

209213
override TupleField getTupleField(int i) { none() }
210214

211-
override TypeRepr getABaseType() { none() }
215+
pragma[nomagic]
216+
private TypeRepr_ getABound() {
217+
result = trait.(Trait).getTypeBoundList().getABound().getTypeRepr()
218+
}
219+
220+
override TypeRepr getABaseType() { result = this.getABound() }
212221

213222
override string toString() { result = trait.toString() }
214223

@@ -267,7 +276,7 @@ class TypeParameter extends Type, TTypeParameter {
267276

268277
override TupleField getTupleField(int i) { none() }
269278

270-
override TypeRepr getABaseType() { result = this.getABound() }
279+
override TypeRepr getABaseType() { none() }
271280

272281
override string toString() { result = typeParam.toString() }
273282

@@ -755,15 +764,15 @@ private Type resolveTargetTyped(AstNode n, TypePath path) {
755764
result = resolveType(let.getInitializer(), path)
756765
)
757766
or
758-
exists(ItemNode i, Function f, SelfParam p, TypePath suffix, Type res |
767+
exists(ItemNode i, FunctionItemNode f, SelfParam p, TypePath suffix, Type res |
759768
n = p and
760769
(
761770
res = resolveImplSelfType(i, suffix)
762771
or
763772
res = resolveTraitSelfType(i, suffix)
764773
) and
765-
f = i.getASuccessor(_) and
766-
p = f.getParamList().getSelfParam()
774+
f.getImmediateParent() = i and
775+
p = f.(Function).getParamList().getSelfParam()
767776
|
768777
if p.isRef()
769778
then
@@ -917,13 +926,18 @@ private module FunctionMatchingInput implements MatchingInputSig {
917926
predicate target(Access a, Decl target) { target = a.getStaticTarget() }
918927

919928
private AstNode getExplicitArgument(Access a, int pos) {
920-
result = a.getArgList().getArg(pos)
929+
exists(int offset, Decl target |
930+
result = a.getArgList().getArg(pos + offset) and
931+
target(a, target)
932+
|
933+
if target.getParamList().hasSelfParam() and not a instanceof MethodCallExpr
934+
then offset = 1
935+
else offset = 0
936+
)
921937
or
922-
(
923-
result = a.(CallExpr).getFunction()
924-
or
925-
result = a.(MethodCallExpr).getReceiver()
926-
) and
938+
// result = a.(CallExpr).getFunction()
939+
// or
940+
result = a.(MethodCallExpr).getReceiver() and
927941
pos = -1
928942
}
929943

@@ -968,6 +982,12 @@ private module FunctionMatchingInput implements MatchingInputSig {
968982
|
969983
t = tp.resolveTypeAt(path)
970984
)
985+
or
986+
exists(SelfParam self |
987+
self = decl.getParamList().getSelfParam() and
988+
pos = -1 and
989+
t = resolveTargetTyped(self, path)
990+
)
971991
}
972992

973993
pragma[nomagic]

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,57 @@ mod m6 {
234234
}
235235
}
236236

237+
mod m7 {
238+
#[derive(Debug)]
239+
struct MyThing<A> {
240+
a: A,
241+
}
242+
243+
#[derive(Debug)]
244+
struct S1;
245+
#[derive(Debug)]
246+
struct S2;
247+
248+
trait MyTrait1<A> {
249+
fn m1(self) -> A;
250+
}
251+
252+
trait MyTrait2<A>: MyTrait1<A> {
253+
fn m2(self) -> A
254+
where
255+
Self: Sized,
256+
{
257+
if 1 + 1 > 2 {
258+
self.m1() // missing
259+
} else {
260+
Self::m1(self) // missing
261+
}
262+
}
263+
}
264+
265+
impl<T> MyTrait1<T> for MyThing<T> {
266+
fn m1(self) -> T {
267+
self.a
268+
}
269+
}
270+
271+
impl<T> MyTrait2<T> for MyThing<T> {}
272+
273+
pub fn f() {
274+
let x = MyThing { a: S1 };
275+
let y = MyThing { a: S2 };
276+
277+
println!("{:?}", x.m1()); // `x.m1` missing type
278+
println!("{:?}", y.m1()); // `y.m1` missing type
279+
280+
let x = MyThing { a: S1 };
281+
let y = MyThing { a: S2 };
282+
283+
println!("{:?}", x.m2()); // `x.m1` missing type
284+
println!("{:?}", y.m2()); // `y.m1` missing type
285+
}
286+
}
287+
237288
fn main() {
238289
m1::f();
239290
m1::g(m1::Foo {}, m1::Foo {});
@@ -242,4 +293,5 @@ fn main() {
242293
m4::f();
243294
m5::f();
244295
m6::f();
296+
m7::f();
245297
}

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

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,50 @@
171171
| main.rs:230:33:230:34 | S2 | | main.rs:216:5:217:14 | struct S2 |
172172
| main.rs:232:26:232:26 | x | | main.rs:208:5:212:5 | enum MyEnum |
173173
| main.rs:233:26:233:26 | y | 0 | main.rs:216:5:217:14 | struct S2 |
174-
| main.rs:238:5:238:9 | ...::f | | main.rs:2:5:2:21 | struct Foo |
175-
| main.rs:238:5:238:11 | ...::f(...) | | main.rs:2:5:2:21 | struct Foo |
176-
| main.rs:239:5:239:9 | ...::g | | main.rs:2:5:2:21 | struct Foo |
177-
| main.rs:239:5:239:33 | ...::g(...) | | main.rs:2:5:2:21 | struct Foo |
178-
| main.rs:239:11:239:20 | ...::Foo {...} | | main.rs:2:5:2:21 | struct Foo |
179-
| main.rs:239:23:239:32 | ...::Foo {...} | | main.rs:2:5:2:21 | struct Foo |
174+
| main.rs:249:15:249:18 | SelfParam | | main.rs:248:5:250:5 | trait MyTrait1 |
175+
| main.rs:249:15:249:18 | SelfParam | 0 | main.rs:248:20:248:20 | A |
176+
| main.rs:253:15:253:18 | SelfParam | | main.rs:252:5:263:5 | trait MyTrait2 |
177+
| main.rs:253:15:253:18 | SelfParam | 0 | main.rs:252:20:252:20 | A |
178+
| main.rs:258:17:258:20 | self | | main.rs:252:5:263:5 | trait MyTrait2 |
179+
| main.rs:258:17:258:20 | self | 0 | main.rs:252:20:252:20 | A |
180+
| main.rs:260:26:260:29 | self | | main.rs:252:5:263:5 | trait MyTrait2 |
181+
| main.rs:260:26:260:29 | self | 0 | main.rs:252:20:252:20 | A |
182+
| main.rs:266:15:266:18 | SelfParam | | main.rs:238:5:241:5 | struct MyThing |
183+
| main.rs:266:15:266:18 | SelfParam | 0 | main.rs:265:10:265:10 | T |
184+
| main.rs:267:13:267:16 | self | | main.rs:238:5:241:5 | struct MyThing |
185+
| main.rs:267:13:267:16 | self | 0 | main.rs:265:10:265:10 | T |
186+
| main.rs:267:13:267:18 | self.a | | main.rs:265:10:265:10 | T |
187+
| main.rs:274:13:274:13 | x | | main.rs:238:5:241:5 | struct MyThing |
188+
| main.rs:274:13:274:13 | x | 0 | main.rs:243:5:244:14 | struct S1 |
189+
| main.rs:274:17:274:33 | MyThing {...} | | main.rs:238:5:241:5 | struct MyThing |
190+
| main.rs:274:17:274:33 | MyThing {...} | 0 | main.rs:243:5:244:14 | struct S1 |
191+
| main.rs:274:30:274:31 | S1 | | main.rs:243:5:244:14 | struct S1 |
192+
| main.rs:275:13:275:13 | y | | main.rs:238:5:241:5 | struct MyThing |
193+
| main.rs:275:13:275:13 | y | 0 | main.rs:245:5:246:14 | struct S2 |
194+
| main.rs:275:17:275:33 | MyThing {...} | | main.rs:238:5:241:5 | struct MyThing |
195+
| main.rs:275:17:275:33 | MyThing {...} | 0 | main.rs:245:5:246:14 | struct S2 |
196+
| main.rs:275:30:275:31 | S2 | | main.rs:245:5:246:14 | struct S2 |
197+
| main.rs:277:26:277:26 | x | | main.rs:238:5:241:5 | struct MyThing |
198+
| main.rs:277:26:277:26 | x | 0 | main.rs:243:5:244:14 | struct S1 |
199+
| main.rs:278:26:278:26 | y | | main.rs:238:5:241:5 | struct MyThing |
200+
| main.rs:278:26:278:26 | y | 0 | main.rs:245:5:246:14 | struct S2 |
201+
| main.rs:280:13:280:13 | x | | main.rs:238:5:241:5 | struct MyThing |
202+
| main.rs:280:13:280:13 | x | 0 | main.rs:243:5:244:14 | struct S1 |
203+
| main.rs:280:17:280:33 | MyThing {...} | | main.rs:238:5:241:5 | struct MyThing |
204+
| main.rs:280:17:280:33 | MyThing {...} | 0 | main.rs:243:5:244:14 | struct S1 |
205+
| main.rs:280:30:280:31 | S1 | | main.rs:243:5:244:14 | struct S1 |
206+
| main.rs:281:13:281:13 | y | | main.rs:238:5:241:5 | struct MyThing |
207+
| main.rs:281:13:281:13 | y | 0 | main.rs:245:5:246:14 | struct S2 |
208+
| main.rs:281:17:281:33 | MyThing {...} | | main.rs:238:5:241:5 | struct MyThing |
209+
| main.rs:281:17:281:33 | MyThing {...} | 0 | main.rs:245:5:246:14 | struct S2 |
210+
| main.rs:281:30:281:31 | S2 | | main.rs:245:5:246:14 | struct S2 |
211+
| main.rs:283:26:283:26 | x | | main.rs:238:5:241:5 | struct MyThing |
212+
| main.rs:283:26:283:26 | x | 0 | main.rs:243:5:244:14 | struct S1 |
213+
| main.rs:284:26:284:26 | y | | main.rs:238:5:241:5 | struct MyThing |
214+
| 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 |

0 commit comments

Comments
 (0)