@@ -38,21 +38,45 @@ private module Input1 implements InputSig1<Location> {
3838 }
3939 }
4040
41- class TypeParameterPosition = TypeParam ;
41+ private newtype TTypeParameterPosition =
42+ TTypeParamTypeParameterPosition ( TypeParam tp ) or
43+ TSelfTypeParameterPosition ( )
44+
45+ class TypeParameterPosition extends TTypeParameterPosition {
46+ TypeParam asTypeParam ( ) { this = TTypeParamTypeParameterPosition ( result ) }
47+
48+ predicate isSelf ( ) { this = TSelfTypeParameterPosition ( ) }
49+
50+ string toString ( ) {
51+ result = this .asTypeParam ( ) .toString ( )
52+ or
53+ result = "Self" and this .isSelf ( )
54+ }
55+ }
56+
57+ /** Holds if `typeParam`, `param` and `ppos` all concern the same `TypeParam`. */
58+ additional predicate typeParamMatchPosition (
59+ TypeParam typeParam , TypeParamTypeParameter param , TypeParameterPosition ppos
60+ ) {
61+ typeParam = param .getTypeParam ( ) and typeParam = ppos .asTypeParam ( )
62+ }
4263
4364 bindingset [ apos]
4465 bindingset [ ppos]
4566 predicate typeArgumentParameterPositionMatch ( TypeArgumentPosition apos , TypeParameterPosition ppos ) {
46- apos .asTypeParam ( ) = ppos
67+ apos .asTypeParam ( ) = ppos . asTypeParam ( )
4768 or
48- apos .asMethodTypeArgumentPosition ( ) = ppos .getPosition ( )
69+ apos .asMethodTypeArgumentPosition ( ) = ppos .asTypeParam ( ) . getPosition ( )
4970 }
5071
51- private predicate id ( Raw:: TypeParam x , Raw:: TypeParam y ) { x = y }
72+ /** A raw AST node that might correspond to a type parameter. */
73+ private class RawTypeParameter = @type_param or @trait;
74+
75+ private predicate id ( RawTypeParameter x , RawTypeParameter y ) { x = y }
5276
53- private predicate idOfRaw ( Raw :: TypeParam x , int y ) = equivalenceRelation( id / 2 ) ( x , y )
77+ private predicate idOfRaw ( RawTypeParameter x , int y ) = equivalenceRelation( id / 2 ) ( x , y )
5478
55- private int idOf ( TypeParam node ) { idOfRaw ( Synth:: convertAstNodeToRaw ( node ) , result ) }
79+ private int idOf ( AstNode node ) { idOfRaw ( Synth:: convertAstNodeToRaw ( node ) , result ) }
5680
5781 int getTypeParameterId ( TypeParameter tp ) {
5882 tp =
@@ -61,12 +85,11 @@ private module Input1 implements InputSig1<Location> {
6185 kind = 0 and
6286 id = 0
6387 or
64- tp0 instanceof SelfTypeParameter and
65- kind = 0 and
66- id = 1
67- or
68- id = idOf ( tp0 .( TypeParamTypeParameter ) .getTypeParam ( ) ) and
69- kind = 1
88+ kind = 1 and
89+ exists ( AstNode node | id = idOf ( node ) |
90+ node = tp0 .( TypeParamTypeParameter ) .getTypeParam ( ) or
91+ node = tp0 .( SelfTypeParameter ) .getTrait ( )
92+ )
7093 |
7194 tp0 order by kind , id
7295 )
@@ -211,15 +234,6 @@ private Type inferImplSelfType(Impl i, TypePath path) {
211234 result = i .getSelfTy ( ) .( TypeReprMention ) .resolveTypeAt ( path )
212235}
213236
214- pragma [ nomagic]
215- private Type inferTraitSelfType ( Trait t , TypePath path ) {
216- result = TTrait ( t ) and
217- path .isEmpty ( )
218- or
219- result = TTypeParamTypeParameter ( t .getGenericParamList ( ) .getATypeParam ( ) ) and
220- path = TypePath:: singleton ( result )
221- }
222-
223237/** Gets the type at `path` of the implicitly typed `self` parameter. */
224238pragma [ nomagic]
225239private Type inferImplicitSelfType ( SelfParam self , TypePath path ) {
@@ -230,7 +244,7 @@ private Type inferImplicitSelfType(SelfParam self, TypePath path) {
230244 |
231245 t = inferImplSelfType ( i , suffix )
232246 or
233- t = inferTraitSelfType ( i , suffix )
247+ t = TSelfTypeParameter ( i ) and suffix . isEmpty ( )
234248 )
235249}
236250
@@ -273,8 +287,7 @@ private module StructExprMatchingInput implements MatchingInputSig {
273287 abstract TypeParam getATypeParam ( ) ;
274288
275289 final TypeParameter getTypeParameter ( TypeParameterPosition ppos ) {
276- result .( TypeParamTypeParameter ) .getTypeParam ( ) = ppos and
277- ppos = this .getATypeParam ( )
290+ typeParamMatchPosition ( this .getATypeParam ( ) , result , ppos )
278291 }
279292
280293 abstract StructField getField ( string name ) ;
@@ -417,12 +430,7 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
417430 }
418431
419432 abstract class Declaration extends AstNode {
420- abstract TypeParam getATypeParam ( ) ;
421-
422- final TypeParameter getTypeParameter ( TypeParameterPosition ppos ) {
423- result .( TypeParamTypeParameter ) .getTypeParam ( ) = ppos and
424- ppos = this .getATypeParam ( )
425- }
433+ abstract TypeParameter getTypeParameter ( TypeParameterPosition ppos ) ;
426434
427435 pragma [ nomagic]
428436 abstract Type getParameterType ( DeclarationPosition dpos , TypePath path ) ;
@@ -440,7 +448,9 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
440448 private class TupleStructDecl extends Declaration , Struct {
441449 TupleStructDecl ( ) { this .isTuple ( ) }
442450
443- override TypeParam getATypeParam ( ) { result = this .getGenericParamList ( ) .getATypeParam ( ) }
451+ override TypeParameter getTypeParameter ( TypeParameterPosition ppos ) {
452+ typeParamMatchPosition ( this .getGenericParamList ( ) .getATypeParam ( ) , result , ppos )
453+ }
444454
445455 override Type getParameterType ( DeclarationPosition dpos , TypePath path ) {
446456 exists ( int pos |
@@ -461,8 +471,8 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
461471 private class TupleVariantDecl extends Declaration , Variant {
462472 TupleVariantDecl ( ) { this .isTuple ( ) }
463473
464- override TypeParam getATypeParam ( ) {
465- result = this .getEnum ( ) .getGenericParamList ( ) .getATypeParam ( )
474+ override TypeParameter getTypeParameter ( TypeParameterPosition ppos ) {
475+ typeParamMatchPosition ( this .getEnum ( ) .getGenericParamList ( ) .getATypeParam ( ) , result , ppos )
466476 }
467477
468478 override Type getParameterType ( DeclarationPosition dpos , TypePath path ) {
@@ -483,38 +493,36 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
483493 }
484494 }
485495
486- pragma [ nomagic]
487- private Type inferAnnotatedTypeInclSelf ( AstNode n , TypePath path ) {
488- result = getTypeAnnotation ( n ) .resolveTypeAtInclSelf ( path )
489- }
490-
491496 private class FunctionDecl extends Declaration , Function {
492- override TypeParam getATypeParam ( ) { result = this .getGenericParamList ( ) .getATypeParam ( ) }
497+ override TypeParameter getTypeParameter ( TypeParameterPosition ppos ) {
498+ typeParamMatchPosition ( this .getGenericParamList ( ) .getATypeParam ( ) , result , ppos )
499+ or
500+ exists ( TraitItemNode trait | this = trait .getAnAssocItem ( ) |
501+ typeParamMatchPosition ( trait .getTypeParam ( _) , result , ppos )
502+ or
503+ ppos .isSelf ( ) and result = TSelfTypeParameter ( trait )
504+ )
505+ }
493506
494507 override Type getParameterType ( DeclarationPosition dpos , TypePath path ) {
495508 exists ( Param p , int i , boolean inMethod |
496509 paramPos ( this .getParamList ( ) , p , i , inMethod ) and
497510 dpos = TPositionalDeclarationPosition ( i , inMethod ) and
498- result = inferAnnotatedTypeInclSelf ( p .getPat ( ) , path )
511+ result = inferAnnotatedType ( p .getPat ( ) , path )
499512 )
500513 or
501514 exists ( SelfParam self |
502515 self = pragma [ only_bind_into ] ( this .getParamList ( ) .getSelfParam ( ) ) and
503516 dpos .isSelf ( )
504517 |
505- // `self` parameter with type annotation
506- result = inferAnnotatedTypeInclSelf ( self , path )
507- or
508- // `self` parameter without type annotation
509- result = inferImplicitSelfType ( self , path )
518+ result = inferAnnotatedType ( self , path ) // `self` parameter with type annotation
510519 or
511- // `self` parameter without type annotation should also have the special `Self` type
512- result = getRefAdjustImplicitSelfType ( self , TypePath:: nil ( ) , TSelfTypeParameter ( ) , path )
520+ result = inferImplicitSelfType ( self , path ) // `self` parameter without type annotation
513521 )
514522 }
515523
516524 override Type getReturnType ( TypePath path ) {
517- result = this .getRetType ( ) .getTypeRepr ( ) .( TypeReprMention ) .resolveTypeAtInclSelf ( path )
525+ result = this .getRetType ( ) .getTypeRepr ( ) .( TypeReprMention ) .resolveTypeAt ( path )
518526 }
519527 }
520528
0 commit comments