Skip to content

Commit 51d84ef

Browse files
committed
wip4
1 parent 0a94c7e commit 51d84ef

File tree

1 file changed

+87
-8
lines changed

1 file changed

+87
-8
lines changed

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

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,6 +2051,19 @@ private module AssocFunctionResolution {
20512051
borrow = TSomeBorrowKind(true)
20522052
}
20532053

2054+
private import codeql.rust.elements.internal.generated.Raw
2055+
private import codeql.rust.elements.internal.generated.Synth
2056+
2057+
private class RawImplOrTrait = @impl or @trait;
2058+
2059+
private predicate id(RawImplOrTrait x, RawImplOrTrait y) { x = y }
2060+
2061+
private predicate idOfRaw(RawImplOrTrait x, int y) = equivalenceRelation(id/2)(x, y)
2062+
2063+
private int idOfImplOrTraitItemNode(ImplOrTraitItemNode i) {
2064+
idOfRaw(Synth::convertAstNodeToRaw(i), result)
2065+
}
2066+
20542067
bindingset[derefChain]
20552068
private signature predicate fooSig(
20562069
AssocFunctionCall afc, FunctionPosition selfPos, DerefChain derefChain, BorrowKind borrow
@@ -2080,22 +2093,88 @@ private module AssocFunctionResolution {
20802093
t = getNthLookupType(afc, strippedType, n)
20812094
}
20822095

2083-
// forex using recursion
20842096
pragma[nomagic]
2085-
private predicate hasNoCompatibleNonBlanketLikeTargetCheckToIndex(
2097+
private ImplOrTraitItemNode getNthNonBlanketLikeCandidate(
2098+
AssocFunctionCall afc, FunctionPosition selfPos, DerefChain derefChain, BorrowKind borrow,
2099+
TypePath strippedTypePath, Type strippedType, int n, Type t, int rnk
2100+
) {
2101+
bar(afc, selfPos, derefChain, borrow, strippedTypePath, strippedType, n, t) and
2102+
// foo(afc, selfPos, derefChain, borrow, strippedTypePath, strippedType, _) and
2103+
// t = getNthLookupType(afc, strippedType, n) and
2104+
result =
2105+
rank[rnk + 1](ImplOrTraitItemNode i, int id |
2106+
nonBlanketLikeCandidate(afc, selfPos, i, _, strippedTypePath, t) and
2107+
id = idOfImplOrTraitItemNode(i)
2108+
|
2109+
i order by id
2110+
)
2111+
}
2112+
2113+
pragma[nomagic]
2114+
private int getLastNonBlanketLikeCandidateIndex(
20862115
AssocFunctionCall afc, FunctionPosition selfPos, DerefChain derefChain, BorrowKind borrow,
20872116
TypePath strippedTypePath, Type strippedType, int n
20882117
) {
2118+
// max(int rnk |
2119+
// exists(
2120+
// getNthNonBlanketLikeCandidate(afc, selfPos, derefChain, borrow, strippedTypePath,
2121+
// strippedType, n, t, rnk)
2122+
// )
2123+
// )
20892124
exists(Type t |
2090-
bar(afc, selfPos, derefChain, borrow, strippedTypePath, strippedType, n, t) and
2091-
forall(ImplOrTraitItemNode i |
2092-
nonBlanketLikeCandidate(afc, selfPos, i, _, strippedTypePath, t)
2093-
|
2125+
foo(afc, selfPos, derefChain, borrow, strippedTypePath, strippedType, _) and
2126+
t = getNthLookupType(afc, strippedType, n) and
2127+
result =
2128+
count(ImplOrTraitItemNode i |
2129+
nonBlanketLikeCandidate(afc, selfPos, i, _, strippedTypePath, t)
2130+
) - 1
2131+
)
2132+
}
2133+
2134+
// forex using recursion
2135+
pragma[nomagic]
2136+
private predicate hasNoCompatibleNonBlanketLikeTargetCheckToIndexToIndex(
2137+
AssocFunctionCall afc, FunctionPosition selfPos, DerefChain derefChain, BorrowKind borrow,
2138+
TypePath strippedTypePath, Type strippedType, int n, int rnk
2139+
) {
2140+
// afc = Debug::getRelevantLocatable() and
2141+
exists(Type t | bar(afc, selfPos, derefChain, borrow, strippedTypePath, strippedType, n, t) |
2142+
rnk = -1
2143+
or
2144+
hasNoCompatibleNonBlanketLikeTargetCheckToIndexToIndex(afc, selfPos, derefChain, borrow,
2145+
strippedTypePath, strippedType, n, rnk - 1) and
2146+
exists(ImplOrTraitItemNode i |
2147+
i =
2148+
getNthNonBlanketLikeCandidate(afc, selfPos, derefChain, borrow, strippedTypePath,
2149+
strippedType, n, t, rnk) and
20942150
afc.hasIncompatibleTarget(i, selfPos, derefChain, borrow, t)
20952151
)
20962152
)
20972153
}
20982154

2155+
// forex using recursion
2156+
pragma[nomagic]
2157+
private predicate hasNoCompatibleNonBlanketLikeTargetCheckToIndex(
2158+
AssocFunctionCall afc, FunctionPosition selfPos, DerefChain derefChain, BorrowKind borrow,
2159+
TypePath strippedTypePath, Type strippedType, int n
2160+
) {
2161+
exists(int last |
2162+
last =
2163+
getLastNonBlanketLikeCandidateIndex(afc, selfPos, derefChain, borrow, strippedTypePath,
2164+
strippedType, n) and
2165+
hasNoCompatibleNonBlanketLikeTargetCheckToIndexToIndex(afc, selfPos, derefChain, borrow,
2166+
strippedTypePath, strippedType, n, last)
2167+
)
2168+
// exists(Type t |
2169+
// bar(afc, selfPos, derefChain, borrow, strippedTypePath, strippedType, n, t) and
2170+
// forall(ImplOrTraitItemNode i |
2171+
// nonBlanketLikeCandidate(afc, selfPos, i, _, strippedTypePath, t)
2172+
// |
2173+
// afc.hasIncompatibleTarget(i, selfPos, derefChain, borrow, t)
2174+
// )
2175+
// )
2176+
}
2177+
20992178
pragma[nomagic]
21002179
predicate hasNoCompatibleNonBlanketLikeTargetCheck(
21012180
AssocFunctionCall afc, FunctionPosition selfPos, DerefChain derefChain, BorrowKind borrow
@@ -4005,8 +4084,8 @@ private module Debug {
40054084
Locatable getRelevantLocatable() {
40064085
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
40074086
result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
4008-
filepath.matches("%/main.rs") and
4009-
startline = 1986
4087+
filepath.matches("%/overloading.rs") and
4088+
startline = [398 .. 399]
40104089
)
40114090
}
40124091

0 commit comments

Comments
 (0)