Skip to content

Commit ce5232b

Browse files
committed
wip2
1 parent 9079fde commit ce5232b

File tree

4 files changed

+43
-24
lines changed

4 files changed

+43
-24
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
)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10392,6 +10392,7 @@ inferType
1039210392
| main.rs:2220:26:2220:29 | 2i64 | | {EXTERNAL LOCATION} | i64 |
1039310393
| main.rs:2221:9:2221:15 | S(...) | | main.rs:2104:5:2104:19 | S |
1039410394
| main.rs:2221:9:2221:15 | S(...) | T | {EXTERNAL LOCATION} | i64 |
10395+
| main.rs:2221:9:2221:28 | ... .my_add(...) | | main.rs:2104:5:2104:19 | S |
1039510396
| main.rs:2221:11:2221:14 | 1i64 | | {EXTERNAL LOCATION} | i64 |
1039610397
| main.rs:2221:24:2221:27 | 3i64 | | {EXTERNAL LOCATION} | i64 |
1039710398
| main.rs:2222:9:2222:15 | S(...) | | main.rs:2104:5:2104:19 | S |
@@ -14020,3 +14021,6 @@ inferType
1402014021
| raw_pointer.rs:59:5:59:30 | raw_type_from_deref(...) | | {EXTERNAL LOCATION} | () |
1402114022
| raw_pointer.rs:59:25:59:29 | false | | {EXTERNAL LOCATION} | bool |
1402214023
testFailures
14024+
| main.rs:2220:9:2220:31 | ... .my_add(...) | Unexpected result: target=S::my_add2 |
14025+
| main.rs:2221:9:2221:28 | ... .my_add(...) | Fixed missing result: target=S::my_add2 |
14026+
| main.rs:2222:9:2222:29 | ... .my_add(...) | Unexpected result: target=S::my_add2 |

0 commit comments

Comments
 (0)