Skip to content

Commit e5ee765

Browse files
committed
wip
1 parent b70da1e commit e5ee765

File tree

1 file changed

+86
-65
lines changed

1 file changed

+86
-65
lines changed

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

Lines changed: 86 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2844,15 +2844,15 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
28442844
result = this.getInferredNonSelfType(posAdj, path)
28452845
}
28462846

2847-
Method getTarget(ImplOrTraitItemNode i, string derefChainBorrow) {
2847+
private AssocFunction getTarget(ImplOrTraitItemNode i, string derefChainBorrow) {
28482848
exists(DerefChain derefChain, BorrowKind borrow |
28492849
derefChainBorrow = encodeDerefChainBorrow(derefChain, borrow) and
28502850
result = super.resolveCallTarget(i, derefChain, borrow) // mutual recursion; resolving method calls requires resolving types and vice versa
28512851
)
28522852
}
28532853

28542854
override Declaration getTarget(string derefChainBorrow) {
2855-
exists(ImplOrTraitItemNodeOption i, Function f |
2855+
exists(ImplOrTraitItemNodeOption i, AssocFunction f |
28562856
f = this.getTarget(i.asSome(), derefChainBorrow) and
28572857
result = TFunctionDeclaration(i, f)
28582858
)
@@ -2883,6 +2883,78 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
28832883
)
28842884
}
28852885
}
2886+
2887+
private class NonAssocCallExprAccess extends Access, CallExpr, ContextTyping::ContextTypedCallCand
2888+
{
2889+
NonAssocCallExprAccess() {
2890+
forex(ItemNode i | i = CallExprImpl::getResolvedFunction(this) |
2891+
not i instanceof AssocFunction
2892+
)
2893+
}
2894+
2895+
pragma[nomagic]
2896+
override Type getTypeArgument(TypeArgumentPosition apos, TypePath path) {
2897+
result = getCallExprTypeArgument(this, apos, path)
2898+
}
2899+
2900+
override AstNode getNodeAt(FunctionPosition pos) {
2901+
result = this.getSyntacticArgument(pos.asArgumentPosition())
2902+
or
2903+
result = this and pos.isReturn()
2904+
}
2905+
2906+
pragma[nomagic]
2907+
private Type getInferredType(AccessPosition apos, TypePath path) {
2908+
apos.isTypeQualifier() and
2909+
result = getCallExprTypeQualifier(this, path, false)
2910+
or
2911+
result = inferType(this.getNodeAt(apos), path)
2912+
}
2913+
2914+
bindingset[derefChainBorrow]
2915+
override Type getInferredType(string derefChainBorrow, FunctionPosition posAdj, TypePath path) {
2916+
exists(derefChainBorrow) and
2917+
result = this.getInferredType(posAdj, path)
2918+
}
2919+
2920+
pragma[nomagic]
2921+
private FunctionDeclaration resolveCallTargetViaPathResolution() {
2922+
result = CallExprImpl::getResolvedFunction(this)
2923+
}
2924+
2925+
private Declaration getTarget() {
2926+
exists(ImplOrTraitItemNodeOption i, FunctionDeclaration f |
2927+
f = this.resolveCallTargetViaPathResolution() and
2928+
f.isDirectlyFor(i) and
2929+
result = TFunctionDeclaration(i, f)
2930+
)
2931+
}
2932+
2933+
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()
2944+
}
2945+
2946+
/**
2947+
* Holds if the return type of this call at `path` may have to be inferred
2948+
* from the context.
2949+
*/
2950+
pragma[nomagic]
2951+
override predicate hasUnknownTypeAt(FunctionPosition pos, TypePath path) {
2952+
exists(ImplOrTraitItemNodeOption i, FunctionDeclaration f |
2953+
TFunctionDeclaration(i, f) = this.getTarget() and
2954+
this.hasUnknownTypeAt(i.asSome(), f, pos, path)
2955+
)
2956+
}
2957+
}
28862958
}
28872959

