Skip to content

Commit 23ee720

Browse files
committed
wip3
1 parent 96a5210 commit 23ee720

4 files changed

Lines changed: 129 additions & 122 deletions

File tree

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

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -480,18 +480,18 @@ private module Input3 implements InputSig3 {
480480
)
481481
or
482482
exists(CallExprImpl::DynamicCallExpr dce, TupleType tt, int i |
483-
n1 = dce.getArgList() and
483+
n2 = dce.getArgList() and
484484
tt.getArity() = dce.getNumberOfSyntacticArguments() and
485-
n2 = dce.getSyntacticPositionalArgument(i) and
486-
prefix1 = TypePath::singleton(tt.getPositionalTypeParameter(i)) and
487-
prefix2.isEmpty()
485+
n1 = dce.getSyntacticPositionalArgument(i) and
486+
prefix2 = TypePath::singleton(tt.getPositionalTypeParameter(i)) and
487+
prefix1.isEmpty()
488488
)
489489
or
490490
exists(ClosureExpr ce, int index |
491-
n1 = ce and
492-
n2 = ce.getParam(index).getPat() and
493-
prefix1 = closureParameterPath(ce.getNumberOfParams(), index) and
494-
prefix2.isEmpty()
491+
n2 = ce and
492+
n1 = ce.getParam(index).getPat() and
493+
prefix2 = closureParameterPath(ce.getNumberOfParams(), index) and
494+
prefix1.isEmpty()
495495
)
496496
}
497497

@@ -536,11 +536,11 @@ private module Input3 implements InputSig3 {
536536
prefix1.isEmpty() and
537537
prefix2.isEmpty() and
538538
(
539-
n1 = n2.(OrPat).getAPat()
539+
n2 = n1.(OrPat).getAPat()
540540
or
541-
n1 = n2.(ParenPat).getPat()
541+
n2 = n1.(ParenPat).getPat()
542542
or
543-
n1 = n2.(LiteralPat).getLiteral()
543+
n2 = n1.(LiteralPat).getLiteral()
544544
or
545545
exists(BreakExpr break |
546546
break.getExpr() = n1 and
@@ -560,43 +560,46 @@ private module Input3 implements InputSig3 {
560560
prefix2 = TypePath::singleton(inferRefExprType(re).getPositionalTypeParameter(0))
561561
)
562562
or
563-
n2 =
563+
n1 =
564564
any(RefPat rp |
565-
n1 = rp.getPat() and
566-
prefix1.isEmpty() and
565+
n2 = rp.getPat() and
566+
prefix2.isEmpty() and
567567
exists(boolean isMutable | if rp.isMut() then isMutable = true else isMutable = false |
568-
prefix2 = TypePath::singleton(getRefTypeParameter(isMutable))
568+
prefix1 = TypePath::singleton(getRefTypeParameter(isMutable))
569569
)
570570
)
571571
or
572-
exists(int i, int arity |
573-
prefix1.isEmpty() and
574-
prefix2 = TypePath::singleton(getTupleTypeParameter(arity, i))
572+
exists(int i, int arity, TypePath prefix |
573+
prefix = TypePath::singleton(getTupleTypeParameter(arity, i))
575574
|
576575
arity = n2.(TupleExpr).getNumberOfFields() and
577-
n1 = n2.(TupleExpr).getField(i)
576+
n1 = n2.(TupleExpr).getField(i) and
577+
prefix1.isEmpty() and
578+
prefix2 = prefix
578579
or
579-
arity = n2.(TuplePat).getTupleArity() and
580-
n1 = n2.(TuplePat).getField(i)
580+
arity = n1.(TuplePat).getTupleArity() and
581+
n2 = n1.(TuplePat).getField(i) and
582+
prefix2.isEmpty() and
583+
prefix1 = prefix
581584
)
582585
or
583586
exists(BlockExpr be |
584-
n1 = be and
585-
n2 = be.getStmtList().getTailExpr() and
587+
n2 = be and
588+
n1 = be.getStmtList().getTailExpr() and
586589
if be.isAsync()
587590
then
588-
prefix1 = TypePath::singleton(getDynFutureOutputTypeParameter()) and
589-
prefix2.isEmpty()
591+
prefix2 = TypePath::singleton(getDynFutureOutputTypeParameter()) and
592+
prefix1.isEmpty()
590593
else (
591594
prefix1.isEmpty() and
592595
prefix2.isEmpty()
593596
)
594597
)
595598
or
596599
// an array repeat expression (`[1; 3]`) has the type of the repeat operand
597-
n1.(ArrayRepeatExpr).getRepeatOperand() = n2 and
598-
prefix1 = TypePath::singleton(getArrayTypeParameter()) and
599-
prefix2.isEmpty()
600+
n2.(ArrayRepeatExpr).getRepeatOperand() = n1 and
601+
prefix2 = TypePath::singleton(getArrayTypeParameter()) and
602+
prefix1.isEmpty()
600603
}
601604

602605
predicate inferStep(AstNode n1, TypePath prefix1, AstNode n2, TypePath prefix2) {
@@ -1853,6 +1856,14 @@ private module AssocFunctionResolution {
18531856
)
18541857
}
18551858

1859+
pragma[nomagic]
1860+
predicate resolutionDependsOnReturnType(DerefChain derefChain, BorrowKind borrow) {
1861+
exists(AssocFunctionCallCand afcc |
1862+
afcc = MkAssocFunctionCallCand(this, _, derefChain, borrow) and
1863+
afcc.resolutionDependsOnReturnType()
1864+
)
1865+
}
1866+
18561867
/**
18571868
* Holds if the argument `arg` of this call has been implicitly dereferenced
18581869
* and borrowed according to `derefChain` and `borrow`, in order to be able to
@@ -2333,6 +2344,18 @@ private module AssocFunctionResolution {
23332344
OverloadedCallArgsAreInstantiationsOf::argsAreInstantiationsOf(this, i, result)
23342345
}
23352346

2347+
pragma[nomagic]
2348+
predicate resolutionDependsOnReturnType() {
2349+
exists(
2350+
ImplOrTraitItemNode i, AssocFunctionDeclaration target, TypeParameter tp,
2351+
FunctionPosition pos
2352+
|
2353+
target = this.resolveCallTargetCand(i) and
2354+
FunctionOverloading::functionResolutionDependsOnArgument(i, target, tp, pos) and
2355+
pos.isReturn()
2356+
)
2357+
}
2358+
23362359
string toString() {
23372360
result = afc_ + " at " + selfPos_ + " [" + derefChain.toString() + "; " + borrow + "]"
23382361
}
@@ -2773,6 +2796,13 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
27732796
|
27742797
this.hasUnknownTypeAt(i, f, pos, path)
27752798
)
2799+
or
2800+
exists(DerefChain derefChain, BorrowKind borrow |
2801+
super.resolutionDependsOnReturnType(derefChain, borrow) and
2802+
derefChainBorrow = encodeDerefChainBorrow(derefChain, borrow) and
2803+
pos.isReturn() and
2804+
path.isEmpty()
2805+
)
27762806
}
27772807
}
27782808

0 commit comments

Comments
 (0)