Skip to content

Commit afb0520

Browse files
committed
wip
1 parent e5ee765 commit afb0520

File tree

1 file changed

+41
-71
lines changed

1 file changed

+41
-71
lines changed

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

Lines changed: 41 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2782,24 +2782,30 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
27822782
)
27832783
}
27842784

2785-
abstract class Access extends Expr {
2786-
abstract Type getTypeArgument(TypeArgumentPosition apos, TypePath path);
2785+
private string noDerefChainBorrow() {
2786+
exists(DerefChain derefChain, BorrowKind borrow |
2787+
derefChain.isEmpty() and
2788+
borrow.isNoBorrow() and
2789+
result = encodeDerefChainBorrow(derefChain, borrow)
2790+
)
2791+
}
27872792

2793+
abstract class Access extends ContextTyping::ContextTypedCallCand {
27882794
abstract AstNode getNodeAt(FunctionPosition posAdj);
27892795

27902796
bindingset[derefChainBorrow]
2791-
abstract Type getInferredType(string derefChainBorrow, FunctionPosition pos, TypePath path);
2797+
abstract Type getInferredType(string derefChainBorrow, FunctionPosition posAdj, TypePath path);
27922798

27932799
abstract Declaration getTarget(string derefChainBorrow);
27942800

2801+
/**
2802+
* Holds if the return type of this call at `path` may have to be inferred
2803+
* from the context.
2804+
*/
27952805
abstract predicate hasUnknownTypeAt(string derefChainBorrow, FunctionPosition pos, TypePath path);
2796-
2797-
abstract predicate hasUnknownTypeAt(FunctionPosition pos, TypePath path);
27982806
}
27992807

2800-
final private class AssocFunctionCallFinal = AssocFunctionResolution::AssocFunctionCall;
2801-
2802-
private class AssocFunctionCallAccess extends Access, ContextTyping::ContextTypedCallCand instanceof AssocFunctionResolution::AssocFunctionCall
2808+
private class AssocFunctionCallAccess extends Access instanceof AssocFunctionResolution::AssocFunctionCall
28032809
{
28042810
AssocFunctionCallAccess() {
28052811
// handled in the `OperationMatchingInput` module
@@ -2819,7 +2825,7 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
28192825
}
28202826

28212827
override AstNode getNodeAt(FunctionPosition posAdj) {
2822-
result = AssocFunctionCallFinal.super.getNodeAt(posAdj)
2828+
result = AssocFunctionResolution::AssocFunctionCall.super.getNodeAt(posAdj)
28232829
}
28242830

28252831
pragma[nomagic]
@@ -2858,23 +2864,13 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
28582864
)
28592865
}
28602866

2861-
/**
2862-
* Holds if the return type of this call at `path` may have to be inferred
2863-
* from the context.
2864-
*/
28652867
pragma[nomagic]
28662868
override predicate hasUnknownTypeAt(string derefChainBorrow, FunctionPosition pos, TypePath path) {
28672869
exists(ImplOrTraitItemNode i |
28682870
this.hasUnknownTypeAt(i, this.getTarget(i, derefChainBorrow), pos, path)
28692871
)
2870-
}
2871-
2872-
/**
2873-
* Holds if the return type of this call at `path` may have to be inferred
2874-
* from the context.
2875-
*/
2876-
pragma[nomagic]
2877-
override predicate hasUnknownTypeAt(FunctionPosition pos, TypePath path) {
2872+
or
2873+
derefChainBorrow = noDerefChainBorrow() and
28782874
forex(ImplOrTraitItemNode i, Function f |
28792875
f = CallExprImpl::getResolvedFunction(this) and
28802876
f = i.getAnAssocItem()
@@ -2884,9 +2880,8 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
28842880
}
28852881
}
28862882

