Skip to content

Commit 6028cd6

Browse files
committed
Address review comments
1 parent fef00c1 commit 6028cd6

File tree

4 files changed

+35
-44
lines changed

4 files changed

+35
-44
lines changed

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

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@ private module MethodResolution {
15381538
pragma[nomagic]
15391539
Type getACandidateReceiverTypeAtNoBorrow(DerefChain derefChain, TypePath path) {
15401540
result = this.getReceiverTypeAt(path) and
1541-
derefChain = ""
1541+
derefChain.isEmpty()
15421542
or
15431543
exists(DerefImplItemNode impl, DerefChain suffix |
15441544
result = ImplicitDeref::getDereferencedCandidateReceiverType(this, impl, suffix, path) and
@@ -1647,7 +1647,7 @@ private module MethodResolution {
16471647
or
16481648
// needed for the `hasNoCompatibleTarget` check in
16491649
// `ReceiverSatisfiesBlanketLikeConstraintInput::hasBlanketCandidate`
1650-
derefChain = ""
1650+
derefChain.isEmpty()
16511651
) and
16521652
strippedType = this.getComplexStrippedType(derefChain, TNoBorrowKind(), strippedTypePath) and
16531653
n = -1
@@ -1680,7 +1680,7 @@ private module MethodResolution {
16801680
or
16811681
// needed for the `hasNoCompatibleTarget` check in
16821682
// `ReceiverSatisfiesBlanketLikeConstraintInput::hasBlanketCandidate`
1683-
derefChain = ""
1683+
derefChain.isEmpty()
16841684
) and
16851685
strippedType = this.getComplexStrippedType(derefChain, TNoBorrowKind(), strippedTypePath) and
16861686
n = -1
@@ -1830,7 +1830,7 @@ private module MethodResolution {
18301830
* as long as the method cannot be resolved in an earlier candidate type, and possibly
18311831
* applying a borrow at the end.
18321832
*
1833-
* The string `derefChain` encodes the sequence of dereferences, and `borrows` indicates
1833+
* The parameter `derefChain` encodes the sequence of dereferences, and `borrows` indicates
18341834
* whether a borrow has been applied.
18351835
*
18361836
* [1]: https://doc.rust-lang.org/reference/expressions/method-call-expr.html#r-expr.method.candidate-receivers
@@ -1865,8 +1865,8 @@ private module MethodResolution {
18651865

18661866
/**
18671867
* Gets a method that this call resolves to after having applied a sequence of
1868-
* dereferences and possibly a borrow on the receiver type, encoded in the string
1869-
* `derefChain` and the enum `borrow`.
1868+
* dereferences and possibly a borrow on the receiver type, encoded in `derefChain`
1869+
* and `borrow`.
18701870
*/
18711871
pragma[nomagic]
18721872
Method resolveCallTarget(ImplOrTraitItemNode i, DerefChain derefChain, BorrowKind borrow) {
@@ -1881,9 +1881,7 @@ private module MethodResolution {
18811881
* and borrowed according to `derefChain` and `borrow`, in order to be able to
18821882
* resolve the call target.
18831883
*/
1884-
predicate argumentHasImplicitDerefChainBorrow(
1885-
AstNode arg, DerefChain derefChain, BorrowKind borrow
1886-
) {
1884+
predicate argumentHasImplicitDerefChainBorrow(Expr arg, DerefChain derefChain, BorrowKind borrow) {
18871885
exists(this.resolveCallTarget(_, derefChain, borrow)) and
18881886
arg = this.getArg(any(ArgumentPosition pos | pos.isSelf())) and
18891887
not (derefChain.isEmpty() and borrow.isNoBorrow())
@@ -2025,7 +2023,7 @@ private module MethodResolution {
20252023
}
20262024

20272025
override predicate argumentHasImplicitDerefChainBorrow(
2028-
AstNode arg, DerefChain derefChain, BorrowKind borrow
2026+
Expr arg, DerefChain derefChain, BorrowKind borrow
20292027
) {
20302028
exists(ArgumentPosition pos, boolean isMutable |
20312029
this.implicitBorrowAt(pos, isMutable) and
@@ -2161,7 +2159,7 @@ private module MethodResolution {
21612159
MkMethodCallDerefCand(MethodCall mc, DerefChain derefChain) {
21622160
mc.supportsAutoDerefAndBorrow() and
21632161
mc.hasNoCompatibleTargetMutBorrow(derefChain) and
2164-
exists(mc.getACandidateReceiverTypeAtNoBorrow(derefChain, _))
2162+
exists(mc.getACandidateReceiverTypeAtNoBorrow(derefChain, TypePath::nil()))
21652163
}
21662164

21672165
/** A method call with a dereference chain. */
@@ -2579,6 +2577,13 @@ private Type inferMethodCallTypeNonSelf(AstNode n, boolean isReturn, TypePath pa
25792577
)
25802578
}
25812579

2580+
/**
2581+
* Gets the type of `n` at `path` after applying `derefChain` and `borrow`,
2582+
* where `n` is the `self` argument of a method call.
2583+
*
2584+
* The predicate recursively pops the head of `derefChain` until it becomes
2585+
* empty, at which point the inferred type can be applied back to `n`.
2586+
*/
25822587
pragma[nomagic]
25832588
private Type inferMethodCallTypeSelf(
25842589
AstNode n, DerefChain derefChain, BorrowKind borrow, TypePath path
@@ -2604,14 +2609,14 @@ private Type inferMethodCallTypeSelf(
26042609
t0 = inferMethodCallTypeSelf(n, derefChain0, borrow, path0) and
26052610
derefChain0.isCons(impl, derefChain) and
26062611
borrow.isNoBorrow() and
2607-
selfParamType = impl.resolveSelfParamTypeStrippedAt(selfPath)
2612+
selfParamType = impl.resolveSelfTypeAt(selfPath)
26082613
|
26092614
result = selfParamType and
26102615
path = selfPath and
26112616
not result instanceof TypeParameter
26122617
or
26132618
exists(TypeParameter tp, TypePath pathToTypeParam, TypePath suffix |
2614-
impl.returnTypeStrippedMentionsTypeParameterAt(tp, pathToTypeParam) and
2619+
impl.targetTypeParameterAt(tp, pathToTypeParam) and
26152620
path0 = pathToTypeParam.appendInverse(suffix) and
26162621
result = t0 and
26172622
path = selfPath.append(suffix)
@@ -3911,13 +3916,13 @@ cached
39113916
private module Cached {
39123917
/** Holds if `n` is implicitly dereferenced and/or borrowed. */
39133918
cached
3914-
predicate implicitDerefChainBorrow(AstNode n, DerefChain derefChain, boolean borrow) {
3919+
predicate implicitDerefChainBorrow(Expr e, DerefChain derefChain, boolean borrow) {
39153920
exists(BorrowKind bk |
3916-
any(MethodResolution::MethodCall mc).argumentHasImplicitDerefChainBorrow(n, derefChain, bk) and
3921+
any(MethodResolution::MethodCall mc).argumentHasImplicitDerefChainBorrow(e, derefChain, bk) and
39173922
if bk.isNoBorrow() then borrow = false else borrow = true
39183923
)
39193924
or
3920-
n =
3925+
e =
39213926
any(FieldExpr fe |
39223927
exists(resolveStructFieldExpr(fe, derefChain))
39233928
or

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ class NonAliasPathTypeMention extends PathTypeMention {
309309
}
310310

311311
pragma[nomagic]
312-
private Type resolveImplSelfTypeAt(Impl i, TypePath path) {
312+
Type resolveImplSelfTypeAt(Impl i, TypePath path) {
313313
result = i.getSelfTy().(TypeMention).resolveTypeAt(path)
314314
}
315315

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

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,16 @@ class DerefImplItemNode extends ImplItemNode {
1616
/** Gets the `deref` function in this `Deref` impl block. */
1717
Function getDerefFunction() { result = this.getAssocItem("deref") }
1818

19-
private SelfParam getSelfParam() { result = this.getDerefFunction().getSelfParam() }
19+
/** Gets the type of the implementing type at `path`. */
20+
Type resolveSelfTypeAt(TypePath path) { result = resolveImplSelfTypeAt(this, path) }
2021

2122
/**
22-
* Resolves the type at `path` of the `self` parameter inside the `deref` function,
23-
* stripped of the leading `&`.
23+
* Holds if the target type of the dereference implemention mentions type
24+
* parameter `tp` at `path`.
2425
*/
2526
pragma[nomagic]
26-
Type resolveSelfParamTypeStrippedAt(TypePath path) {
27-
exists(TypePath path0 |
28-
result = getSelfParamTypeMention(this.getSelfParam()).resolveTypeAt(path0) and
29-
path0.isCons(getRefTypeParameter(false), path)
30-
)
31-
}
32-
33-
/**
34-
* Holds if the return type at `path` of the `deref` function, stripped of the
35-
* leading `&`, mentions type parameter `tp` at `path`.
36-
*/
37-
pragma[nomagic]
38-
predicate returnTypeStrippedMentionsTypeParameterAt(TypeParameter tp, TypePath path) {
39-
exists(TypePath path0 |
40-
tp = getReturnTypeMention(this.getDerefFunction()).resolveTypeAt(path0) and
41-
path0.isCons(getRefTypeParameter(false), path)
42-
)
27+
predicate targetTypeParameterAt(TypeParameter tp, TypePath path) {
28+
tp = this.getAssocItem("Target").(TypeAlias).getTypeRepr().(TypeMention).resolveTypeAt(path)
4329
}
4430

4531
/** Gets the first type parameter of the type being implemented, if any. */

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
974974
}
975975

976976
pragma[inline]
977-
private predicate satisfiesConstraintTypeMention1Inline(
977+
private predicate satisfiesConstraintTypeMentionInline(
978978
HasTypeTree tt, TypeAbstraction abs, Type constraint, TypePath path,
979979
TypePath pathToTypeParamInSub
980980
) {
@@ -986,18 +986,18 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
986986
}
987987

988988
pragma[nomagic]
989-
private predicate satisfiesConstraintTypeMention1(
989+
private predicate satisfiesConstraintTypeMention(
990990
HasTypeTree tt, Type constraint, TypePath path, TypePath pathToTypeParamInSub
991991
) {
992-
satisfiesConstraintTypeMention1Inline(tt, _, constraint, path, pathToTypeParamInSub)
992+
satisfiesConstraintTypeMentionInline(tt, _, constraint, path, pathToTypeParamInSub)
993993
}
994994

995995
pragma[nomagic]
996-
private predicate satisfiesConstraintTypeMention1Through(
996+
private predicate satisfiesConstraintTypeMentionThrough(
997997
HasTypeTree tt, TypeAbstraction abs, Type constraint, TypePath path,
998998
TypePath pathToTypeParamInSub
999999
) {
1000-
satisfiesConstraintTypeMention1Inline(tt, abs, constraint, path, pathToTypeParamInSub)
1000+
satisfiesConstraintTypeMentionInline(tt, abs, constraint, path, pathToTypeParamInSub)
10011001
}
10021002

10031003
pragma[inline]
@@ -1017,7 +1017,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
10171017
satisfiesConstraintTypeNonTypeParamInline(tt, _, constraint, path, t)
10181018
or
10191019
exists(TypePath prefix0, TypePath pathToTypeParamInSub, TypePath suffix |
1020-
satisfiesConstraintTypeMention1(tt, constraint, prefix0, pathToTypeParamInSub) and
1020+
satisfiesConstraintTypeMention(tt, constraint, prefix0, pathToTypeParamInSub) and
10211021
getTypeAt(tt, pathToTypeParamInSub.appendInverse(suffix)) = t and
10221022
path = prefix0.append(suffix)
10231023
)
@@ -1037,7 +1037,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
10371037
satisfiesConstraintTypeNonTypeParamInline(tt, abs, constraint, path, t)
10381038
or
10391039
exists(TypePath prefix0, TypePath pathToTypeParamInSub, TypePath suffix |
1040-
satisfiesConstraintTypeMention1Through(tt, abs, constraint, prefix0, pathToTypeParamInSub) and
1040+
satisfiesConstraintTypeMentionThrough(tt, abs, constraint, prefix0, pathToTypeParamInSub) and
10411041
getTypeAt(tt, pathToTypeParamInSub.appendInverse(suffix)) = t and
10421042
path = prefix0.append(suffix)
10431043
)

0 commit comments

Comments
 (0)