Skip to content

Commit 7e98400

Browse files
committed
wip2
1 parent f7891f9 commit 7e98400

File tree

5 files changed

+53
-25
lines changed

5 files changed

+53
-25
lines changed

rust/ql/lib/codeql/rust/internal/typeinference/FunctionOverloading.qll

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -126,37 +126,50 @@ private predicate functionResolutionDependsOnArgument0(
126126

127127
/**
128128
* Holds if resolving the function `f` in `impl` requires inspecting the type
129-
* of applied _arguments_ at position `pos` (including the return type) in
130-
* order to determine whether it is the correct resolution.
129+
* of applied _arguments_ at position `pos` (possibly including the return type)
130+
* in order to determine whether it is the correct resolution.
131131
*/
132132
pragma[nomagic]
133133
predicate functionResolutionDependsOnArgument(
134134
ImplItemNode impl, Function f, TypeParameter traitTp, FunctionPosition pos, TypePath path,
135135
Type type
136136
) {
137137
exists(string functionName |
138-
functionResolutionDependsOnArgument0(impl, f, functionName, traitTp, pos, path, type)
138+
functionResolutionDependsOnArgument0(impl, f, functionName, traitTp, pos, path, type) and
139+
not pos.isSelf()
139140
|
140-
pos.isPosition()
141-
or
142-
// Only disambiguate based on return type when all other positions are trivially
143-
// satisfied for all arguments.
144-
pos.isReturn() and
145-
forall(FunctionPosition pos0, TypePath path0, Type type0 |
146-
pos0.isPosition() and
147-
functionResolutionDependsOnArgument0(impl, f, _, _, pos0, path0, type0)
141+
exists(FunctionPosition pos0 |
142+
functionResolutionDependsOnArgument0(impl, f, functionName, traitTp, pos0, _, _)
148143
|
149-
type0.(TypeParamTypeParameter).getTypeParam() = any(TypeParam tp | not tp.hasTypeBound())
144+
pos0.isPosition()
150145
or
151-
forall(ImplItemNode impl1, Function f1, Type type1 |
152-
implSiblings(_, impl, impl1) and
153-
f1 = impl1.getASuccessor(functionName) and
154-
type1 = getAssocFunctionTypeAt(f1, impl1, pos0, path0)
155-
|
156-
type1.(TypeParamTypeParameter).getTypeParam() = any(TypeParam tp | not tp.hasTypeBound())
157-
or
158-
type0 = type1
146+
pos0.isReturn() and
147+
not exists(FunctionPosition pos1 |
148+
functionResolutionDependsOnArgument0(impl, f, functionName, _, pos1, _, _) and
149+
pos1.isPosition()
159150
)
160151
)
152+
// |
153+
// pos.isPosition()
154+
// or
155+
// // Only disambiguate based on return type when all other positions are trivially
156+
// // satisfied for all arguments.
157+
// pos.isReturn() and
158+
// forall(FunctionPosition pos0, TypePath path0, Type type0 |
159+
// pos0.isPosition() and
160+
// functionResolutionDependsOnArgument0(impl, f, _, _, pos0, path0, type0)
161+
// |
162+
// type0.(TypeParamTypeParameter).getTypeParam() = any(TypeParam tp | not tp.hasTypeBound())
163+
// or
164+
// forall(ImplItemNode impl1, Function f1, Type type1 |
165+
// implSiblings(_, impl, impl1) and
166+
// f1 = impl1.getASuccessor(functionName) and
167+
// type1 = getAssocFunctionTypeAt(f1, impl1, pos0, path0)
168+
// |
169+
// type1.(TypeParamTypeParameter).getTypeParam() = any(TypeParam tp | not tp.hasTypeBound())
170+
// or
171+
// type0 = type1
172+
// )
173+
// )
161174
)
162175
}

rust/ql/lib/codeql/rust/internal/typeinference/FunctionType.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ signature module ArgsAreInstantiationsOfInputSig {
298298
* we need to check that the type of `f` for `traitTp` is satisfied.
299299
*
300300
* `pos` is one of the positions in `f` in which the relevant type occours.
301+
*
302+
* For example, i
301303
*/
302304
predicate toCheck(ImplOrTraitItemNode i, Function f, TypeParameter traitTp, FunctionPosition pos);
303305

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,8 +1283,9 @@ private class BorrowKind extends TBorrowKind {
12831283
// for now, we do not handle ambiguous targets when one of the types is itself
12841284
// a constrained type parameter; we should be checking the constraints in this case
12851285
private predicate typeCanBeUsedForDisambiguation(Type t) {
1286-
not t instanceof TypeParameter or
1287-
t.(TypeParamTypeParameter).getTypeParam() = any(TypeParam tp | not tp.hasTypeBound())
1286+
any()
1287+
// not t instanceof TypeParameter or
1288+
// t.(TypeParamTypeParameter).getTypeParam() = any(TypeParam tp | not tp.hasTypeBound())
12881289
}
12891290

12901291
/**
@@ -2952,8 +2953,6 @@ private module NonMethodResolution {
29522953
{
29532954
predicate toCheck(ImplOrTraitItemNode i, Function f, TypeParameter traitTp, FunctionPosition pos) {
29542955
exists(Type t0 | typeCanBeUsedForDisambiguation(t0) |
2955-
// FunctionOverloading::functionResolutionDependsOnArgument(i, f, pos, _, t0)
2956-
// or
29572956
traitFunctionDependsOnPos2(_, _, pos, t0, i, f, traitTp)
29582957
)
29592958
}
@@ -3009,6 +3008,7 @@ private module NonMethodResolution {
30093008
or
30103009
// match against the trait function itself
30113010
exists(Trait trait |
3011+
i = trait and
30123012
FunctionOverloading::traitTypeParameterOccurrence(trait, f, _, pos, _, traitTp) and
30133013
traitTp = TSelfTypeParameter(trait)
30143014
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
multipleResolvedTargets
2+
| main.rs:2220:9:2220:31 | ... .my_add(...) |
3+
| main.rs:2222:9:2222:29 | ... .my_add(...) |
24
| main.rs:2720:13:2720:17 | x.f() |
5+
| overloading.rs:197:22:197:30 | x.g(...) |

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10415,6 +10415,7 @@ inferType
1041510415
| main.rs:2220:26:2220:29 | 2i64 | | {EXTERNAL LOCATION} | i64 |
1041610416
| main.rs:2221:9:2221:15 | S(...) | | main.rs:2104:5:2104:19 | S |
1041710417
| main.rs:2221:9:2221:15 | S(...) | T | {EXTERNAL LOCATION} | i64 |
10418+
| main.rs:2221:9:2221:28 | ... .my_add(...) | | main.rs:2104:5:2104:19 | S |
1041810419
| main.rs:2221:11:2221:14 | 1i64 | | {EXTERNAL LOCATION} | i64 |
1041910420
| main.rs:2221:24:2221:27 | 3i64 | | {EXTERNAL LOCATION} | i64 |
1042010421
| main.rs:2222:9:2222:15 | S(...) | | main.rs:2104:5:2104:19 | S |
@@ -11998,15 +11999,18 @@ inferType
1199811999
| overloading.rs:194:13:194:13 | z | | {EXTERNAL LOCATION} | i32 |
1199912000
| overloading.rs:194:22:194:22 | x | | {EXTERNAL LOCATION} | i32 |
1200012001
| overloading.rs:194:22:194:44 | x.f(...) | | {EXTERNAL LOCATION} | i32 |
12002+
| overloading.rs:194:26:194:43 | ...::default(...) | | {EXTERNAL LOCATION} | i32 |
1200112003
| overloading.rs:195:13:195:13 | z | | {EXTERNAL LOCATION} | i64 |
1200212004
| overloading.rs:195:17:195:17 | x | | {EXTERNAL LOCATION} | i32 |
1200312005
| overloading.rs:195:17:195:25 | x.f(...) | | {EXTERNAL LOCATION} | i64 |
1200412006
| overloading.rs:195:21:195:24 | 0i64 | | {EXTERNAL LOCATION} | i64 |
1200512007
| overloading.rs:196:13:196:13 | z | | {EXTERNAL LOCATION} | i64 |
1200612008
| overloading.rs:196:22:196:22 | x | | {EXTERNAL LOCATION} | i32 |
1200712009
| overloading.rs:196:22:196:44 | x.f(...) | | {EXTERNAL LOCATION} | i64 |
12010+
| overloading.rs:196:26:196:43 | ...::default(...) | | {EXTERNAL LOCATION} | i64 |
1200812011
| overloading.rs:197:13:197:13 | z | | {EXTERNAL LOCATION} | i64 |
1200912012
| overloading.rs:197:22:197:22 | x | | {EXTERNAL LOCATION} | i32 |
12013+
| overloading.rs:197:22:197:30 | x.g(...) | | {EXTERNAL LOCATION} | i32 |
1201012014
| overloading.rs:197:22:197:30 | x.g(...) | | {EXTERNAL LOCATION} | i64 |
1201112015
| overloading.rs:197:26:197:29 | 0i32 | | {EXTERNAL LOCATION} | i32 |
1201212016
| overloading.rs:205:17:208:5 | { ... } | | overloading.rs:202:5:203:13 | S |
@@ -14086,4 +14090,10 @@ inferType
1408614090
| raw_pointer.rs:59:5:59:30 | raw_type_from_deref(...) | | {EXTERNAL LOCATION} | () |
1408714091
| raw_pointer.rs:59:25:59:29 | false | | {EXTERNAL LOCATION} | bool |
1408814092
testFailures
14089-
| overloading.rs:197:33:197:68 | //... | Fixed spurious result: target=g3 |
14093+
| main.rs:2220:9:2220:31 | ... .my_add(...) | Unexpected result: target=S::my_add2 |
14094+
| main.rs:2221:9:2221:28 | ... .my_add(...) | Fixed missing result: target=S::my_add2 |
14095+
| main.rs:2222:9:2222:29 | ... .my_add(...) | Unexpected result: target=S::my_add2 |
14096+
| overloading.rs:194:22:194:44 | x.f(...) | Fixed missing result: target=f1 |
14097+
| overloading.rs:194:26:194:43 | ...::default(...) | Unexpected result: target=default |
14098+
| overloading.rs:196:22:196:44 | x.f(...) | Fixed missing result: target=f2 |
14099+
| overloading.rs:196:26:196:43 | ...::default(...) | Unexpected result: target=default |

0 commit comments

Comments
 (0)