@@ -773,7 +773,7 @@ class FunctionPosition extends TFunctionPosition {
773
773
this .isReturn ( ) and
774
774
result = this
775
775
or
776
- if f .getParamList ( ) . hasSelfParam ( )
776
+ if f .hasSelfParam ( )
777
777
then
778
778
this .isSelf ( ) and result .asPositional ( ) = 0
779
779
or
@@ -951,7 +951,7 @@ private newtype TFunctionPositionType =
951
951
MkFunctionPositionType ( Function f , FunctionPosition pos , ImplOrTraitItemNode i ) {
952
952
f = i .getAnAssocItem ( ) and
953
953
(
954
- f .getParamList ( ) . hasSelfParam ( ) and
954
+ f .hasSelfParam ( ) and
955
955
pos .asArgumentPosition ( ) .isSelf ( )
956
956
or
957
957
exists ( f .getParam ( pos .asPositional ( ) ) )
@@ -1107,7 +1107,8 @@ private module MethodCallResolution {
1107
1107
*/
1108
1108
pragma [ nomagic]
1109
1109
predicate methodCandidate (
1110
- Type type , string name , int arity , ImplOrTraitItemNode i , FunctionPositionType self
1110
+ Type type , string name , int arity , ImplOrTraitItemNode i , FunctionPositionType self ,
1111
+ Type selfType
1111
1112
) {
1112
1113
exists ( Function f , FunctionPosition pos |
1113
1114
f = i .getASuccessor ( name ) and
@@ -1116,13 +1117,17 @@ private module MethodCallResolution {
1116
1117
self .appliesTo ( f , pos , i ) and
1117
1118
pos .isSelf ( ) and
1118
1119
not i .( ImplItemNode ) .isBlanket ( )
1120
+ |
1121
+ selfType = i .( Impl ) .getSelfTy ( ) .( TypeMention ) .resolveType ( )
1122
+ or
1123
+ selfType = TTrait ( i )
1119
1124
)
1120
1125
}
1121
1126
1122
1127
pragma [ nomagic]
1123
1128
private predicate methodCandidateImplTrait ( string name , int arity , Trait trait ) {
1124
1129
exists ( ImplItemNode i |
1125
- methodCandidate ( _, name , arity , i , _) and
1130
+ methodCandidate ( _, name , arity , i , _, _ ) and
1126
1131
trait = i .resolveTraitTy ( )
1127
1132
)
1128
1133
}
@@ -1151,7 +1156,7 @@ private module MethodCallResolution {
1151
1156
) {
1152
1157
exists ( string name , int arity |
1153
1158
mc .isMethodCall ( name , arity ) and
1154
- methodCandidate ( type , name , arity , i , self ) //and
1159
+ methodCandidate ( type , name , arity , i , self , _ ) //and
1155
1160
|
1156
1161
// not CertainTypeInference::inferCertainType(mc.getReceiver(), TypePath::nil()) != type
1157
1162
not exists ( i .( ImplItemNode ) .resolveTraitTy ( ) )
@@ -1389,7 +1394,7 @@ private module MethodCallResolution {
1389
1394
exists ( Type type , string name , int arity |
1390
1395
this .isMethodCall ( _, type , name , arity ) and
1391
1396
forall ( Impl impl |
1392
- methodCandidate ( type , name , arity , impl , _) and
1397
+ methodCandidate ( type , name , arity , impl , _, _ ) and
1393
1398
not impl .hasTrait ( )
1394
1399
|
1395
1400
this .isNotInherentTarget ( impl )
@@ -1656,22 +1661,39 @@ private module FunctionCallResolution {
1656
1661
1657
1662
/** A function call, `f(x)`. */
1658
1663
final class FunctionCall extends CallExpr {
1659
- // FunctionCall() { this = Debug::getRelevantLocatable() }
1660
- private ItemNode getResolvedFunction ( ) { result = CallExprImpl:: getResolvedFunction ( this ) }
1664
+ private ItemNode getPathResolutionResolvedFunction ( ) {
1665
+ result = CallExprImpl:: getResolvedFunction ( this )
1666
+ }
1667
+
1668
+ // The `Self` type is supplied explicitly as a type qualifier, e.g. `Foo::<Bar>::baz()`
1669
+ pragma [ nomagic]
1670
+ Type getQualifierType ( TypePath path ) {
1671
+ exists ( PathExpr pe , TypeMention tm |
1672
+ pe = this .getFunction ( ) and
1673
+ tm = pe .getPath ( ) .getQualifier ( ) and
1674
+ result = tm .resolveTypeAt ( path ) and
1675
+ not resolvePath ( tm ) instanceof Trait
1676
+ )
1677
+ }
1661
1678
1662
1679
/**
1663
1680
* Holds if the target of this call is ambigous, and type information is required
1664
1681
* to disambiguate.
1665
1682
*/
1666
- predicate isAmbigous ( ) {
1683
+ private predicate isAmbigous ( ) {
1667
1684
this .( Call ) .hasTrait ( )
1668
1685
or
1669
- functionResolutionDependsOnArgument ( _, _, this .getResolvedFunction ( ) , _, _, _)
1686
+ functionResolutionDependsOnArgument ( _, _, this .getPathResolutionResolvedFunction ( ) , _, _, _)
1687
+ // or
1688
+ // // always check the `self` type in method calls
1689
+ // this.getPathResolutionResolvedFunction().(Function).hasSelfParam()
1670
1690
}
1671
1691
1672
1692
pragma [ nomagic]
1673
- Function getAnAmbigousCandidate0 ( ImplItemNode impl , FunctionPosition pos , Function resolved ) {
1674
- resolved = this .getResolvedFunction ( ) and
1693
+ Function getAnAmbigousCandidate0 (
1694
+ ImplOrTraitItemNode impl , FunctionPosition pos , Function resolved
1695
+ ) {
1696
+ resolved = this .getPathResolutionResolvedFunction ( ) and
1675
1697
(
1676
1698
exists ( TraitItemNode trait |
1677
1699
trait = this .( Call ) .getTrait ( ) and
@@ -1680,18 +1702,32 @@ private module FunctionCallResolution {
1680
1702
|
1681
1703
functionResolutionDependsOnArgument ( impl , _, result , pos , _, _)
1682
1704
or
1705
+ // todo: remove tp
1683
1706
exists ( TypeParameter tp | traitTypeParameterOccurrence ( trait , resolved , _, pos , _, tp ) |
1684
1707
not pos .isReturn ( )
1685
1708
or
1686
1709
// We only check that the context of the call provides relevant type information
1687
1710
// when no argument can
1688
- not traitTypeParameterOccurrence ( trait , resolved , _,
1689
- any ( FunctionPosition pos0 | not pos0 .isReturn ( ) ) , _, _)
1711
+ not exists ( FunctionPosition pos0 |
1712
+ traitTypeParameterOccurrence ( trait , resolved , _, pos0 , _, _) and
1713
+ not pos0 .isReturn ( )
1714
+ )
1690
1715
)
1716
+ or
1717
+ // always check the `self` type in method calls
1718
+ resolved .hasSelfParam ( ) and
1719
+ pos .isSelf ( )
1691
1720
)
1692
1721
or
1722
+ not this .( Call ) .hasTrait ( ) and
1693
1723
result = resolved and
1694
1724
functionResolutionDependsOnArgument ( impl , _, result , pos , _, _)
1725
+ // or
1726
+ // // always check the `self` type in method calls
1727
+ // not this.(Call).hasTrait() and
1728
+ // result = impl.getASuccessor(_) and
1729
+ // resolved.hasSelfParam() and
1730
+ // pos.isSelf()
1695
1731
)
1696
1732
}
1697
1733
@@ -1704,7 +1740,9 @@ private module FunctionCallResolution {
1704
1740
* `resolved` is the corresponding function resolved through path resolution.
1705
1741
*/
1706
1742
pragma [ nomagic]
1707
- Function getAnAmbigousCandidate ( ImplItemNode impl , FunctionPosition pos , Function resolved ) {
1743
+ Function getAnAmbigousCandidate (
1744
+ ImplOrTraitItemNode impl , FunctionPosition pos , Function resolved
1745
+ ) {
1708
1746
exists ( FunctionPosition pos0 |
1709
1747
result = this .getAnAmbigousCandidate0 ( impl , pos0 , resolved ) and
1710
1748
pos = pos0 .getFunctionCallAdjusted ( result )
@@ -1715,7 +1753,7 @@ private module FunctionCallResolution {
1715
1753
* Same as `getAnAmbigousCandidate`, ranks the positions to be checked.
1716
1754
*/
1717
1755
private Function getAnAmbigousCandidateRanked (
1718
- ImplItemNode impl , FunctionPosition pos , Function f , int rnk
1756
+ ImplOrTraitItemNode impl , FunctionPosition pos , Function f , int rnk
1719
1757
) {
1720
1758
pos =
1721
1759
rank [ rnk + 1 ] ( FunctionPosition pos0 , int i1 , int i2 |
@@ -1732,7 +1770,7 @@ private module FunctionCallResolution {
1732
1770
1733
1771
pragma [ nomagic]
1734
1772
private Function resolveAmbigousFunctionCallTargetFromIndex ( int index ) {
1735
- exists ( Impl impl , FunctionPosition pos , Function resolved |
1773
+ exists ( ImplOrTraitItemNode impl , FunctionPosition pos , Function resolved |
1736
1774
IsInstantiationOf< AmbigousFunctionCall , FunctionPositionType , AmbigousFuncIsInstantiationOfInput > :: isInstantiationOf ( MkAmbigousFunctionCall ( this ,
1737
1775
resolved , pos ) , impl , _) and
1738
1776
result = this .getAnAmbigousCandidateRanked ( impl , pos , resolved , index )
@@ -1759,7 +1797,7 @@ private module FunctionCallResolution {
1759
1797
*/
1760
1798
pragma [ nomagic]
1761
1799
private ItemNode resolveUnambigousFunctionCallTarget ( ) {
1762
- result = this .getResolvedFunction ( ) and
1800
+ result = this .getPathResolutionResolvedFunction ( ) and
1763
1801
not this .isAmbigous ( )
1764
1802
}
1765
1803
@@ -1785,13 +1823,24 @@ private module FunctionCallResolution {
1785
1823
AmbigousFunctionCall ( ) { this = MkAmbigousFunctionCall ( call , resolved , pos ) }
1786
1824
1787
1825
pragma [ nomagic]
1788
- Type getTypeAt ( TypePath path ) {
1826
+ private Type getTypeAt0 ( TypePath path ) {
1789
1827
result = inferType ( call .( CallExpr ) .getArg ( pos .asPositional ( ) ) , path )
1790
1828
or
1829
+ // pos.asPositional() = 0 and
1830
+ // result = call.getQualifierType(path)
1831
+ // or
1791
1832
pos .isReturn ( ) and
1792
1833
result = inferType ( call , path )
1793
1834
}
1794
1835
1836
+ pragma [ nomagic]
1837
+ Type getTypeAt ( TypePath path ) {
1838
+ result = this .getTypeAt0 ( path ) and
1839
+ not exists ( getATraitBound ( result ) )
1840
+ or
1841
+ result = TTrait ( getATraitBound ( this .getTypeAt0 ( path ) ) )
1842
+ }
1843
+
1795
1844
string toString ( ) { result = call .toString ( ) + " (pos: " + pos + ")" }
1796
1845
1797
1846
Location getLocation ( ) { result = call .getLocation ( ) }
@@ -1937,9 +1986,7 @@ private module FunctionCallMatchingInput implements MatchingInputSig {
1937
1986
exists ( Param p , int i |
1938
1987
p = this .getParam ( i ) and
1939
1988
result = p .getTypeRepr ( ) .( TypeMention ) .resolveTypeAt ( path ) and
1940
- if this .getParamList ( ) .hasSelfParam ( )
1941
- then dpos .asPositional ( ) = i + 1
1942
- else dpos .asPositional ( ) = i
1989
+ if this .hasSelfParam ( ) then dpos .asPositional ( ) = i + 1 else dpos .asPositional ( ) = i
1943
1990
)
1944
1991
or
1945
1992
dpos .asPositional ( ) = 0 and
@@ -1992,14 +2039,8 @@ private module FunctionCallMatchingInput implements MatchingInputSig {
1992
2039
1993
2040
pragma [ nomagic]
1994
2041
Type getInferredType ( AccessPosition apos , TypePath path ) {
1995
- // The `Self` type is supplied explicitly as a type qualifier, e.g. `Foo::<Bar>::baz()`
1996
2042
apos .asArgumentPosition ( ) .isSelf ( ) and
1997
- exists ( PathExpr pe , TypeMention tm |
1998
- pe = this .getFunction ( ) and
1999
- tm = pe .getPath ( ) .getQualifier ( ) and
2000
- result = tm .resolveTypeAt ( path ) and
2001
- not resolvePath ( tm ) instanceof Trait
2002
- )
2043
+ result = this .getQualifierType ( path )
2003
2044
or
2004
2045
result = inferType ( this .getNodeAt ( apos ) , path )
2005
2046
}
@@ -2028,6 +2069,7 @@ private Type inferCallExprType(AstNode n, TypePath path) {
2028
2069
private module OperationResolution {
2029
2070
/** An operation, `x + y`. */
2030
2071
final class Op extends Operation {
2072
+ // Op() { none() }
2031
2073
pragma [ nomagic]
2032
2074
private Type getTypeAt0 ( TypePath path ) {
2033
2075
if this .( Call ) .implicitBorrowAt ( any ( ArgumentPosition pos | pos .isSelf ( ) ) , true )
@@ -2099,7 +2141,7 @@ private module OperationResolution {
2099
2141
Type type , Trait trait , string name , int arity , ImplOrTraitItemNode i ,
2100
2142
FunctionPositionType self
2101
2143
) {
2102
- MethodCallResolution:: methodCandidate ( type , name , arity , i , self ) and
2144
+ MethodCallResolution:: methodCandidate ( type , name , arity , i , self , _ ) and
2103
2145
(
2104
2146
trait = i .( ImplItemNode ) .resolveTraitTy ( )
2105
2147
or
@@ -2109,9 +2151,10 @@ private module OperationResolution {
2109
2151
2110
2152
pragma [ nomagic]
2111
2153
predicate potentialInstantiationOf ( Op op , TypeAbstraction abs , FunctionPositionType constraint ) {
2112
- exists ( Type type , Trait trait , string name , int arity |
2154
+ exists ( Type type , Trait trait , string name , int arity , Type selfType |
2113
2155
op .isOperation ( type , trait , name , arity ) and
2114
- MethodCallResolution:: methodCandidate ( type , name , arity , abs , constraint )
2156
+ MethodCallResolution:: methodCandidate ( type , name , arity , abs , constraint , selfType ) and
2157
+ op .getTypeAt ( _) = selfType
2115
2158
|
2116
2159
trait = abs .( ImplItemNode ) .resolveTraitTy ( )
2117
2160
or
@@ -3035,7 +3078,7 @@ private module Debug {
3035
3078
// filepath.matches("%/crates/wdk-macros/src/lib.rs") and
3036
3079
// endline = [255 .. 256]
3037
3080
filepath .matches ( "%/main.rs" ) and
3038
- startline = 2318
3081
+ startline = 2317
3039
3082
)
3040
3083
}
3041
3084
0 commit comments