2887-
private class NonAssocCallExprAccess extends Access, CallExpr, ContextTyping::ContextTypedCallCand
2888-
{
2889-
NonAssocCallExprAccess() {
2883+
private class NonAssocFunctionCallAccess extends Access, CallExpr {
2884+
NonAssocFunctionCallAccess() {
28902885
forex(ItemNode i | i = CallExprImpl::getResolvedFunction(this) |
28912886
not i instanceof AssocFunction
28922887
)
@@ -2897,18 +2892,18 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
28972892
result = getCallExprTypeArgument(this, apos, path)
28982893
}
28992894

2900-
override AstNode getNodeAt(FunctionPosition pos) {
2901-
result = this.getSyntacticArgument(pos.asArgumentPosition())
2895+
override AstNode getNodeAt(FunctionPosition posAdj) {
2896+
result = this.getSyntacticArgument(posAdj.asArgumentPosition())
29022897
or
2903-
result = this and pos.isReturn()
2898+
result = this and posAdj.isReturn()
29042899
}
29052900

29062901
pragma[nomagic]
2907-
private Type getInferredType(AccessPosition apos, TypePath path) {
2908-
apos.isTypeQualifier() and
2902+
private Type getInferredType(FunctionPosition posAdj, TypePath path) {
2903+
posAdj.isTypeQualifier() and
29092904
result = getCallExprTypeQualifier(this, path, false)
29102905
or
2911-
result = inferType(this.getNodeAt(apos), path)
2906+
result = inferType(this.getNodeAt(posAdj), path)
29122907
}
29132908

29142909
bindingset[derefChainBorrow]
@@ -2918,37 +2913,22 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
29182913
}
29192914

29202915
pragma[nomagic]
2921-
private FunctionDeclaration resolveCallTargetViaPathResolution() {
2922-
result = CallExprImpl::getResolvedFunction(this)
2923-
}
2924-
29252916
private Declaration getTarget() {
29262917
exists(ImplOrTraitItemNodeOption i, FunctionDeclaration f |
2927-
f = this.resolveCallTargetViaPathResolution() and
2918+
f = CallExprImpl::getResolvedFunction(this) and
29282919
f.isDirectlyFor(i) and
29292920
result = TFunctionDeclaration(i, f)
29302921
)
29312922
}
29322923

29332924
override Declaration getTarget(string derefChainBorrow) {
2934-
exists(DerefChain derefChain, BorrowKind borrow |
2935-
result = this.getTarget() and
2936-
derefChain.isEmpty() and
2937-
borrow.isNoBorrow() and
2938-
derefChainBorrow = encodeDerefChainBorrow(derefChain, borrow)
2939-
)
2940-
}
2941-
2942-
override predicate hasUnknownTypeAt(string derefChainBorrow, FunctionPosition pos, TypePath path) {
2943-
none()
2925+
result = this.getTarget() and
2926+
derefChainBorrow = noDerefChainBorrow()
29442927
}
29452928

2946-
/**
2947-
* Holds if the return type of this call at `path` may have to be inferred
2948-
* from the context.
2949-
*/
29502929
pragma[nomagic]
2951-
override predicate hasUnknownTypeAt(FunctionPosition pos, TypePath path) {
2930+
override predicate hasUnknownTypeAt(string derefChainBorrow, FunctionPosition pos, TypePath path) {
2931+
derefChainBorrow = noDerefChainBorrow() and
29522932
exists(ImplOrTraitItemNodeOption i, FunctionDeclaration f |
29532933
TFunctionDeclaration(i, f) = this.getTarget() and
29542934
this.hasUnknownTypeAt(i.asSome(), f, pos, path)
@@ -2966,20 +2946,13 @@ private Type inferFunctionCallType0(
29662946
) {
29672947
exists(TypePath path0 |
29682948
n = a.getNodeAt(posAdj) and
2969-
(
2970-
exists(string derefChainBorrow |
2971-
FunctionCallMatchingInput::decodeDerefChainBorrow(derefChainBorrow, derefChain, borrow)
2972-
|
2973-
result = FunctionCallMatching::inferAccessType(a, derefChainBorrow, posAdj, path0)
2974-
or
2975-
a.hasUnknownTypeAt(derefChainBorrow, posAdj, path0) and
2976-
result = TUnknownType()
2977-
)
2949+
exists(string derefChainBorrow |
2950+
FunctionCallMatchingInput::decodeDerefChainBorrow(derefChainBorrow, derefChain, borrow)
2951+
|
2952+
result = FunctionCallMatching::inferAccessType(a, derefChainBorrow, posAdj, path0)
29782953
or
2979-
a.hasUnknownTypeAt(posAdj, path0) and
2980-
result = TUnknownType() and
2981-
derefChain.isEmpty() and
2982-
borrow.isNoBorrow()
2954+
a.hasUnknownTypeAt(derefChainBorrow, posAdj, path0) and
2955+
result = TUnknownType()
29832956
)
29842957
|
29852958
if
@@ -3012,7 +2985,7 @@ private Type inferFunctionCallTypeSelf(
30122985
MethodCall mc, AstNode n, DerefChain derefChain, TypePath path
30132986
) {
30142987
exists(FunctionPosition posAdj, BorrowKind borrow, TypePath path0 |
3015-
result = inferFunctionCallType0(mc, posAdj, n, derefChain, borrow, path0) //and
2988+
result = inferFunctionCallType0(mc, posAdj, n, derefChain, borrow, path0)
30162989
|
30172990
posAdj.asPosition() = 0 and
30182991
borrow.isNoBorrow() and
@@ -3059,8 +3032,8 @@ private Type inferFunctionCallTypePreCheck(AstNode n, FunctionPosition pos, Type
30593032
}
30603033

30613034
/**
3062-
* Gets the type of `n` at `path`, where `n` is either a method call or an
3063-
* argument/receiver of a method call.
3035+
* Gets the type of `n` at `path`, where `n` is either a function call or an
3036+
* argument/receiver of a function call.
30643037
*/
30653038
private predicate inferFunctionCallType =
30663039
ContextTyping::CheckContextTyping<inferFunctionCallTypePreCheck/3>::check/2;
@@ -3114,11 +3087,8 @@ private class TupleLikeVariant extends TupleLikeConstructor instanceof Variant {
31143087
}
31153088

31163089
/**
3117-
* A matching configuration for resolving types of calls like
3118-
* `foo::bar(baz)` where the target is not a method.
3119-
*
3120-
* This also includes "calls" to tuple variants and tuple structs such
3121-
* as `Result::Ok(42)`.
3090+
* A matching configuration for resolving types of tuple variants and tuple structs
3091+
* such as `Result::Ok(42)`.
31223092
*/
31233093
private module TupleLikeConstructionMatchingInput implements MatchingInputSig {
31243094
import FunctionPositionMatchingInput

0 commit comments

Comments
 (0)