Skip to content

Commit aefd835

Browse files
committed
wip12
1 parent 0365a0c commit aefd835

2 files changed

Lines changed: 26 additions & 57 deletions

File tree

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

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -394,13 +394,7 @@ private module Input3 implements InputSig3 {
394394
}
395395
}
396396

397-
class Call extends Expr instanceof FunctionCallMatchingInput::Access {
398-
Type getTypeArgument(TypeArgumentPosition apos, TypePath path) {
399-
result = super.getTypeArgument(apos, path)
400-
}
401-
402-
AstNode getNodeAt(TypePosition pos) { result = super.getNodeAt(pos) }
403-
397+
class Call extends FunctionCallMatchingInput::Access {
404398
/** Gets the target of this call. */
405399
Callable getTargetCertain() {
406400
exists(ImplOrTraitItemNodeOption i, FunctionDeclaration f, Path p |
@@ -617,14 +611,22 @@ private module Input3 implements InputSig3 {
617611
result = inferTypeFromAnnotationTopDown(n, path)
618612
or
619613
result = inferClosureExprBodyTypeTopDown(n, path)
614+
or
615+
exists(FunctionPosition pos | not pos.isReturn() |
616+
result = inferConstructionType(n, pos, path)
617+
or
618+
result = inferOperationType(n, pos, path)
619+
)
620620
}
621621

622622
Type inferTypeSpecific(AstNode n, TypePath path) {
623623
result = inferAssignmentOperationType(n, path)
624624
or
625-
result = inferConstructionType(n, path)
626-
or
627-
result = inferOperationType(n, path)
625+
exists(FunctionPosition pos | pos.isReturn() |
626+
result = inferConstructionType(n, pos, path)
627+
or
628+
result = inferOperationType(n, pos, path)
629+
)
628630
or
629631
result = inferFieldExprType(n, path)
630632
or
@@ -1082,7 +1084,7 @@ private module ContextTyping {
10821084
* context in which the call appears, for example a call like
10831085
* `Default::default()`.
10841086
*/
1085-
abstract class ContextTypedCallCand extends AstNode {
1087+
abstract class ContextTypedCallCand extends Expr {
10861088
abstract Type getTypeArgument(TypeArgumentPosition apos, TypePath path);
10871089

10881090
predicate hasTypeArgument(TypeArgumentPosition apos) { exists(this.getTypeArgument(apos, _)) }
@@ -2653,7 +2655,9 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
26532655
)
26542656
}
26552657

2656-
abstract class Access extends ContextTyping::ContextTypedCallCand {
2658+
final class Access = AccessImpl;
2659+
2660+
abstract private class AccessImpl extends ContextTyping::ContextTypedCallCand {
26572661
abstract AstNode getNodeAt(FunctionPosition pos);
26582662

26592663
bindingset[derefChainBorrow]
@@ -2668,7 +2672,7 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
26682672
abstract predicate hasUnknownTypeAt(string derefChainBorrow, FunctionPosition pos, TypePath path);
26692673
}
26702674

2671-
private class AssocFunctionCallAccess extends Access instanceof AssocFunctionResolution::AssocFunctionCall
2675+
private class AssocFunctionCallAccess extends AccessImpl instanceof AssocFunctionResolution::AssocFunctionCall
26722676
{
26732677
AssocFunctionCallAccess() {
26742678
// handled in the `OperationMatchingInput` module
@@ -2755,7 +2759,7 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
27552759
}
27562760
}
27572761

2758-
private class NonAssocFunctionCallAccess extends Access instanceof NonAssocCallExpr,
2762+
private class NonAssocFunctionCallAccess extends AccessImpl instanceof NonAssocCallExpr,
27592763
CallExprImpl::CallExprCall
27602764
{
27612765
pragma[nomagic]
@@ -3024,16 +3028,13 @@ private module ConstructionMatchingInput implements MatchingInputSig {
30243028
private module ConstructionMatching = Matching<ConstructionMatchingInput>;
30253029

30263030
pragma[nomagic]
3027-
private Type inferConstructionTypePreCheck(AstNode n, FunctionPosition pos, TypePath path) {
3031+
private Type inferConstructionType(AstNode n, FunctionPosition pos, TypePath path) {
30283032
exists(ConstructionMatchingInput::Access a |
30293033
n = a.getNodeAt(pos) and
30303034
result = ConstructionMatching::inferAccessType(a, pos, path)
30313035
)
30323036
}
30333037

3034-
private predicate inferConstructionType =
3035-
CheckContextTyping<inferConstructionTypePreCheck/3>::check/2;
3036-
30373038
pragma[nomagic]
30383039
private Type inferUnknownType(AstNode n, TypePath path) {
30393040
result = TUnknownType() and
@@ -3119,16 +3120,14 @@ private module OperationMatchingInput implements MatchingInputSig {
31193120
private module OperationMatching = Matching<OperationMatchingInput>;
31203121

31213122
pragma[nomagic]
3122-
private Type inferOperationTypePreCheck(AstNode n, FunctionPosition pos, TypePath path) {
3123+
private Type inferOperationType(AstNode n, FunctionPosition pos, TypePath path) {
31233124
exists(OperationMatchingInput::Access a |
31243125
n = a.getNodeAt(pos) and
31253126
result = OperationMatching::inferAccessType(a, pos, path) and
31263127
if pos.asPosition() = 0 then not path.isEmpty() else any()
31273128
)
31283129
}
31293130

3130-
private predicate inferOperationType = CheckContextTyping<inferOperationTypePreCheck/3>::check/2;
3131-
31323131
pragma[nomagic]
31333132
private Type getFieldExprLookupType(FieldExpr fe, string name, DerefChain derefChain) {
31343133
exists(TypePath path |
@@ -3741,11 +3740,10 @@ private module Debug {
37413740
// n = getRelevantLocatable() and
37423741
// t = inferFunctionCallType(n, path)
37433742
// }
3744-
predicate debugInferConstructionType(AstNode n, TypePath path, Type t) {
3745-
n = getRelevantLocatable() and
3746-
t = inferConstructionType(n, path)
3747-
}
3748-
3743+
// predicate debugInferConstructionType(AstNode n, TypePath path, Type t) {
3744+
// n = getRelevantLocatable() and
3745+
// t = inferConstructionType(n, path)
3746+
// }
37493747
predicate debugTypeMention(TypeMention tm, TypePath path, Type type) {
37503748
tm = getRelevantLocatable() and
37513749
tm.getTypeAt(path) = type

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

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2746,7 +2746,8 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
27462746
Call call, CallResolutionContext ctx, TypePosition pos, AstNode n, TypePath path
27472747
) {
27482748
result = inferCallTypeOut(call, ctx, pos, n, path) and
2749-
not pos.isReturn()
2749+
not pos.isReturn() and
2750+
hasUnknownType(n)
27502751
}
27512752

27522753
pragma[nomagic]
@@ -2782,36 +2783,6 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
27822783
}
27832784
}
27842785

2785-
signature Type inferCallTypeSig(AstNode n, TypePosition pos, TypePath path);
2786-
2787-
/**
2788-
* Given a predicate `inferCallType` for inferring the type of a call at a given
2789-
* position, this module exposes the predicate `check`, which wraps the input
2790-
* predicate and checks that types are only propagated into arguments when they
2791-
* are context-typed.
2792-
*/
2793-
module CheckContextTyping<inferCallTypeSig/3 inferCallType> {
2794-
pragma[nomagic]
2795-
private Type inferCallNonReturnType(AstNode n, TypePath prefix, TypePath path) {
2796-
exists(TypePosition pos |
2797-
result = inferCallType(n, pos, path) and
2798-
hasUnknownType(n) and
2799-
not pos.isReturn() and
2800-
prefix = path.getAPrefix()
2801-
)
2802-
}
2803-
2804-
pragma[nomagic]
2805-
Type check(AstNode n, TypePath path) {
2806-
result = inferCallType(n, any(TypePosition pos | pos.isReturn()), path)
2807-
or
2808-
exists(TypePath prefix |
2809-
result = inferCallNonReturnType(n, prefix, path) and
2810-
hasUnknownTypeAt(n, prefix)
2811-
)
2812-
}
2813-
}
2814-
28152786
/**
28162787
* Gets the inferred root type of `n`, if any.
28172788
*/

0 commit comments

Comments
 (0)