@@ -22,15 +22,6 @@ private import codeql.rust.elements.internal.CallExprImpl::Impl as CallExprImpl
2222
2323class Type = T:: Type ;
2424
25- private newtype TTypeArgumentPosition =
26- // method type parameters are matched by position instead of by type
27- // parameter entity, to avoid extra recursion through method call resolution
28- TMethodTypeArgumentPosition ( int pos ) {
29- exists ( any ( MethodCallExpr mce ) .getGenericArgList ( ) .getTypeArg ( pos ) )
30- } or
31- TTypeParamTypeArgumentPosition ( TypeParam tp ) or
32- TTypeQualifierArgumentPosition ( )
33-
3425private module Input1 implements InputSig1< Location > {
3526 private import Type as T
3627 private import codeql.rust.elements.internal.generated.Raw
@@ -46,22 +37,7 @@ private module Input1 implements InputSig1<Location> {
4637
4738 class TypeAbstraction = TA:: TypeAbstraction ;
4839
49- class TypeArgumentPosition extends TTypeArgumentPosition {
50- int asMethodTypeArgumentPosition ( ) { this = TMethodTypeArgumentPosition ( result ) }
51-
52- TypeParam asTypeParam ( ) { this = TTypeParamTypeArgumentPosition ( result ) }
53-
54- predicate isTypeQualifierArgumentPosition ( ) { this = TTypeQualifierArgumentPosition ( ) }
55-
56- string toString ( ) {
57- result = this .asMethodTypeArgumentPosition ( ) .toString ( )
58- or
59- result = this .asTypeParam ( ) .toString ( )
60- or
61- this .isTypeQualifierArgumentPosition ( ) and
62- result = "type qualifier"
63- }
64- }
40+ class TypeArgumentPosition = int ;
6541
6642 private newtype TTypeParameterPosition = TTypeParamTypeParameterPosition ( TypeParameter tp )
6743
@@ -78,18 +54,7 @@ private module Input1 implements InputSig1<Location> {
7854 bindingset [ apos]
7955 bindingset [ ppos]
8056 predicate typeArgumentParameterPositionMatch ( TypeArgumentPosition apos , TypeParameterPosition ppos ) {
81- TTypeParamTypeParameter ( apos .asTypeParam ( ) ) = ppos .asTypeParameter ( )
82- or
83- apos .asMethodTypeArgumentPosition ( ) = ppos .asTypeParam ( ) .getPosition ( )
84- or
85- // Type qualifiers can match against implicit `Self` type parameters and blanket
86- // type parameters.
87- apos .isTypeQualifierArgumentPosition ( ) and
88- (
89- ppos .asTypeParameter ( ) instanceof SelfTypeParameter
90- or
91- exists ( ImplItemNode i | ppos .asTypeParam ( ) = i .getBlanketImplementationTypeParam ( ) )
92- )
57+ apos = ppos .asTypeParam ( ) .getPosition ( )
9358 }
9459
9560 int getTypeParameterId ( TypeParameter tp ) {
@@ -460,12 +425,24 @@ private module Input3 implements InputSig3 {
460425
461426 abstract TypeMention getAdditionalTypeParameterConstraint ( TypeParameter tp ) ;
462427
428+ abstract TypeMention getDeclaringType ( ) ;
429+
430+ // todo
431+ Type getDeclaringType ( TypePath path ) { result = this .getDeclaringType ( ) .getTypeAt ( path ) }
432+
463433 abstract Parameter getParameter ( int i ) ;
464434
465435 abstract TypeMention getType ( ) ;
466436 }
467437
468438 class Callable extends ParameterizableImpl instanceof Rust:: Callable {
439+ override TypeMention getDeclaringType ( ) {
440+ exists ( ImplOrTraitItemNode implOrTrait | this = implOrTrait .getAnAssocItem ( ) |
441+ result = implOrTrait .( Impl ) .getSelfTy ( ) or
442+ result = implOrTrait .( Trait )
443+ )
444+ }
445+
469446 override TypeParameter getTypeParameter ( TypeParameterPosition ppos ) {
470447 result = this .( FunctionDeclaration ) .getTypeParameter ( ppos )
471448 }
@@ -492,6 +469,10 @@ private module Input3 implements InputSig3 {
492469 additional final class Constructor = ConstructorImpl ;
493470
494471 abstract private class ConstructorImpl extends ParameterizableImpl {
472+ override TypeMention getDeclaringType ( ) {
473+ result = this .getTypeItem ( ) // todo?
474+ }
475+
495476 override TypeMention getAdditionalTypeParameterConstraint ( TypeParameter tp ) { none ( ) }
496477
497478 override TypeParameter getTypeParameter ( TypeParameterPosition ppos ) {
@@ -564,6 +545,8 @@ private module Input3 implements InputSig3 {
564545 final class Invocation = InvocationImpl ;
565546
566547 abstract private class InvocationImpl extends Expr {
548+ abstract TypeMention getTypeQualifier ( ) ;
549+
567550 abstract Type getTypeArgument ( TypeArgumentPosition apos , TypePath path ) ;
568551
569552 abstract AstNode getArgument ( int i ) ;
@@ -573,14 +556,12 @@ private module Input3 implements InputSig3 {
573556
574557 private class AssocFunctionCall extends InvocationImpl instanceof AssocFunctionResolution:: AssocFunctionCall
575558 {
559+ override TypeMention getTypeQualifier ( ) { result = getCallExprTypeArgument2 ( this ) }
560+
576561 pragma [ nomagic]
577562 override Type getTypeArgument ( TypeArgumentPosition apos , TypePath path ) {
578563 result =
579- this .( MethodCallExpr )
580- .getGenericArgList ( )
581- .getTypeArg ( apos .asMethodTypeArgumentPosition ( ) )
582- .( TypeMention )
583- .getTypeAt ( path )
564+ this .( MethodCallExpr ) .getGenericArgList ( ) .getTypeArg ( apos ) .( TypeMention ) .getTypeAt ( path )
584565 or
585566 result = getCallExprTypeArgument ( this , apos , path )
586567 }
@@ -622,6 +603,8 @@ private module Input3 implements InputSig3 {
622603 private class NonAssocFunctionCall extends InvocationImpl instanceof NonAssocCallExpr ,
623604 CallExprImpl:: CallExprCall
624605 {
606+ override TypeMention getTypeQualifier ( ) { none ( ) }
607+
625608 pragma [ nomagic]
626609 override Type getTypeArgument ( TypeArgumentPosition apos , TypePath path ) {
627610 result = NonAssocCallExpr .super .getTypeArgument ( apos , path )
@@ -658,6 +641,8 @@ private module Input3 implements InputSig3 {
658641 this instanceof CallExprImpl:: TupleVariantExpr
659642 }
660643
644+ override TypeMention getTypeQualifier ( ) { result = getCallExprTypeArgument2 ( this ) }
645+
661646 override Type getTypeArgument ( TypeArgumentPosition apos , TypePath path ) {
662647 result = NonAssocCallExpr .super .getTypeArgument ( apos , path )
663648 }
@@ -676,27 +661,16 @@ private module Input3 implements InputSig3 {
676661 pragma [ nomagic]
677662 override Constructor getTarget ( ) { result = resolvePath ( super .getPath ( ) ) }
678663
679- pragma [ nomagic]
680- override Type getTypeArgument ( TypeArgumentPosition apos , TypePath path ) {
664+ override TypeMention getTypeQualifier ( ) {
681665 // Handle constructions that use `Self {...}` syntax
682- exists ( TypeMention tm , TypePath path0 |
683- tm = super .getPath ( ) and
684- result = tm .getTypeAt ( path0 ) and
685- path0 .isCons ( TTypeParamTypeParameter ( apos .asTypeParam ( ) ) , path )
686- )
666+ result = super .getPath ( )
687667 }
668+
669+ pragma [ nomagic]
670+ override Type getTypeArgument ( TypeArgumentPosition apos , TypePath path ) { none ( ) }
688671 }
689672
690673 private class StructExprConstruction extends StructConstruction , StructExpr {
691- override Type getTypeArgument ( TypeArgumentPosition apos , TypePath path ) {
692- result = super .getTypeArgument ( apos , path )
693- or
694- exists ( TypePath suffix |
695- suffix .isCons ( TTypeParamTypeParameter ( apos .asTypeParam ( ) ) , path ) and
696- result = inferTypeCertain ( this , suffix )
697- )
698- }
699-
700674 override AstNode getArgument ( int i ) {
701675 result =
702676 this .getFieldExpr ( pragma [ only_bind_into ] ( this .getNthStructField ( i ) .getName ( ) .getText ( ) ) )
@@ -1215,62 +1189,29 @@ private class AssocFunctionDeclaration extends FunctionDeclaration {
12151189}
12161190
12171191pragma [ nomagic]
1218- private TypePath getPathToImplSelfTypeParam ( TypeParam tp ) {
1219- exists ( ImplItemNode impl |
1220- tp = impl .getTypeParam ( _) and
1221- TTypeParamTypeParameter ( tp ) = impl .( Impl ) .getSelfTy ( ) .( TypeMention ) .getTypeAt ( result )
1192+ private Type getCallExprTypeArgument ( CallExpr ce , int pos , TypePath path ) {
1193+ exists ( Path p | p = CallExprImpl:: getFunctionPath ( ce ) |
1194+ result = getPathTypeArgument ( p , pos ) .getTypeAt ( path )
12221195 )
12231196}
12241197
12251198pragma [ nomagic]
1226- private Type getCallExprTypeArgument ( CallExpr ce , TypeArgumentPosition apos , TypePath path ) {
1199+ private TypeMention getCallExprTypeArgument2 ( CallExpr ce ) {
12271200 exists ( Path p | p = CallExprImpl:: getFunctionPath ( ce ) |
1228- exists ( ItemNode resolved , TypeParam tp |
1229- resolved = resolvePath ( p ) and
1230- apos .asTypeParam ( ) = tp
1231- |
1232- // For type parameters of the function we must resolve their
1233- // instantiation from the path. For instance, for `fn bar<A>(a: A) -> A`
1234- // and the path `bar<i64>`, we must resolve `A` to `i64`.
1235- exists ( int i |
1236- tp = resolved .getTypeParam ( pragma [ only_bind_into ] ( i ) ) and
1237- result = getPathTypeArgument ( p , pragma [ only_bind_into ] ( i ) ) .getTypeAt ( path )
1238- )
1239- or
1240- // For type parameters of the `impl` block we must resolve their
1241- // instantiation from the path. For instance, for `impl<A> for Foo<A>`
1242- // and the path `Foo<i64>::bar` we must resolve `A` to `i64`.
1243- exists ( ImplItemNode impl , TypePath pathToTp |
1244- resolved = impl .getASuccessor ( _) and
1245- tp = impl .getTypeParam ( _) and
1246- pathToTp = getPathToImplSelfTypeParam ( tp ) and
1247- result = p .getQualifier ( ) .( TypeMention ) .getTypeAt ( pathToTp .appendInverse ( path ) )
1248- )
1249- )
1250- or
1251- exists ( Path qualifier , TypeMention tm |
1201+ exists ( Path qualifier |
12521202 qualifier = p .getQualifier ( ) and
1253- result = tm .getTypeAt ( path ) and
1254- not tm .getType ( ) instanceof TraitType and
1255- apos .isTypeQualifierArgumentPosition ( )
1203+ not result .getType ( ) instanceof TraitType
12561204 |
1257- tm = qualifier
1205+ result = qualifier
12581206 or
12591207 // `<Foo as Bar>::baz()`; `Foo` is the type qualifier
12601208 exists ( PathSegment segment |
12611209 segment = qualifier .getSegment ( ) and
1262- tm = segment .getTypeRepr ( ) and
1210+ result = segment .getTypeRepr ( ) and
12631211 segment .hasTraitTypeRepr ( )
12641212 )
12651213 )
12661214 )
1267- or
1268- // Handle constructions that use `Self(...)` syntax
1269- exists ( Path p , TypePath path0 |
1270- p = CallExprImpl:: getFunctionPath ( ce ) and
1271- result = p .( TypeMention ) .getTypeAt ( path0 ) and
1272- path0 .isCons ( TTypeParamTypeParameter ( apos .asTypeParam ( ) ) , path )
1273- )
12741215}
12751216
12761217/**
0 commit comments