1
1
/** Provides functionality for inferring types. */
2
2
3
+ private import codeql.util.Boolean
3
4
private import rust
4
5
private import PathResolution
5
6
private import Type
@@ -638,7 +639,28 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
638
639
}
639
640
}
640
641
641
- class AccessPosition = DeclarationPosition ;
642
+ private newtype TAccessPosition =
643
+ TSelfAccessPosition ( Boolean implicitlyBorrowed ) or
644
+ TPositionalAccessPosition ( int pos ) { exists ( TPositionalDeclarationPosition ( pos ) ) } or
645
+ TReturnAccessPosition ( )
646
+
647
+ class AccessPosition extends TAccessPosition {
648
+ predicate isSelf ( boolean implicitlyBorrowed ) { this = TSelfAccessPosition ( implicitlyBorrowed ) }
649
+
650
+ int asPosition ( ) { this = TPositionalAccessPosition ( result ) }
651
+
652
+ predicate isReturn ( ) { this = TReturnAccessPosition ( ) }
653
+
654
+ string toString ( ) {
655
+ this .isSelf ( _) and
656
+ result = "self"
657
+ or
658
+ result = this .asPosition ( ) .toString ( )
659
+ or
660
+ this .isReturn ( ) and
661
+ result = "(return)"
662
+ }
663
+ }
642
664
643
665
private import codeql.rust.elements.internal.CallExprImpl:: Impl as CallExprImpl
644
666
@@ -655,7 +677,8 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
655
677
AstNode getNodeAt ( AccessPosition apos ) {
656
678
result = this .getArgument ( apos .asPosition ( ) )
657
679
or
658
- result = this .getReceiver ( ) and apos .isSelf ( )
680
+ result = this .getReceiver ( ) and
681
+ if this .receiverImplicitlyBorrowed ( ) then apos .isSelf ( true ) else apos .isSelf ( false )
659
682
or
660
683
result = this and apos .isReturn ( )
661
684
}
@@ -672,16 +695,19 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
672
695
}
673
696
674
697
predicate accessDeclarationPositionMatch ( AccessPosition apos , DeclarationPosition dpos ) {
675
- apos = dpos
698
+ apos .isSelf ( _) and dpos .isSelf ( )
699
+ or
700
+ apos .asPosition ( ) = dpos .asPosition ( )
701
+ or
702
+ apos .isReturn ( ) and dpos .isReturn ( )
676
703
}
677
704
678
- bindingset [ a , apos, target, path, t]
705
+ bindingset [ apos, target, path, t]
679
706
pragma [ inline_late]
680
707
predicate adjustAccessType (
681
- Access a , AccessPosition apos , Declaration target , TypePath path , Type t , TypePath pathAdj ,
682
- Type tAdj
708
+ AccessPosition apos , Declaration target , TypePath path , Type t , TypePath pathAdj , Type tAdj
683
709
) {
684
- if apos .isSelf ( ) and a . receiverImplicitlyBorrowed ( )
710
+ if apos .isSelf ( true )
685
711
then
686
712
exists ( Type selfParamType |
687
713
selfParamType = target .getParameterType ( TSelfDeclarationPosition ( ) , TypePath:: nil ( ) )
@@ -741,7 +767,7 @@ private Type inferCallExprBaseType(AstNode n, TypePath path) {
741
767
n = a .getNodeAt ( apos ) and
742
768
result = CallExprBaseMatching:: inferAccessType ( a , apos , path0 )
743
769
|
744
- if apos .isSelf ( )
770
+ if apos .isSelf ( _ )
745
771
then
746
772
exists ( Type receiverType | receiverType = inferType ( n ) |
747
773
if receiverType = TRefType ( )
@@ -845,13 +871,11 @@ private module FieldExprMatchingInput implements MatchingInputSig {
845
871
apos = dpos
846
872
}
847
873
848
- bindingset [ a , apos, target, path, t]
874
+ bindingset [ apos, target, path, t]
849
875
pragma [ inline_late]
850
876
predicate adjustAccessType (
851
- Access a , AccessPosition apos , Declaration target , TypePath path , Type t , TypePath pathAdj ,
852
- Type tAdj
877
+ AccessPosition apos , Declaration target , TypePath path , Type t , TypePath pathAdj , Type tAdj
853
878
) {
854
- exists ( a ) and
855
879
exists ( target ) and
856
880
if apos .isSelf ( )
857
881
then
@@ -1220,7 +1244,7 @@ private module Cached {
1220
1244
cached
1221
1245
predicate receiverHasImplicitDeref ( AstNode receiver ) {
1222
1246
exists ( CallExprBaseMatchingInput:: Access a , CallExprBaseMatchingInput:: AccessPosition apos |
1223
- apos .isSelf ( ) and
1247
+ apos .isSelf ( true ) and
1224
1248
receiver = a .getNodeAt ( apos ) and
1225
1249
inferType ( receiver ) = TRefType ( ) and
1226
1250
CallExprBaseMatching:: inferAccessType ( a , apos , TypePath:: nil ( ) ) != TRefType ( )
@@ -1231,7 +1255,7 @@ private module Cached {
1231
1255
cached
1232
1256
predicate receiverHasImplicitBorrow ( AstNode receiver ) {
1233
1257
exists ( CallExprBaseMatchingInput:: Access a , CallExprBaseMatchingInput:: AccessPosition apos |
1234
- apos .isSelf ( ) and
1258
+ apos .isSelf ( true ) and
1235
1259
receiver = a .getNodeAt ( apos ) and
1236
1260
CallExprBaseMatching:: inferAccessType ( a , apos , TypePath:: nil ( ) ) = TRefType ( ) and
1237
1261
inferType ( receiver ) != TRefType ( )
0 commit comments