Skip to content

Commit d1f2d04

Browse files
committed
param
1 parent 0210a0e commit d1f2d04

3 files changed

Lines changed: 180 additions & 159 deletions

File tree

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

Lines changed: 60 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -453,22 +453,19 @@ private module Input3 implements InputSig3 {
453453
override TypeMention getType() { result = super.getTypeRepr() }
454454
}
455455

456-
final class Callable = CallableImpl;
456+
final class Parameterizable = ParameterizableImpl;
457457

458-
/** A callable, including enum/struct constructors. */
459-
abstract private class CallableImpl extends AstNode {
458+
abstract private class ParameterizableImpl extends AstNode {
460459
abstract TypeParameter getTypeParameter(TypeParameterPosition ppos);
461460

462461
abstract TypeMention getAdditionalTypeParameterConstraint(TypeParameter tp);
463462

464463
abstract Parameter getParameter(int i);
465464

466-
abstract TypeMention getReturnType();
467-
468-
abstract AstNode getBody();
465+
abstract TypeMention getType();
469466
}
470467

471-
private class CallableCallable extends CallableImpl instanceof Rust::Callable {
468+
class Callable extends ParameterizableImpl instanceof Rust::Callable {
472469
override TypeParameter getTypeParameter(TypeParameterPosition ppos) {
473470
result = this.(FunctionDeclaration).getTypeParameter(ppos)
474471
}
@@ -487,24 +484,22 @@ private module Input3 implements InputSig3 {
487484
)
488485
}
489486

490-
override TypeMention getReturnType() { result = getReturnTypeMention(this) }
487+
override TypeMention getType() { result = getReturnTypeMention(this) }
491488

492-
override AstNode getBody() { result = Rust::Callable.super.getBody() }
489+
AstNode getBody() { result = Rust::Callable.super.getBody() }
493490
}
494491

495492
additional final class Constructor = ConstructorImpl;
496493

497-
abstract private class ConstructorImpl extends CallableImpl {
494+
abstract private class ConstructorImpl extends ParameterizableImpl {
498495
override TypeMention getAdditionalTypeParameterConstraint(TypeParameter tp) { none() }
499496

500497
override TypeParameter getTypeParameter(TypeParameterPosition ppos) {
501498
result = ppos.asTypeParameter() and
502499
ppos.asTypeParam() = this.getTypeItem().getGenericParamList().getATypeParam()
503500
}
504501

505-
override TypeMention getReturnType() { none() }
506-
507-
override AstNode getBody() { none() }
502+
override TypeMention getType() { result = this.getTypeItem() }
508503

509504
abstract TypeItem getTypeItem();
510505
}
@@ -525,33 +520,25 @@ private module Input3 implements InputSig3 {
525520
}
526521
}
527522

528-
Type getCallableReturnType(Callable c, TypePath path) {
529-
c =
530-
any(Constructor constructor |
531-
result = TDataType(constructor.getTypeItem()) and
532-
path.isEmpty()
533-
or
534-
result =
535-
TTypeParamTypeParameter(constructor.getTypeItem().getGenericParamList().getATypeParam()) and
536-
path = TypePath::singleton(result)
537-
)
523+
Type getParameterizableType(Parameterizable p, TypePath path) {
524+
result = p.(Constructor).getType().getTypeAt(path)
538525
or
539-
if c.(Function).isAsync()
526+
if p.(Function).isAsync() or p.(ClosureExpr).isAsync()
540527
then
541528
path.isEmpty() and
542529
result = getFutureTraitType()
543530
or
544531
exists(TypePath suffix |
545-
result = getReturnTypeMention(c).getTypeAt(suffix) and
532+
result = getReturnTypeMention(p).getTypeAt(suffix) and
546533
path = TypePath::cons(getDynFutureOutputTypeParameter(), suffix)
547534
)
548-
else result = getReturnTypeMention(c).getTypeAt(path)
535+
else result = getReturnTypeMention(p).getTypeAt(path)
549536
}
550537

551-
class CallResolutionContext = string;
538+
class ResolutionContext = string;
552539

553540
bindingset[derefChain, borrow]
554-
private CallResolutionContext encodeDerefChainBorrow(DerefChain derefChain, BorrowKind borrow) {
541+
private ResolutionContext encodeDerefChainBorrow(DerefChain derefChain, BorrowKind borrow) {
555542
result = derefChain + ";" + borrow
556543
}
557544

@@ -574,18 +561,17 @@ private module Input3 implements InputSig3 {
574561
)
575562
}
576563

577-
final class Call = CallImpl;
564+
final class Invocation = InvocationImpl;
578565

579-
/** A call expression, including enum/struct constructions. */
580-
abstract private class CallImpl extends Expr {
566+
abstract private class InvocationImpl extends Expr {
581567
abstract Type getTypeArgument(TypeArgumentPosition apos, TypePath path);
582568

583569
abstract AstNode getArgument(int i);
584570

585-
abstract Callable getTarget(string derefChainBorrow);
571+
abstract Parameterizable getTarget(string derefChainBorrow);
586572
}
587573

588-
private class AssocFunctionCall extends CallImpl instanceof AssocFunctionResolution::AssocFunctionCall
574+
private class AssocFunctionCall extends InvocationImpl instanceof AssocFunctionResolution::AssocFunctionCall
589575
{
590576
pragma[nomagic]
591577
override Type getTypeArgument(TypeArgumentPosition apos, TypePath path) {
@@ -633,7 +619,7 @@ private module Input3 implements InputSig3 {
633619
}
634620
}
635621

636-
private class NonAssocFunctionCall extends CallImpl instanceof NonAssocCallExpr,
622+
private class NonAssocFunctionCall extends InvocationImpl instanceof NonAssocCallExpr,
637623
CallExprImpl::CallExprCall
638624
{
639625
pragma[nomagic]
@@ -657,10 +643,10 @@ private module Input3 implements InputSig3 {
657643
}
658644
}
659645

660-
abstract private class Construction extends CallImpl {
646+
abstract private class Construction extends InvocationImpl {
661647
abstract Constructor getTarget();
662648

663-
override Callable getTarget(string derefChainBorrow) {
649+
override Parameterizable getTarget(string derefChainBorrow) {
664650
result = this.getTarget() and
665651
derefChainBorrow = noDerefChainBorrow()
666652
}
@@ -725,27 +711,30 @@ private module Input3 implements InputSig3 {
725711
override AstNode getArgument(int i) { none() }
726712
}
727713

728-
Callable getAdditionalCallTargetForContextualReturnTyping(Call call) {
729-
result = CallExprImpl::getResolvedFunction(call)
714+
Parameterizable getAdditionalTargetForContextualReturnTyping(Invocation invocation) {
715+
result = CallExprImpl::getResolvedFunction(invocation)
730716
}
731717

732718
pragma[nomagic]
733-
private Type inferCallArgumentType0(Call call, int pos, TypePath path) {
719+
private Type inferInvocationArgumentType0(Invocation invocation, int pos, TypePath path) {
734720
exists(FunctionPosition fpos |
735721
pos = fpos.asPosition() and
736-
result = call.(AssocFunctionResolution::OperationAssocFunctionCall).getTypeAt(fpos, path)
722+
result =
723+
invocation.(AssocFunctionResolution::OperationAssocFunctionCall).getTypeAt(fpos, path)
737724
)
738725
or
739-
not call instanceof Operation and
740-
not call instanceof AssocFunctionCall and
741-
result = inferType(call.getArgument(pos), path)
726+
not invocation instanceof Operation and
727+
not invocation instanceof AssocFunctionCall and
728+
result = inferType(invocation.getArgument(pos), path)
742729
}
743730

744731
bindingset[derefChainBorrow]
745-
Type inferCallArgumentType(Call call, string derefChainBorrow, int pos, TypePath path) {
746-
result = inferCallArgumentType0(call, pos, path)
732+
Type inferInvocationArgumentType(
733+
Invocation invocation, string derefChainBorrow, int pos, TypePath path
734+
) {
735+
result = inferInvocationArgumentType0(invocation, pos, path)
747736
or
748-
call =
737+
invocation =
749738
any(AssocFunctionCall afc |
750739
result = afc.getInferredSelfType(pos, derefChainBorrow, path)
751740
or
@@ -754,12 +743,14 @@ private module Input3 implements InputSig3 {
754743
}
755744

756745
pragma[nomagic]
757-
private Type inferCallArgumentTypeContextualDefault(
758-
Call call, int pos, AstNode n, DerefChain derefChain, BorrowKind borrow, TypePath path
746+
private Type inferInvocationArgumentTypeContextualDefault(
747+
Invocation invocation, int pos, AstNode n, DerefChain derefChain, BorrowKind borrow,
748+
TypePath path
759749
) {
760750
exists(string derefChainBorrow |
761751
decodeDerefChainBorrow(derefChainBorrow, derefChain, borrow) and
762-
result = M3::inferCallArgumentTypeContextualDefault(call, derefChainBorrow, pos, n, path)
752+
result =
753+
M3::inferInvocationArgumentTypeContextualDefault(invocation, derefChainBorrow, pos, n, path)
763754
)
764755
}
765756

@@ -771,13 +762,14 @@ private module Input3 implements InputSig3 {
771762
* empty, at which point the inferred type can be applied back to `n`.
772763
*/
773764
pragma[nomagic]
774-
private Type inferFunctionCallSelfArgumentTypeContextual(
775-
Call call, AstNode n, DerefChain derefChain, TypePath path
765+
private Type inferInvocationSelfArgumentTypeContextual(
766+
Invocation invocation, AstNode n, DerefChain derefChain, TypePath path
776767
) {
777768
exists(FunctionPosition pos, BorrowKind borrow, TypePath path0 |
778-
call.(AssocFunctionResolution::AssocFunctionCall).hasReceiverAtPos(pos) and
769+
invocation.(AssocFunctionResolution::AssocFunctionCall).hasReceiverAtPos(pos) and
779770
result =
780-
inferCallArgumentTypeContextualDefault(call, pos.asPosition(), n, derefChain, borrow, path0)
771+
inferInvocationArgumentTypeContextualDefault(invocation, pos.asPosition(), n, derefChain,
772+
borrow, path0)
781773
|
782774
borrow.isNoBorrow() and
783775
path = path0
@@ -794,7 +786,7 @@ private module Input3 implements InputSig3 {
794786
DerefChain derefChain0, Type t0, TypePath path0, DerefImplItemNode impl, Type selfParamType,
795787
TypePath selfPath
796788
|
797-
t0 = inferFunctionCallSelfArgumentTypeContextual(call, n, derefChain0, path0) and
789+
t0 = inferInvocationSelfArgumentTypeContextual(invocation, n, derefChain0, path0) and
798790
derefChain0.isCons(impl, derefChain) and
799791
selfParamType = impl.resolveSelfTypeAt(selfPath)
800792
|
@@ -811,40 +803,41 @@ private module Input3 implements InputSig3 {
811803
)
812804
}
813805

814-
Type inferCallArgumentTypeContextual(AstNode n, TypePath path) {
815-
exists(Call call, FunctionPosition pos, TypePath path0 |
816-
result = inferCallArgumentTypeContextualDefault(call, pos.asPosition(), n, _, _, path0) and
817-
not call.(AssocFunctionResolution::AssocFunctionCall).hasReceiverAtPos(pos)
806+
Type inferInvocationArgumentTypeContextual(AstNode n, TypePath path) {
807+
exists(Invocation invocation, FunctionPosition pos, TypePath path0 |
808+
result =
809+
inferInvocationArgumentTypeContextualDefault(invocation, pos.asPosition(), n, _, _, path0) and
810+
not invocation.(AssocFunctionResolution::AssocFunctionCall).hasReceiverAtPos(pos)
818811
or
819812
pos.asPosition() = 0 and
820-
result = inferFunctionCallSelfArgumentTypeContextual(call, n, DerefChain::nil(), path0) and
813+
result = inferInvocationSelfArgumentTypeContextual(invocation, n, DerefChain::nil(), path0) and
821814
not path0.isEmpty()
822815
|
823-
if call.(AssocFunctionResolution::OperationAssocFunctionCall).implicitBorrowAt(pos, _)
816+
if invocation.(AssocFunctionResolution::OperationAssocFunctionCall).implicitBorrowAt(pos, _)
824817
then
825818
// adjust for implicit borrow
826819
path0.isCons(getRefTypeParameter(_), path)
827820
else path = path0
828821
)
829822
}
830823

831-
Type inferCallType(Call call, TypePath path) {
824+
Type inferInvocationType(Invocation invocation, TypePath path) {
832825
exists(TypePath path0 |
833-
result = M3::inferCallTypeDefault(call, _, path0) and
826+
result = M3::inferInvocationTypeDefault(invocation, _, path0) and
834827
// index expression `x[i]` desugars to `*x.index(i)`, so we must account for
835828
// the implicit deref
836-
if call instanceof IndexExpr or call instanceof DerefExpr
829+
if invocation instanceof IndexExpr or invocation instanceof DerefExpr
837830
then path0.isCons(getRefTypeParameter(_), path)
838831
else path = path0
839832
)
840833
}
841834

842-
Type inferCallTypeContextual(Call call, TypePath path) {
835+
Type inferInvocationTypeContextual(Invocation invocation, TypePath path) {
843836
exists(TypePath path0 |
844-
result = inferType(call, path0) and
837+
result = inferType(invocation, path0) and
845838
// index expression `x[i]` desugars to `*x.index(i)`, so we must account for
846839
// the implicit deref
847-
if call instanceof IndexExpr or call instanceof DerefExpr
840+
if invocation instanceof IndexExpr or invocation instanceof DerefExpr
848841
then path0.isCons(getRefTypeParameter(_), path)
849842
else path = path0
850843
)
@@ -3151,7 +3144,7 @@ private module DeconstructionPatMatchingInput implements MatchingInputSig {
31513144
result = this.getParameter(pos.asPosition()).getType().getTypeAt(path)
31523145
or
31533146
pos.isReturn() and
3154-
result = Input3::getCallableReturnType(this, path)
3147+
result = Input3::getParameterizableType(this, path)
31553148
}
31563149
}
31573150

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,20 @@ private module MkTypeMention<getAdditionalPathTypeAtSig/2 getAdditionalPathTypeA
701701
class InferTypeReprMention extends TypeMentionImpl, InferTypeRepr {
702702
override Type getTypeAt(TypePath path) { result = TUnknownType() and path.isEmpty() }
703703
}
704+
705+
/** A type item mention that resolves to itself. */
706+
class TypeItemMention extends TypeMentionImpl instanceof TypeItem {
707+
override Type getTypeAt(TypePath typePath) {
708+
typePath.isEmpty() and
709+
result = TDataType(this)
710+
or
711+
exists(TypeParam tp |
712+
tp = this.(ItemNode).getTypeParam(_) and
713+
typePath = TypePath::singleton(result) and
714+
result = TTypeParamTypeParameter(tp)
715+
)
716+
}
717+
}
704718
}
705719

706720
private Type preGetAdditionalPathTypeAt(Path p, TypePath typePath) { none() }

0 commit comments

Comments
 (0)