Skip to content

Commit 75f3c88

Browse files
committed
wip3
1 parent 14721b9 commit 75f3c88

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,7 +1605,7 @@ private module MethodResolution {
16051605
pos.isPositional() and
16061606
FunctionOverloading::functionResolutionDependsOnArgument(i, f, pos, path, t0) and
16071607
t.appliesTo(f, pos, i) and
1608-
not t0 instanceof TypeParameter
1608+
not t0 instanceof TypeParameter // skip parametric types, as we ought to check type bounds
16091609
)
16101610
}
16111611

@@ -1817,12 +1817,9 @@ private module NonMethodResolution {
18171817
TraitItemNode trait, NonMethodFunction traitFunction, FunctionTypePosition pos,
18181818
NonMethodFunction implFunction
18191819
) {
1820-
exists(TypePath path, Type type |
1821-
functionTypeAtPath(implFunction, pos, path, type) and
1822-
implFunction.implements(traitFunction) and
1823-
traitTypeParameterOccurrence(trait, traitFunction, _, pos, _, _) and
1824-
not type instanceof TypeParameter
1825-
|
1820+
implFunction.implements(traitFunction) and
1821+
traitTypeParameterOccurrence(trait, traitFunction, _, pos, _, _) and
1822+
(
18261823
not pos.isReturn()
18271824
or
18281825
// We only check that the context of the call provides relevant type information
@@ -1975,7 +1972,7 @@ private module NonMethodResolution {
19751972
}
19761973

19771974
pragma[nomagic]
1978-
NonMethodFunction resolveAssocCallTargetCand(ImplOrTraitItemNode i) {
1975+
NonMethodFunction resolveAssocCallTargetCand(ImplItemNode i) {
19791976
not this.(Call).hasTrait() and
19801977
result = this.getPathResolutionResolved() and
19811978
result = i.getASuccessor(_)
@@ -2033,10 +2030,17 @@ private module NonMethodResolution {
20332030
exists(Type t0 |
20342031
FunctionOverloading::functionResolutionDependsOnArgument(i, f, pos, _, t0) and
20352032
t.appliesTo(f, pos, i) and
2036-
not t0 instanceof TypeParameter
2033+
not t0 instanceof TypeParameter // skip parametric types, as we ought to check type bounds
20372034
)
20382035
or
2039-
traitFunctionDependsOnArgument(_, _, pos, f) and
2036+
(
2037+
traitFunctionDependsOnArgument(_, _, pos, f)
2038+
or
2039+
exists(Trait trait |
2040+
traitFunctionDependsOnArgument(trait, f, pos, _) and
2041+
t.getTypeAt(_) = TTrait(trait)
2042+
)
2043+
) and
20402044
t.appliesTo(f, pos, i)
20412045
}
20422046

@@ -2048,10 +2052,14 @@ private module NonMethodResolution {
20482052
predicate hasTargetCand(ImplOrTraitItemNode i, Function f) {
20492053
f = this.resolveAssocCallTargetCand(i)
20502054
or
2051-
exists(TraitItemNode trait, NonMethodFunction resolved |
2055+
exists(TraitItemNode trait, NonMethodFunction resolved, Function f1 |
20522056
this.hasTraitResolved(trait, resolved) and
2053-
traitFunctionDependsOnArgument(trait, resolved, _, f) and
2057+
traitFunctionDependsOnArgument(trait, resolved, _, f1) and
20542058
f = i.getASuccessor(_)
2059+
|
2060+
f = f1
2061+
or
2062+
f = resolved
20552063
)
20562064
}
20572065
}
@@ -3055,8 +3063,8 @@ private module Debug {
30553063
Locatable getRelevantLocatable() {
30563064
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
30573065
result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
3058-
filepath.matches("%/sqlx.rs") and
3059-
startline = [56 .. 60]
3066+
filepath.matches("%/main.rs") and
3067+
startline = 701
30603068
)
30613069
}
30623070

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -698,9 +698,9 @@ mod function_trait_bounds {
698698
T2::assoc(x) // $ target=assoc
699699
}
700700
fn call_trait_assoc_2<T1, T2: MyTrait<T1> + Copy>(x: T2) -> T1 {
701-
let y = MyTrait::assoc(x); // $ MISSING: target=assoc
702-
y; // $ MISSING: type=y:T1
703-
MyTrait::assoc(x) // $ MISSING: target=assoc
701+
let y = MyTrait::assoc(x); // $ target=assoc
702+
y; // $ type=y:T1
703+
MyTrait::assoc(x) // $ target=assoc
704704
}
705705

706706
// Type parameter with bound occurs nested within another type.
@@ -2446,9 +2446,9 @@ mod loops {
24462446
String::from("bar"), // $ target=from
24472447
String::from("baz"), // $ target=from
24482448
];
2449-
for s in strings3 {} // $ MISSING: type=s:String
2449+
for s in strings3 {} // $ type=s:&T.String
24502450

2451-
let callables = [MyCallable::new(), MyCallable::new(), MyCallable::new()]; // $ target=new $ MISSING: type=callables:[T;...].MyCallable; 3
2451+
let callables = [MyCallable::new(), MyCallable::new(), MyCallable::new()]; // $ target=new $ type=callables:[T;...].MyCallable
24522452
for c // $ type=c:MyCallable
24532453
in callables
24542454
{
@@ -2502,10 +2502,10 @@ mod loops {
25022502
let mut map1 = std::collections::HashMap::new(); // $ target=new type=map1:K.i32 type=map1:V.Box $ MISSING: type=map1:Hashmap type1=map1:V.T.&T.str
25032503
map1.insert(1, Box::new("one")); // $ target=insert target=new
25042504
map1.insert(2, Box::new("two")); // $ target=insert target=new
2505-
for key in map1.keys() {} // $ target=keys MISSING: type=key:i32
2506-
for value in map1.values() {} // $ target=values MISSING: type=value:Box type=value:T.&T.str
2507-
for (key, value) in map1.iter() {} // $ target=iter MISSING: type=key:i32 type=value:Box type=value:T.&T.str
2508-
for (key, value) in &map1 {} // $ MISSING: type=key:i32 type=value:Box type=value:T.&T.str
2505+
for key in map1.keys() {} // $ target=keys type=key:&T.i32
2506+
for value in map1.values() {} // $ target=values type=value:&T.Box type=value:&T.T.&T.str
2507+
for (key, value) in map1.iter() {} // $ target=iter type=key:&T.i32 type=value:&T.Box type=value:&T.T.&T.str
2508+
for (key, value) in &map1 {} // $ type=key:&T.i32 type=value:&T.Box type=value:&T.T.&T.str
25092509

25102510
// while loops
25112511

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,7 +1992,10 @@ inferType
19921992
| main.rs:698:19:698:19 | x | | main.rs:695:31:695:52 | T2 |
19931993
| main.rs:700:55:700:55 | x | | main.rs:700:31:700:52 | T2 |
19941994
| main.rs:700:68:704:5 | { ... } | | main.rs:700:27:700:28 | T1 |
1995+
| main.rs:701:13:701:13 | y | | main.rs:700:27:700:28 | T1 |
1996+
| main.rs:701:17:701:33 | ...::assoc(...) | | main.rs:700:27:700:28 | T1 |
19951997
| main.rs:701:32:701:32 | x | | main.rs:700:31:700:52 | T2 |
1998+
| main.rs:702:9:702:9 | y | | main.rs:700:27:700:28 | T1 |
19961999
| main.rs:703:9:703:25 | ...::assoc(...) | | main.rs:700:27:700:28 | T1 |
19972000
| main.rs:703:24:703:24 | x | | main.rs:700:31:700:52 | T2 |
19982001
| main.rs:708:49:708:49 | x | | main.rs:656:5:659:5 | MyThing |

0 commit comments

Comments
 (0)