Skip to content

Commit b749ad6

Browse files
authored
Merge pull request #21706 from hvitved/rust/type-inference-perf-fixes
Rust: Improve performance of two type inference predicates
2 parents e095294 + d69be77 commit b749ad6

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,15 @@ private module AssocFunctionResolution {
17081708

17091709
predicate hasReceiverAtPos(FunctionPosition pos) { this.hasReceiver() and pos.asPosition() = 0 }
17101710

1711+
pragma[nomagic]
1712+
private predicate hasIncompatibleArgsTarget(
1713+
ImplOrTraitItemNode i, FunctionPosition selfPos, DerefChain derefChain, BorrowKind borrow,
1714+
AssocFunctionType selfType
1715+
) {
1716+
SelfArgIsInstantiationOf::argIsInstantiationOf(this, i, selfPos, derefChain, borrow, selfType) and
1717+
OverloadedCallArgsAreInstantiationsOf::argsAreNotInstantiationsOf(this, i)
1718+
}
1719+
17111720
/**
17121721
* Holds if the function inside `i` with matching name and arity can be ruled
17131722
* out as a target of this call, because the candidate receiver type represented
@@ -1722,16 +1731,11 @@ private module AssocFunctionResolution {
17221731
ImplOrTraitItemNode i, FunctionPosition selfPos, DerefChain derefChain, BorrowKind borrow,
17231732
Type root
17241733
) {
1725-
exists(TypePath path |
1726-
SelfArgIsInstantiationOf::argIsNotInstantiationOf(this, i, selfPos, derefChain, borrow, path) and
1727-
path.isCons(root.getATypeParameter(), _)
1728-
)
1729-
or
1730-
exists(AssocFunctionType selfType |
1731-
SelfArgIsInstantiationOf::argIsInstantiationOf(this, i, selfPos, derefChain, borrow,
1732-
selfType) and
1733-
OverloadedCallArgsAreInstantiationsOf::argsAreNotInstantiationsOf(this, i) and
1734-
root = selfType.getTypeAt(TypePath::nil())
1734+
exists(AssocFunctionType selfType | root = selfType.getTypeAt(TypePath::nil()) |
1735+
this.hasIncompatibleArgsTarget(i, selfPos, derefChain, borrow, selfType)
1736+
or
1737+
SelfArgIsInstantiationOf::argIsNotInstantiationOf(this, i, selfPos, derefChain, borrow,
1738+
selfType)
17351739
)
17361740
}
17371741

@@ -2608,9 +2612,13 @@ private module AssocFunctionResolution {
26082612
pragma[nomagic]
26092613
predicate argIsNotInstantiationOf(
26102614
AssocFunctionCall afc, ImplOrTraitItemNode i, FunctionPosition selfPos, DerefChain derefChain,
2611-
BorrowKind borrow, TypePath path
2615+
BorrowKind borrow, AssocFunctionType selfType
26122616
) {
2613-
argIsNotInstantiationOf(MkAssocFunctionCallCand(afc, selfPos, derefChain, borrow), i, _, path)
2617+
exists(TypePath path |
2618+
argIsNotInstantiationOf(MkAssocFunctionCallCand(afc, selfPos, derefChain, borrow), i,
2619+
selfType, path) and
2620+
not path.isEmpty()
2621+
)
26142622
}
26152623

26162624
pragma[nomagic]

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -566,15 +566,17 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
566566
)
567567
}
568568

569+
pragma[nomagic]
569570
private predicate typeParametersEqual(
570-
App app, TypeAbstraction abs, Constraint constraint, TypeParameter tp
571+
App app, TypeAbstraction abs, Constraint constraint, int i
571572
) {
572-
satisfiesConcreteTypes(app, abs, constraint) and
573-
tp = getNthTypeParameter(abs, _) and
574-
(
573+
exists(TypeParameter tp |
574+
satisfiesConcreteTypes(app, abs, constraint) and
575+
tp = getNthTypeParameter(abs, i)
576+
|
575577
not exists(getNthTypeParameterPath(constraint, tp, _))
576578
or
577-
exists(int n | n = max(int i | exists(getNthTypeParameterPath(constraint, tp, i))) |
579+
exists(int n | n = max(int j | exists(getNthTypeParameterPath(constraint, tp, j))) |
578580
// If the largest index is 0, then there are no equalities to check as
579581
// the type parameter only occurs once.
580582
if n = 0 then any() else typeParametersEqualToIndex(app, abs, constraint, tp, _, n)
@@ -585,12 +587,10 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
585587
private predicate typeParametersHaveEqualInstantiationToIndex(
586588
App app, TypeAbstraction abs, Constraint constraint, int i
587589
) {
588-
exists(TypeParameter tp | tp = getNthTypeParameter(abs, i) |
589-
typeParametersEqual(app, abs, constraint, tp) and
590-
if i = 0
591-
then any()
592-
else typeParametersHaveEqualInstantiationToIndex(app, abs, constraint, i - 1)
593-
)
590+
typeParametersEqual(app, abs, constraint, i) and
591+
if i = 0
592+
then any()
593+
else typeParametersHaveEqualInstantiationToIndex(app, abs, constraint, i - 1)
594594
}
595595

596596
/**

0 commit comments

Comments
 (0)