Skip to content

Commit 8cde1ee

Browse files
committed
Rust: Remove Access from adjustAccessType
1 parent f138f77 commit 8cde1ee

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

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

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/** Provides functionality for inferring types. */
22

3+
private import codeql.util.Boolean
34
private import rust
45
private import PathResolution
56
private import Type
@@ -638,7 +639,28 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
638639
}
639640
}
640641

641-
class AccessPosition = DeclarationPosition;
642+
private newtype TAccessPosition =
643+
TSelfAccessPosition(Boolean implicitlyBorrowed) or
644+
TPositionalAccessPosition(int pos) { exists(TPositionalDeclarationPosition(pos)) } or
645+
TReturnAccessPosition()
646+
647+
class AccessPosition extends TAccessPosition {
648+
predicate isSelf(boolean implicitlyBorrowed) { this = TSelfAccessPosition(implicitlyBorrowed) }
649+
650+
int asPosition() { this = TPositionalAccessPosition(result) }
651+
652+
predicate isReturn() { this = TReturnAccessPosition() }
653+
654+
string toString() {
655+
this.isSelf(_) and
656+
result = "self"
657+
or
658+
result = this.asPosition().toString()
659+
or
660+
this.isReturn() and
661+
result = "(return)"
662+
}
663+
}
642664

643665
private import codeql.rust.elements.internal.CallExprImpl::Impl as CallExprImpl
644666

@@ -655,7 +677,8 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
655677
AstNode getNodeAt(AccessPosition apos) {
656678
result = this.getArgument(apos.asPosition())
657679
or
658-
result = this.getReceiver() and apos.isSelf()
680+
result = this.getReceiver() and
681+
if this.receiverImplicitlyBorrowed() then apos.isSelf(true) else apos.isSelf(false)
659682
or
660683
result = this and apos.isReturn()
661684
}
@@ -672,16 +695,19 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
672695
}
673696

674697
predicate accessDeclarationPositionMatch(AccessPosition apos, DeclarationPosition dpos) {
675-
apos = dpos
698+
apos.isSelf(_) and dpos.isSelf()
699+
or
700+
apos.asPosition() = dpos.asPosition()
701+
or
702+
apos.isReturn() and dpos.isReturn()
676703
}
677704

678-
bindingset[a, apos, target, path, t]
705+
bindingset[apos, target, path, t]
679706
pragma[inline_late]
680707
predicate adjustAccessType(
681-
Access a, AccessPosition apos, Declaration target, TypePath path, Type t, TypePath pathAdj,
682-
Type tAdj
708+
AccessPosition apos, Declaration target, TypePath path, Type t, TypePath pathAdj, Type tAdj
683709
) {
684-
if apos.isSelf() and a.receiverImplicitlyBorrowed()
710+
if apos.isSelf(true)
685711
then
686712
exists(Type selfParamType |
687713
selfParamType = target.getParameterType(TSelfDeclarationPosition(), TypePath::nil())
@@ -741,7 +767,7 @@ private Type inferCallExprBaseType(AstNode n, TypePath path) {
741767
n = a.getNodeAt(apos) and
742768
result = CallExprBaseMatching::inferAccessType(a, apos, path0)
743769
|
744-
if apos.isSelf()
770+
if apos.isSelf(_)
745771
then
746772
exists(Type receiverType | receiverType = inferType(n) |
747773
if receiverType = TRefType()
@@ -845,13 +871,11 @@ private module FieldExprMatchingInput implements MatchingInputSig {
845871
apos = dpos
846872
}
847873

848-
bindingset[a, apos, target, path, t]
874+
bindingset[apos, target, path, t]
849875
pragma[inline_late]
850876
predicate adjustAccessType(
851-
Access a, AccessPosition apos, Declaration target, TypePath path, Type t, TypePath pathAdj,
852-
Type tAdj
877+
AccessPosition apos, Declaration target, TypePath path, Type t, TypePath pathAdj, Type tAdj
853878
) {
854-
exists(a) and
855879
exists(target) and
856880
if apos.isSelf()
857881
then
@@ -1220,7 +1244,7 @@ private module Cached {
12201244
cached
12211245
predicate receiverHasImplicitDeref(AstNode receiver) {
12221246
exists(CallExprBaseMatchingInput::Access a, CallExprBaseMatchingInput::AccessPosition apos |
1223-
apos.isSelf() and
1247+
apos.isSelf(true) and
12241248
receiver = a.getNodeAt(apos) and
12251249
inferType(receiver) = TRefType() and
12261250
CallExprBaseMatching::inferAccessType(a, apos, TypePath::nil()) != TRefType()
@@ -1231,7 +1255,7 @@ private module Cached {
12311255
cached
12321256
predicate receiverHasImplicitBorrow(AstNode receiver) {
12331257
exists(CallExprBaseMatchingInput::Access a, CallExprBaseMatchingInput::AccessPosition apos |
1234-
apos.isSelf() and
1258+
apos.isSelf(true) and
12351259
receiver = a.getNodeAt(apos) and
12361260
CallExprBaseMatching::inferAccessType(a, apos, TypePath::nil()) = TRefType() and
12371261
inferType(receiver) != TRefType()

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -847,10 +847,9 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
847847
* the inferred type of `42` is `int`, but it should be adjusted to `int?`
848848
* when matching against `M`.
849849
*/
850-
bindingset[a, apos, target, path, t]
850+
bindingset[apos, target, path, t]
851851
default predicate adjustAccessType(
852-
Access a, AccessPosition apos, Declaration target, TypePath path, Type t, TypePath pathAdj,
853-
Type tAdj
852+
AccessPosition apos, Declaration target, TypePath path, Type t, TypePath pathAdj, Type tAdj
854853
) {
855854
pathAdj = path and
856855
tAdj = t
@@ -877,7 +876,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
877876
target = a.getTarget() and
878877
exists(TypePath path0, Type t0 |
879878
t0 = a.getInferredType(apos, path0) and
880-
adjustAccessType(a, apos, target, path0, t0, path, t)
879+
adjustAccessType(apos, target, path0, t0, path, t)
881880
)
882881
}
883882

0 commit comments

Comments
 (0)