11/** Provides functionality for inferring types. */
22
3+ private import codeql.util.Boolean
34private import rust
45private import PathResolution
56private import Type
@@ -638,7 +639,28 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
638639 }
639640 }
640641
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+ }
642664
643665 private import codeql.rust.elements.internal.CallExprImpl:: Impl as CallExprImpl
644666
@@ -655,7 +677,8 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
655677 AstNode getNodeAt ( AccessPosition apos ) {
656678 result = this .getArgument ( apos .asPosition ( ) )
657679 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 )
659682 or
660683 result = this and apos .isReturn ( )
661684 }
@@ -672,16 +695,19 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
672695 }
673696
674697 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 ( )
676703 }
677704
678- bindingset [ a , apos, target, path, t]
705+ bindingset [ apos, target, path, t]
679706 pragma [ inline_late]
680707 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
683709 ) {
684- if apos .isSelf ( ) and a . receiverImplicitlyBorrowed ( )
710+ if apos .isSelf ( true )
685711 then
686712 exists ( Type selfParamType |
687713 selfParamType = target .getParameterType ( TSelfDeclarationPosition ( ) , TypePath:: nil ( ) )
@@ -741,7 +767,7 @@ private Type inferCallExprBaseType(AstNode n, TypePath path) {
741767 n = a .getNodeAt ( apos ) and
742768 result = CallExprBaseMatching:: inferAccessType ( a , apos , path0 )
743769 |
744- if apos .isSelf ( )
770+ if apos .isSelf ( _ )
745771 then
746772 exists ( Type receiverType | receiverType = inferType ( n ) |
747773 if receiverType = TRefType ( )
@@ -845,13 +871,11 @@ private module FieldExprMatchingInput implements MatchingInputSig {
845871 apos = dpos
846872 }
847873
848- bindingset [ a , apos, target, path, t]
874+ bindingset [ apos, target, path, t]
849875 pragma [ inline_late]
850876 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
853878 ) {
854- exists ( a ) and
855879 exists ( target ) and
856880 if apos .isSelf ( )
857881 then
@@ -1220,7 +1244,7 @@ private module Cached {
12201244 cached
12211245 predicate receiverHasImplicitDeref ( AstNode receiver ) {
12221246 exists ( CallExprBaseMatchingInput:: Access a , CallExprBaseMatchingInput:: AccessPosition apos |
1223- apos .isSelf ( ) and
1247+ apos .isSelf ( true ) and
12241248 receiver = a .getNodeAt ( apos ) and
12251249 inferType ( receiver ) = TRefType ( ) and
12261250 CallExprBaseMatching:: inferAccessType ( a , apos , TypePath:: nil ( ) ) != TRefType ( )
@@ -1231,7 +1255,7 @@ private module Cached {
12311255 cached
12321256 predicate receiverHasImplicitBorrow ( AstNode receiver ) {
12331257 exists ( CallExprBaseMatchingInput:: Access a , CallExprBaseMatchingInput:: AccessPosition apos |
1234- apos .isSelf ( ) and
1258+ apos .isSelf ( true ) and
12351259 receiver = a .getNodeAt ( apos ) and
12361260 CallExprBaseMatching:: inferAccessType ( a , apos , TypePath:: nil ( ) ) = TRefType ( ) and
12371261 inferType ( receiver ) != TRefType ( )
0 commit comments