Skip to content

Commit f06b943

Browse files
committed
perf
1 parent 85c5935 commit f06b943

1 file changed

Lines changed: 71 additions & 42 deletions

File tree

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

Lines changed: 71 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ private module MethodCallResolution {
12431243
}
12441244

12451245
pragma[nomagic]
1246-
private predicate methodInfoTypeParam(
1246+
predicate methodInfoTypeParam(
12471247
Function m, string name, int arity, ImplOrTraitItemNode i, FunctionType selfType,
12481248
TypePath strippedRootTypePath
12491249
) {
@@ -1853,21 +1853,22 @@ private Type inferMethodCallExprType(AstNode n, TypePath path) {
18531853
private module FunctionCallResolution {
18541854
private import FunctionOverloading
18551855

1856-
bindingset[trait, resolved, result]
1857-
pragma[inline_late]
1858-
Function assocTraitFunctionDependsOnArgument(
1859-
TraitItemNode trait, Function resolved, FunctionTypePosition pos, TypePath path, Type type
1856+
pragma[nomagic]
1857+
private predicate assocTraitFunctionDependsOnArgument(
1858+
TraitItemNode trait, Function traitFunction, FunctionTypePosition pos, Function implFunction,
1859+
TypePath path, Type type
18601860
) {
1861-
not resolved.hasSelfParam() and
1862-
functionTypeAtPath(result, pos, path, type) and
1863-
traitTypeParameterOccurrence(trait, resolved, _, pos, _, _) and
1861+
not traitFunction.hasSelfParam() and
1862+
functionTypeAtPath(implFunction, pos, path, type) and
1863+
implFunction.implements(traitFunction) and
1864+
traitTypeParameterOccurrence(trait, traitFunction, _, pos, _, _) and
18641865
(
18651866
not pos.isReturn()
18661867
or
18671868
// We only check that the context of the call provides relevant type information
18681869
// when no argument can
18691870
not exists(FunctionTypePosition pos0 |
1870-
traitTypeParameterOccurrence(trait, resolved, _, pos0, _, _) and
1871+
traitTypeParameterOccurrence(trait, traitFunction, _, pos0, _, _) and
18711872
not pos0.isReturn()
18721873
)
18731874
)
@@ -1879,51 +1880,35 @@ private module FunctionCallResolution {
18791880
result = CallExprImpl::getResolvedFunction(this)
18801881
}
18811882

1882-
ItemNode getPathResolutionResolvedFunctionOrImplementation(ItemNode resolved) {
1883-
resolved = this.getPathResolutionResolvedFunction() and
1884-
(
1885-
result = resolved
1886-
or
1887-
result.(AssocItem).implements(resolved)
1888-
)
1889-
}
1890-
18911883
pragma[nomagic]
18921884
Type getTypeAt(TypePath path) {
18931885
result = substituteLookupTraits(inferType(this.getArg(0), path))
18941886
}
18951887

18961888
pragma[nomagic]
18971889
predicate hasInfo(
1898-
TypePath strippedRootTypePath, Type strippedRootType, TraitItemNode trait, Function resolved,
1899-
Function f
1890+
TypePath strippedRootTypePath, Type strippedRootType, TraitItemNode trait, Function resolved
19001891
) {
1901-
trait = this.(Call).getTrait() and
19021892
this.getTypeAt(strippedRootTypePath) = strippedRootType and
1903-
MethodCallResolution::isComplexStrippedSelfRoot(strippedRootTypePath, strippedRootType) and
1904-
f = this.getPathResolutionResolvedFunctionOrImplementation(resolved)
1893+
resolved = this.getPathResolutionResolvedFunction() and
1894+
trait = this.(Call).getTrait() and
1895+
MethodCallResolution::isComplexStrippedSelfRoot(strippedRootTypePath, strippedRootType)
19051896
}
19061897

19071898
pragma[nomagic]
1908-
private Function getACandidateTraitMethod() {
1909-
exists(TraitItemNode trait, ImplOrTraitItemNode i, FunctionType self, Function resolved |
1899+
private Function getATargetMethodCand() {
1900+
exists(TraitItemNode trait, ImplOrTraitItemNode i, FunctionType self |
19101901
FunctionMethodCallIsInstantiationOfInput::potentialInstantiationOf(this, trait, i, self,
1911-
resolved, result) and
1902+
result) and
19121903
IsInstantiationOf<FunctionCall, FunctionType, FunctionMethodCallIsInstantiationOfInput>::isInstantiationOf(this,
19131904
i, self)
19141905
)
19151906
}
19161907

19171908
pragma[nomagic]
19181909
private Function getAnAmbigousCandidate(FunctionTypePosition pos, TypePath path, Type type) {
1919-
exists(TraitItemNode trait, Function resolved |
1920-
result = this.getPathResolutionResolvedFunctionOrImplementation(resolved) and
1921-
trait = this.(Call).getTrait() and
1922-
result = assocTraitFunctionDependsOnArgument(trait, resolved, pos, path, type)
1923-
)
1924-
or
19251910
(
1926-
result = this.getACandidateTraitMethod()
1911+
result = this.getATargetMethodCand()
19271912
or
19281913
not this.(Call).hasTrait() and
19291914
result = this.getPathResolutionResolvedFunction()
@@ -1939,18 +1924,32 @@ private module FunctionCallResolution {
19391924
result = substituteLookupTraits(inferType(this, path))
19401925
}
19411926

1927+
pragma[nomagic]
1928+
private predicate hasInfo2(
1929+
TraitItemNode trait, Function resolved, FunctionTypePosition pos, TypePath path, Type type
1930+
) {
1931+
resolved = this.getPathResolutionResolvedFunction() and
1932+
trait = this.(Call).getTrait() and
1933+
type = this.inferArgumentType(pos, path)
1934+
}
1935+
19421936
pragma[nomagic]
19431937
private Function resolveAmbigousFunctionCallTarget() {
19441938
exists(FunctionTypePosition pos, TypePath path, Type type |
19451939
result = this.getAnAmbigousCandidate(pos, path, type) and
19461940
type = this.inferArgumentType(pos, path)
1941+
or
1942+
exists(TraitItemNode trait, Function resolved |
1943+
this.hasInfo2(trait, resolved, pos, path, type) and
1944+
assocTraitFunctionDependsOnArgument(trait, resolved, pos, result, path, type)
1945+
)
19471946
)
19481947
}
19491948

19501949
pragma[nomagic]
19511950
private ItemNode resolveUnambigousFunctionCallTarget() {
19521951
(
1953-
result = this.getACandidateTraitMethod()
1952+
result = this.getATargetMethodCand()
19541953
or
19551954
not this.(Call).hasTrait() and
19561955
result = this.getPathResolutionResolvedFunction()
@@ -1973,22 +1972,52 @@ private module FunctionCallResolution {
19731972
IsInstantiationOfInputSig<FunctionCall, FunctionType>
19741973
{
19751974
pragma[nomagic]
1976-
additional predicate potentialInstantiationOf(
1977-
FunctionCall call, TraitItemNode trait, ImplOrTraitItemNode i, FunctionType self,
1978-
Function resolved, Function f
1975+
private predicate methodInfo(
1976+
TypePath strippedRootTypePath, Type strippedRootType, TraitItemNode trait, Function f,
1977+
Function traitFunction
19791978
) {
1980-
exists(TypePath strippedRootTypePath, Type strippedRootType |
1981-
call.hasInfo(strippedRootTypePath, strippedRootType, trait, resolved, f) and
1982-
MethodCallResolution::methodInfoMatch(f, _, _, i, self, strippedRootTypePath,
1983-
strippedRootType)
1979+
MethodCallResolution::methodInfo(f, _, _, _, _, strippedRootTypePath, strippedRootType) and
1980+
traitFunction = trait.getAnAssocItem() and
1981+
(
1982+
f.implements(traitFunction)
1983+
or
1984+
f = traitFunction
19841985
)
19851986
}
19861987

1988+
pragma[nomagic]
1989+
private predicate methodInfoTypeParam(
1990+
TypePath strippedRootTypePath, TraitItemNode trait, Function f, Function traitFunction
1991+
) {
1992+
MethodCallResolution::methodInfoTypeParam(f, _, _, _, _, strippedRootTypePath) and
1993+
traitFunction = trait.getAnAssocItem() and
1994+
(
1995+
f.implements(traitFunction)
1996+
or
1997+
f = traitFunction
1998+
)
1999+
}
2000+
2001+
pragma[nomagic]
2002+
additional predicate potentialInstantiationOf(
2003+
FunctionCall call, TraitItemNode trait, ImplOrTraitItemNode i, FunctionType selfType,
2004+
Function f
2005+
) {
2006+
exists(Function resolved, TypePath strippedRootTypePath, Type strippedRootType |
2007+
call.hasInfo(strippedRootTypePath, strippedRootType, trait, resolved)
2008+
|
2009+
methodInfo(strippedRootTypePath, strippedRootType, trait, f, resolved)
2010+
or
2011+
methodInfoTypeParam(strippedRootTypePath, trait, f, resolved)
2012+
) and
2013+
MethodCallResolution::methodInfo(f, _, _, i, selfType, _, _)
2014+
}
2015+
19872016
pragma[nomagic]
19882017
predicate potentialInstantiationOf(
19892018
FunctionCall call, TypeAbstraction abs, FunctionType constraint
19902019
) {
1991-
potentialInstantiationOf(call, _, abs, constraint, _, _)
2020+
potentialInstantiationOf(call, _, abs, constraint, _)
19922021
}
19932022

19942023
predicate relevantTypeMention(FunctionType constraint) {

0 commit comments

Comments
 (0)