Skip to content

Commit 04100d4

Browse files
committed
wip12
1 parent 362047f commit 04100d4

2 files changed

Lines changed: 97 additions & 86 deletions

File tree

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

Lines changed: 56 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -428,14 +428,6 @@ private module Input3 implements InputSig3 {
428428
.getAdditionalTypeBound(this.getFunction(), _)
429429
.getTypeRepr()
430430
}
431-
432-
Type getParameterType(int j, TypePath path) {
433-
exists(FunctionPosition pos | j = pos.asPosition() | result = this.getDeclaredType(pos, path))
434-
}
435-
436-
Type getReturnType(TypePath path) {
437-
exists(FunctionPosition pos | pos.isReturn() | result = this.getDeclaredType(pos, path))
438-
}
439431
}
440432

441433
class Call extends FunctionCallMatchingInput::Access {
@@ -450,21 +442,19 @@ private module Input3 implements InputSig3 {
450442
}
451443

452444
bindingset[derefChainBorrow]
453-
Type inferCallArgumentType(Call call, string derefChainBorrow, int i, TypePath path) {
454-
exists(FunctionPosition pos |
455-
i = pos.asPosition() and
456-
result = call.(FunctionCallMatchingInput::Access).getInferredType(derefChainBorrow, pos, path)
457-
)
445+
Type inferCallArgumentType(Call call, string derefChainBorrow, int pos, TypePath path) {
446+
result =
447+
call.(FunctionCallMatchingInput::Access).getInferredArgumentType(derefChainBorrow, pos, path)
458448
}
459449

460-
Type inferCallArgumentType(AstNode n, TypePath path) {
450+
Type inferCallArgumentTypeContextual(AstNode n, TypePath path) {
461451
exists(FunctionCallMatchingInput::Access call, FunctionPosition pos |
462-
result = inferCallArgumentType(call, pos.asPosition(), n, _, _, path) and
452+
result = inferCallArgumentTypeContextual(call, pos.asPosition(), n, _, _, path) and
463453
not call.(AssocFunctionResolution::AssocFunctionCall).hasReceiverAtPos(pos)
464454
)
465455
or
466456
exists(FunctionCallMatchingInput::Access a |
467-
result = inferFunctionCallSelfArgumentType(a, n, DerefChain::nil(), path) and
457+
result = inferFunctionCallSelfArgumentTypeContextual(a, n, DerefChain::nil(), path) and
468458
if a.(AssocFunctionResolution::AssocFunctionCall).hasReceiver()
469459
then not path.isEmpty()
470460
else any()
@@ -757,14 +747,13 @@ private class FunctionDeclaration extends Function {
757747
}
758748

759749
pragma[nomagic]
760-
Type getParameterType(ImplOrTraitItemNodeOption i, FunctionPosition pos, TypePath path) {
750+
Type getParameterType(ImplOrTraitItemNodeOption i, int pos, TypePath path) {
761751
i = parent and
762-
(
763-
not pos.isReturn() and
764-
result = getAssocFunctionTypeAt(this, i.asSome(), pos, path)
752+
exists(FunctionPosition fpos | pos = fpos.asPosition() |
753+
result = getAssocFunctionTypeAt(this, i.asSome(), fpos, path)
765754
or
766755
i.isNone() and
767-
result = this.getParam(pos.asPosition()).getTypeRepr().(TypeMention).getTypeAt(path)
756+
result = this.getParam(pos).getTypeRepr().(TypeMention).getTypeAt(path)
768757
)
769758
}
770759

@@ -1039,17 +1028,14 @@ private class NonAssocCallExpr extends CallExpr {
10391028

10401029
pragma[nomagic]
10411030
Type getInferredType(FunctionPosition pos, TypePath path) {
1042-
pos.isTypeQualifier() and
1043-
result = getCallExprTypeQualifier(this, path, false)
1044-
or
10451031
result = inferType(this.getNodeAt(pos), path)
10461032
}
10471033
}
10481034

10491035
/**
1050-
* Provides functionality related to context-based typing of calls.
1036+
* Provides functionality related to contextual typing of calls.
10511037
*/
1052-
private module ContextTyping {
1038+
private module ContextualTyping {
10531039
/**
10541040
* Holds if `f` mentions type parameter `tp` at some non-return position,
10551041
* possibly via a constraint on another mentioned type parameter.
@@ -1094,7 +1080,7 @@ private module ContextTyping {
10941080
* context in which the call appears, for example a call like
10951081
* `Default::default()`.
10961082
*/
1097-
abstract class ContextTypedCallCand extends Expr {
1083+
abstract class ContextuallyTypedCallCand extends Expr {
10981084
abstract Type getTypeArgument(TypeArgumentPosition apos, TypePath path);
10991085

11001086
predicate hasTypeArgument(TypeArgumentPosition apos) {
@@ -2584,12 +2570,12 @@ private module AssocFunctionResolution {
25842570
}
25852571

25862572
/**
2573+
* TODO: rename/remove
2574+
*
25872575
* A matching configuration for resolving types of function call expressions
25882576
* like `foo.bar(baz)` and `Foo::bar(baz)`.
25892577
*/
2590-
private module FunctionCallMatchingInput implements MatchingWithEnvironmentInputSig {
2591-
import FunctionPositionMatchingInput
2592-
2578+
private module FunctionCallMatchingInput {
25932579
private newtype TDeclaration =
25942580
TFunctionDeclaration(ImplOrTraitItemNodeOption i, FunctionDeclaration f, boolean forIndexExpr) {
25952581
f.isFor(i) and
@@ -2632,12 +2618,11 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
26322618
result = f.getTypeParameter(i, ppos)
26332619
}
26342620

2635-
Type getDeclaredType(FunctionPosition pos, TypePath path) {
2636-
result = f.getParameterType(i, pos, path)
2637-
or
2621+
Type getParameterType(int j, TypePath path) { result = f.getParameterType(i, j, path) }
2622+
2623+
Type getReturnType(TypePath path) {
26382624
// index expression `x[i]` desugars to `*x.index(i)`, so we must account for
26392625
// the implicit deref
2640-
pos.isReturn() and
26412626
exists(TypePath path0 | result = f.getReturnType(i, path0) |
26422627
forIndexExpr = true and
26432628
path0.isCons(getRefTypeParameter(_), path)
@@ -2681,9 +2666,7 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
26812666
}
26822667

26832668
bindingset[derefChainBorrow]
2684-
additional predicate decodeDerefChainBorrow(
2685-
string derefChainBorrow, DerefChain derefChain, BorrowKind borrow
2686-
) {
2669+
predicate decodeDerefChainBorrow(string derefChainBorrow, DerefChain derefChain, BorrowKind borrow) {
26872670
exists(int i |
26882671
i = derefChainBorrow.indexOf(";") and
26892672
derefChain = derefChainBorrow.prefix(i) and
@@ -2701,11 +2684,11 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
27012684

27022685
final class Access = AccessImpl;
27032686

2704-
abstract private class AccessImpl extends ContextTyping::ContextTypedCallCand {
2687+
abstract private class AccessImpl extends ContextualTyping::ContextuallyTypedCallCand {
27052688
abstract AstNode getNodeAt(FunctionPosition pos);
27062689

27072690
bindingset[derefChainBorrow]
2708-
abstract Type getInferredType(string derefChainBorrow, FunctionPosition pos, TypePath path);
2691+
abstract Type getInferredArgumentType(string derefChainBorrow, int pos, TypePath path);
27092692

27102693
abstract Declaration getTarget(string derefChainBorrow);
27112694

@@ -2740,22 +2723,26 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
27402723
}
27412724

27422725
pragma[nomagic]
2743-
private Type getInferredSelfType(FunctionPosition pos, string derefChainBorrow, TypePath path) {
2744-
exists(DerefChain derefChain, BorrowKind borrow |
2745-
result = super.getSelfTypeAt(pos, derefChain, borrow, path) and
2726+
private Type getInferredSelfType(int pos, string derefChainBorrow, TypePath path) {
2727+
exists(FunctionPosition fpos, DerefChain derefChain, BorrowKind borrow |
2728+
result = super.getSelfTypeAt(fpos, derefChain, borrow, path) and
27462729
derefChainBorrow = encodeDerefChainBorrow(derefChain, borrow) and
2747-
super.hasReceiverAtPos(pos)
2730+
super.hasReceiverAtPos(fpos) and
2731+
pos = fpos.asPosition()
27482732
)
27492733
}
27502734

27512735
pragma[nomagic]
2752-
private Type getInferredNonSelfType(FunctionPosition pos, TypePath path) {
2753-
not super.hasReceiverAtPos(pos) and
2754-
result = super.getTypeAt(pos, path)
2736+
private Type getInferredNonSelfType(int pos, TypePath path) {
2737+
exists(FunctionPosition fpos |
2738+
not super.hasReceiverAtPos(fpos) and
2739+
result = super.getTypeAt(fpos, path) and
2740+
pos = fpos.asPosition()
2741+
)
27552742
}
27562743

27572744
bindingset[derefChainBorrow]
2758-
override Type getInferredType(string derefChainBorrow, FunctionPosition pos, TypePath path) {
2745+
override Type getInferredArgumentType(string derefChainBorrow, int pos, TypePath path) {
27592746
result = this.getInferredSelfType(pos, derefChainBorrow, path)
27602747
or
27612748
result = this.getInferredNonSelfType(pos, path)
@@ -2811,14 +2798,17 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
28112798
}
28122799

28132800
pragma[nomagic]
2814-
private Type getInferredType(FunctionPosition pos, TypePath path) {
2815-
result = super.getInferredType(pos, path)
2801+
private Type getInferredArgumentType(int pos, TypePath path) {
2802+
exists(FunctionPosition fpos |
2803+
result = super.getInferredType(fpos, path) and
2804+
pos = fpos.asPosition()
2805+
)
28162806
}
28172807

28182808
bindingset[derefChainBorrow]
2819-
override Type getInferredType(string derefChainBorrow, FunctionPosition pos, TypePath path) {
2809+
override Type getInferredArgumentType(string derefChainBorrow, int pos, TypePath path) {
28202810
exists(derefChainBorrow) and
2821-
result = this.getInferredType(pos, path)
2811+
result = this.getInferredArgumentType(pos, path)
28222812
}
28232813

28242814
pragma[nomagic]
@@ -2852,13 +2842,13 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
28522842
}
28532843

28542844
pragma[nomagic]
2855-
private Type inferCallArgumentType(
2845+
private Type inferCallArgumentTypeContextual(
28562846
FunctionCallMatchingInput::Access call, int pos, AstNode n, DerefChain derefChain,
28572847
BorrowKind borrow, TypePath path
28582848
) {
28592849
exists(string derefChainBorrow |
28602850
FunctionCallMatchingInput::decodeDerefChainBorrow(derefChainBorrow, derefChain, borrow) and
2861-
result = M3::inferCallArgumentType(call, derefChainBorrow, pos, n, path)
2851+
result = M3::inferCallArgumentTypeContextual(call, derefChainBorrow, pos, n, path)
28622852
)
28632853
}
28642854

@@ -2870,12 +2860,12 @@ private Type inferCallArgumentType(
28702860
* empty, at which point the inferred type can be applied back to `n`.
28712861
*/
28722862
pragma[nomagic]
2873-
private Type inferFunctionCallSelfArgumentType(
2863+
private Type inferFunctionCallSelfArgumentTypeContextual(
28742864
FunctionCallMatchingInput::Access call, AstNode n, DerefChain derefChain, TypePath path
28752865
) {
28762866
exists(FunctionPosition pos, BorrowKind borrow, TypePath path0 |
28772867
call.(AssocFunctionResolution::AssocFunctionCall).hasReceiverAtPos(pos) and
2878-
result = inferCallArgumentType(call, pos.asPosition(), n, derefChain, borrow, path0)
2868+
result = inferCallArgumentTypeContextual(call, pos.asPosition(), n, derefChain, borrow, path0)
28792869
|
28802870
borrow.isNoBorrow() and
28812871
path = path0
@@ -2892,7 +2882,7 @@ private Type inferFunctionCallSelfArgumentType(
28922882
DerefChain derefChain0, Type t0, TypePath path0, DerefImplItemNode impl, Type selfParamType,
28932883
TypePath selfPath
28942884
|
2895-
t0 = inferFunctionCallSelfArgumentType(call, n, derefChain0, path0) and
2885+
t0 = inferFunctionCallSelfArgumentTypeContextual(call, n, derefChain0, path0) and
28962886
derefChain0.isCons(impl, derefChain) and
28972887
selfParamType = impl.resolveSelfTypeAt(selfPath)
28982888
|
@@ -2919,6 +2909,10 @@ abstract private class Constructor extends Addressable {
29192909

29202910
abstract TypeRepr getParameterTypeRepr(int pos);
29212911

2912+
Type getParameterType(int pos, TypePath path) {
2913+
result = this.getParameterTypeRepr(pos).(TypeMention).getTypeAt(path)
2914+
}
2915+
29222916
Type getReturnType(TypePath path) {
29232917
result = TDataType(this.getTypeItem()) and
29242918
path.isEmpty()
@@ -2933,10 +2927,6 @@ abstract private class Constructor extends Addressable {
29332927
pos.isReturn() and
29342928
result = this.getReturnType(path)
29352929
}
2936-
2937-
Type getParameterType(int pos, TypePath path) {
2938-
result = this.getParameterTypeRepr(pos).(TypeMention).getTypeAt(path)
2939-
}
29402930
}
29412931

29422932
private class StructConstructor extends Constructor instanceof Struct {
@@ -2995,7 +2985,7 @@ private module ConstructionMatchingInput implements MatchingInputSig {
29952985
}
29962986

29972987
private class NonAssocCallAccess extends Access, NonAssocCallExpr,
2998-
ContextTyping::ContextTypedCallCand
2988+
ContextualTyping::ContextuallyTypedCallCand
29992989
{
30002990
NonAssocCallAccess() {
30012991
this instanceof CallExprImpl::TupleStructExpr or
@@ -3130,7 +3120,11 @@ private module OperationMatchingInput implements MatchingInputSig {
31303120

31313121
Type getDeclaredType(FunctionPosition pos, TypePath path) {
31323122
exists(TypePath path0 |
3133-
result = super.getDeclaredType(pos, path0) and
3123+
result = super.getParameterType(pos.asPosition(), path0)
3124+
or
3125+
pos.isReturn() and
3126+
result = super.getReturnType(path0)
3127+
|
31343128
if
31353129
this.borrowsAt(pos)
31363130
or

0 commit comments

Comments
 (0)