28882960
private module FunctionCallMatching = MatchingWithEnvironment<FunctionCallMatchingInput>;
@@ -2984,12 +3056,6 @@ private Type inferFunctionCallTypePreCheck(AstNode n, FunctionPosition pos, Type
29843056
then pos.isSelf()
29853057
else pos.asPosition() = 0
29863058
)
2987-
// result = inferFunctionCallTypeNonSelf(n, pos, path)
2988-
// or
2989-
// exists(MethodCall mc |
2990-
// result = inferMethodCallTypeSelf(mc, n, DerefChain::nil(), path) and
2991-
// if mc instanceof CallExpr then pos.asPosition() = 0 else pos.isSelf()
2992-
// )
29933059
}
29943060

29953061
/**
@@ -3054,16 +3120,10 @@ private class TupleLikeVariant extends TupleLikeConstructor instanceof Variant {
30543120
* This also includes "calls" to tuple variants and tuple structs such
30553121
* as `Result::Ok(42)`.
30563122
*/
3057-
private module NonMethodCallMatchingInput implements MatchingInputSig {
3123+
private module TupleLikeConstructionMatchingInput implements MatchingInputSig {
30583124
import FunctionPositionMatchingInput
30593125

3060-
private class NonMethodFunctionDeclaration extends NonMethodFunction, FunctionDeclaration { }
3061-
3062-
private newtype TDeclaration =
3063-
TNonMethodFunctionDeclaration(ImplOrTraitItemNodeOption i, NonMethodFunctionDeclaration f) {
3064-
f.isFor(i)
3065-
} or
3066-
TTupleLikeConstructorDeclaration(TupleLikeConstructor tc)
3126+
private newtype TDeclaration = TTupleLikeConstructorDeclaration(TupleLikeConstructor tc)
30673127

30683128
abstract class Declaration extends TDeclaration {
30693129
abstract TypeParameter getTypeParameter(TypeParameterPosition ppos);
@@ -3085,31 +3145,6 @@ private module NonMethodCallMatchingInput implements MatchingInputSig {
30853145
abstract Location getLocation();
30863146
}
30873147

3088-
private class NonMethodFunctionDecl extends Declaration, TNonMethodFunctionDeclaration {
3089-
private ImplOrTraitItemNodeOption i;
3090-
private NonMethodFunctionDeclaration f;
3091-
3092-
NonMethodFunctionDecl() { this = TNonMethodFunctionDeclaration(i, f) }
3093-
3094-
override TypeParameter getTypeParameter(TypeParameterPosition ppos) {
3095-
result = f.getTypeParameter(i, ppos)
3096-
}
3097-
3098-
override Type getParameterType(DeclarationPosition dpos, TypePath path) {
3099-
result = f.getParameterType(i, dpos, path)
3100-
}
3101-
3102-
override Type getReturnType(TypePath path) { result = f.getReturnType(i, path) }
3103-
3104-
override string toString() {
3105-
i.isNone() and result = f.toString()
3106-
or
3107-
result = f.toStringExt(i.asSome())
3108-
}
3109-
3110-
override Location getLocation() { result = f.getLocation() }
3111-
}
3112-
31133148
private class TupleLikeConstructorDeclaration extends Declaration,
31143149
TTupleLikeConstructorDeclaration
31153150
{
@@ -3164,15 +3199,6 @@ private module NonMethodCallMatchingInput implements MatchingInputSig {
31643199

31653200
pragma[inline]
31663201
Declaration getTarget() {
3167-
exists(ImplOrTraitItemNodeOption i, NonMethodFunctionDeclaration f |
3168-
result = TNonMethodFunctionDeclaration(i, f)
3169-
|
3170-
f = this.(AssocFunctionResolution::AssocFunctionCall).resolveCallTarget(i.asSome(), _, _) // mutual recursion; resolving some associated function calls requires resolving types
3171-
or
3172-
f = this.resolveCallTargetViaPathResolution() and
3173-
f.isDirectlyFor(i)
3174-
)
3175-
or
31763202
exists(ItemNode i | i = this.resolveCallTargetViaPathResolution() |
31773203
result = TTupleLikeConstructorDeclaration(i)
31783204
)
@@ -3184,11 +3210,6 @@ private module NonMethodCallMatchingInput implements MatchingInputSig {
31843210
*/
31853211
pragma[nomagic]
31863212
predicate hasUnknownTypeAt(FunctionPosition pos, TypePath path) {
3187-
exists(ImplOrTraitItemNodeOption i, NonMethodFunctionDeclaration f |
3188-
TNonMethodFunctionDeclaration(i, f) = this.getTarget() and
3189-
this.hasUnknownTypeAt(i.asSome(), f, pos, path)
3190-
)
3191-
or
31923213
// Tuple declarations, such as `Result::Ok(...)`, may also be context typed
31933214
exists(TupleLikeConstructor tc, TypeParameter tp |
31943215
tc = this.resolveCallTargetViaPathResolution() and
@@ -3205,20 +3226,20 @@ private module NonMethodCallMatchingInput implements MatchingInputSig {
32053226
}
32063227
}
32073228

3208-
private module NonMethodCallMatching = Matching<NonMethodCallMatchingInput>;
3229+
private module TupleLikeConstructionMatching = Matching<TupleLikeConstructionMatchingInput>;
32093230

32103231
pragma[nomagic]
3211-
private Type inferNonMethodCallType0(AstNode n, FunctionPosition pos, TypePath path) {
3212-
exists(NonMethodCallMatchingInput::Access a | n = a.getNodeAt(pos) |
3213-
result = NonMethodCallMatching::inferAccessType(a, pos, path)
3232+
private Type inferTupleLikeConstructionType0(AstNode n, FunctionPosition pos, TypePath path) {
3233+
exists(TupleLikeConstructionMatchingInput::Access a | n = a.getNodeAt(pos) |
3234+
result = TupleLikeConstructionMatching::inferAccessType(a, pos, path)
32143235
or
32153236
a.hasUnknownTypeAt(pos, path) and
32163237
result = TUnknownType()
32173238
)
32183239
}
32193240

3220-
private predicate inferNonMethodCallType =
3221-
ContextTyping::CheckContextTyping<inferNonMethodCallType0/3>::check/2;
3241+
private predicate inferTupleLikeConstructionType =
3242+
ContextTyping::CheckContextTyping<inferTupleLikeConstructionType0/3>::check/2;
32223243

32233244
/**
32243245
* A matching configuration for resolving types of operations like `a + b`.
@@ -4110,7 +4131,7 @@ private module Cached {
41104131
or
41114132
result = inferFunctionCallType(n, path)
41124133
or
4113-
result = inferNonMethodCallType(n, path)
4134+
result = inferTupleLikeConstructionType(n, path)
41144135
or
41154136
result = inferOperationType(n, path)
41164137
or
@@ -4152,7 +4173,7 @@ private module Debug {
41524173
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
41534174
result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
41544175
filepath.matches("%/main.rs") and
4155-
startline = 2050
4176+
startline = 661
41564177
)
41574178
}
41584179

@@ -4183,9 +4204,9 @@ private module Debug {
41834204
t = inferFunctionCallType(n, path)
41844205
}
41854206

4186-
predicate debugInferNonMethodCallType(AstNode n, TypePath path, Type t) {
4207+
predicate debugInferTupleLikeConstructionType(AstNode n, TypePath path, Type t) {
41874208
n = getRelevantLocatable() and
4188-
t = inferNonMethodCallType(n, path)
4209+
t = inferTupleLikeConstructionType(n, path)
41894210
}
41904211

41914212
predicate debugTypeMention(TypeMention tm, TypePath path, Type type) {

0 commit comments

Comments
 (0)