Skip to content

Commit 2748229

Browse files
committed
perf
1 parent cb8a603 commit 2748229

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,17 @@ module TraitIsVisible<relevantTraitVisibleSig/2 relevantTraitVisible> {
14461446
predicate traitIsVisible(Element element, Trait trait) {
14471447
exists(ItemNode encl | traitLookup(encl, element, trait) and trait = encl.getASuccessor(_, _))
14481448
}
1449+
1450+
/** Holds if the trait `trait` is _not_ visible at `element`. */
1451+
pragma[nomagic]
1452+
predicate traitIsNotVisible(Element element, Trait trait) {
1453+
relevantTraitVisible(element, trait) and
1454+
exists(ItemNode encl |
1455+
traitLookup(encl, element, trait) and
1456+
not trait = encl.getASuccessor(_, _) and
1457+
not exists(getOuterScope(encl))
1458+
)
1459+
}
14491460
}
14501461

14511462
pragma[nomagic]

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

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,11 +1167,29 @@ private module MethodCallResolution {
11671167
}
11681168

11691169
pragma[nomagic]
1170-
private predicate isMethodCall0(Type rootType, string name, int arity, string derefChainBorrow) {
1170+
predicate isMethodCall0(Type rootType, string name, int arity, string derefChainBorrow) {
11711171
rootType = this.getACandidateReceiverTypeAt(TypePath::nil(), derefChainBorrow) and
11721172
this.isMethodCall(name, arity)
11731173
}
11741174

1175+
pragma[nomagic]
1176+
private predicate isNot(
1177+
ImplOrTraitItemNode i, FunctionPositionType self, string derefChainBorrow
1178+
) {
1179+
IsInstantiationOf<MethodCallCand, FunctionPositionType, MethodCallIsInstantiationOfInput>::isNotInstantiationOf(MkMethodCallCand(this,
1180+
derefChainBorrow), i, self)
1181+
or
1182+
exists(Trait trait |
1183+
relevantTraitVisible0(i, self, derefChainBorrow, this, trait) and
1184+
TraitIsVisible<relevantTraitVisible1/2>::traitIsNotVisible(this,
1185+
pragma[only_bind_into](trait)) and
1186+
not (
1187+
this instanceof IndexExpr and
1188+
trait instanceof IndexTrait
1189+
)
1190+
)
1191+
}
1192+
11751193
pragma[nomagic]
11761194
private Type getACandidateReceiverTypeAtNoBorrowNoMatch(TypePath path, string derefChain) {
11771195
result = this.getACandidateReceiverTypeAtNoBorrow(path, derefChain) and
@@ -1180,11 +1198,12 @@ private module MethodCallResolution {
11801198
not derefChain.matches("%.ref") and // no need to try a borrow if the last thing we did was a deref
11811199
this.isMethodCall0(rootType, name, arity, derefChainBorrow)
11821200
|
1183-
forall(Impl impl, FunctionPositionType self |
1184-
methodCandidate(rootType, name, arity, impl, self)
1201+
forall(ImplOrTraitItemNode i, FunctionPositionType self |
1202+
methodCandidate(rootType, name, arity, i, self)
11851203
|
1186-
IsInstantiationOf<MethodCallCand, FunctionPositionType, MethodCallIsInstantiationOfInput>::isNotInstantiationOf(MkMethodCallCand(this,
1187-
derefChainBorrow), impl, self)
1204+
this.isNot(i, self, derefChainBorrow)
1205+
// IsInstantiationOf<MethodCallCand, FunctionPositionType, MethodCallIsInstantiationOfInput>::isNotInstantiationOf(MkMethodCallCand(this,
1206+
// derefChainBorrow), i, self)
11881207
)
11891208
)
11901209
}
@@ -1196,11 +1215,12 @@ private module MethodCallResolution {
11961215
derefChainBorrow = derefChain + ";borrow" and
11971216
this.isMethodCall0(rootType, name, arity, derefChainBorrow)
11981217
|
1199-
forall(Impl impl, FunctionPositionType self |
1200-
methodCandidate(rootType, name, arity, impl, self)
1218+
forall(ImplOrTraitItemNode i, FunctionPositionType self |
1219+
methodCandidate(rootType, name, arity, i, self)
12011220
|
1202-
IsInstantiationOf<MethodCallCand, FunctionPositionType, MethodCallIsInstantiationOfInput>::isNotInstantiationOf(MkMethodCallCand(this,
1203-
derefChainBorrow), impl, self)
1221+
// IsInstantiationOf<MethodCallCand, FunctionPositionType, MethodCallIsInstantiationOfInput>::isNotInstantiationOf(MkMethodCallCand(this,
1222+
// derefChainBorrow), i, self)
1223+
this.isNot(i, self, derefChainBorrow)
12041224
)
12051225
)
12061226
}
@@ -1256,6 +1276,20 @@ private module MethodCallResolution {
12561276
}
12571277
}
12581278

1279+
private predicate relevantTraitVisible0(
1280+
ImplItemNode i, FunctionPositionType self, string derefChainBorrow, MethodCall mc, Trait trait
1281+
) {
1282+
exists(Type rootType, string name, int arity |
1283+
mc.(MethodCall).isMethodCall0(rootType, name, arity, derefChainBorrow) and
1284+
methodCandidate(rootType, name, arity, i, self) and
1285+
trait = i.resolveTraitTy()
1286+
)
1287+
}
1288+
1289+
private predicate relevantTraitVisible1(Element mc, Trait trait) {
1290+
relevantTraitVisible0(_, _, _, mc, trait)
1291+
}
1292+
12591293
private class MethodCallMethodCallExpr extends MethodCall, MethodCallExpr {
12601294
pragma[nomagic]
12611295
override predicate isMethodCall(string name, int arity) {

shared/typeinference/codeql/typeinference/internal/TypeInference.qll

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,16 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
11311131
)
11321132
}
11331133

1134+
pragma[nomagic]
1135+
private predicate directTypeMatch0(
1136+
Access a, State state, Declaration target, DeclarationPosition dpos,
1137+
TypePath pathToTypeParam, TypeParameter tp
1138+
) {
1139+
not exists(getTypeArgument(a, target, tp, _)) and
1140+
tp = target.getDeclaredType(dpos, pathToTypeParam) and
1141+
target = a.getTarget(state)
1142+
}
1143+
11341144
/**
11351145
* Holds if the type `t` at `path` of `a` in `state` matches the type parameter `tp`
11361146
* of `target`.
@@ -1139,11 +1149,9 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
11391149
private predicate directTypeMatch(
11401150
Access a, State state, Declaration target, TypePath path, Type t, TypeParameter tp
11411151
) {
1142-
not exists(getTypeArgument(a, target, tp, _)) and
11431152
exists(AccessPosition apos, DeclarationPosition dpos, TypePath pathToTypeParam |
1144-
tp = target.getDeclaredType(dpos, pathToTypeParam) and
1153+
directTypeMatch0(a, state, target, dpos, pathToTypeParam, tp) and
11451154
accessDeclarationPositionMatch(apos, dpos) and
1146-
target = a.getTarget(state) and
11471155
t = a.getInferredType(state, apos, pathToTypeParam.appendInverse(path))
11481156
)
11491157
}

0 commit comments

Comments
 (0)