@@ -38,21 +38,42 @@ 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+ private predicate id ( Raw:: AstNode x , Raw:: AstNode y ) { x = y }
5273
53- private predicate idOfRaw ( Raw:: TypeParam x , int y ) = equivalenceRelation( id / 2 ) ( x , y )
74+ private predicate idOfRaw ( Raw:: AstNode x , int y ) = equivalenceRelation( id / 2 ) ( x , y )
5475
55- private int idOf ( TypeParam node ) { idOfRaw ( Synth:: convertAstNodeToRaw ( node ) , result ) }
76+ private int idOf ( AstNode node ) { idOfRaw ( Synth:: convertAstNodeToRaw ( node ) , result ) }
5677
5778 int getTypeParameterId ( TypeParameter tp ) {
5879 tp =
@@ -65,8 +86,11 @@ private module Input1 implements InputSig1<Location> {
6586 kind = 0 and
6687 id = 1
6788 or
68- id = idOf ( tp0 .( TypeParamTypeParameter ) .getTypeParam ( ) ) and
69- kind = 1
89+ kind = 1 and
90+ exists ( AstNode node | id = idOf ( node ) |
91+ node = tp0 .( TypeParamTypeParameter ) .getTypeParam ( ) or
92+ node = tp0 .( SelfTypeParameter ) .getTrait ( )
93+ )
7094 |
7195 tp0 order by kind , id
7296 )
@@ -211,15 +235,6 @@ private Type inferImplSelfType(Impl i, TypePath path) {
211235 result = i .getSelfTy ( ) .( TypeReprMention ) .resolveTypeAt ( path )
212236}
213237
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-
223238/** Gets the type at `path` of the implicitly typed `self` parameter. */
224239pragma [ nomagic]
225240private Type inferImplicitSelfType ( SelfParam self , TypePath path ) {
@@ -230,7 +245,7 @@ private Type inferImplicitSelfType(SelfParam self, TypePath path) {
230245 |
231246 t = inferImplSelfType ( i , suffix )
232247 or
233- t = inferTraitSelfType ( i , suffix )
248+ t = TSelfTypeParameter ( i ) and suffix . isEmpty ( )
234249 )
235250}
236251
@@ -273,8 +288,7 @@ private module StructExprMatchingInput implements MatchingInputSig {
273288 abstract TypeParam getATypeParam ( ) ;
274289
275290 final TypeParameter getTypeParameter ( TypeParameterPosition ppos ) {
276- result .( TypeParamTypeParameter ) .getTypeParam ( ) = ppos and
277- ppos = this .getATypeParam ( )
291+ typeParamMatchPosition ( this .getATypeParam ( ) , result , ppos )
278292 }
279293
280294 abstract StructField getField ( string name ) ;
@@ -417,12 +431,7 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
417431 }
418432
419433 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- }
434+ abstract TypeParameter getTypeParameter ( TypeParameterPosition ppos ) ;
426435
427436 pragma [ nomagic]
428437 abstract Type getParameterType ( DeclarationPosition dpos , TypePath path ) ;
@@ -440,7 +449,9 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
440449 private class TupleStructDecl extends Declaration , Struct {
441450 TupleStructDecl ( ) { this .isTuple ( ) }
442451
443- override TypeParam getATypeParam ( ) { result = this .getGenericParamList ( ) .getATypeParam ( ) }
452+ override TypeParameter getTypeParameter ( TypeParameterPosition ppos ) {
453+ typeParamMatchPosition ( this .getGenericParamList ( ) .getATypeParam ( ) , result , ppos )
454+ }
444455
445456 override Type getParameterType ( DeclarationPosition dpos , TypePath path ) {
446457 exists ( int pos |
@@ -461,8 +472,8 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
461472 private class TupleVariantDecl extends Declaration , Variant {
462473 TupleVariantDecl ( ) { this .isTuple ( ) }
463474
464- override TypeParam getATypeParam ( ) {
465- result = this .getEnum ( ) .getGenericParamList ( ) .getATypeParam ( )
475+ override TypeParameter getTypeParameter ( TypeParameterPosition ppos ) {
476+ typeParamMatchPosition ( this .getEnum ( ) .getGenericParamList ( ) .getATypeParam ( ) , result , ppos )
466477 }
467478
468479 override Type getParameterType ( DeclarationPosition dpos , TypePath path ) {
@@ -483,38 +494,36 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
483494 }
484495 }
485496
486- pragma [ nomagic]
487- private Type inferAnnotatedTypeInclSelf ( AstNode n , TypePath path ) {
488- result = getTypeAnnotation ( n ) .resolveTypeAtInclSelf ( path )
489- }
490-
491497 private class FunctionDecl extends Declaration , Function {
492- override TypeParam getATypeParam ( ) { result = this .getGenericParamList ( ) .getATypeParam ( ) }
498+ override TypeParameter getTypeParameter ( TypeParameterPosition ppos ) {
499+ typeParamMatchPosition ( this .getGenericParamList ( ) .getATypeParam ( ) , result , ppos )
500+ or
501+ exists ( TraitItemNode trait | this = trait .getAnAssocItem ( ) |
502+ typeParamMatchPosition ( trait .getTypeParam ( _) , result , ppos )
503+ or
504+ ppos .isSelf ( ) and result = TSelfTypeParameter ( trait )
505+ )
506+ }
493507
494508 override Type getParameterType ( DeclarationPosition dpos , TypePath path ) {
495509 exists ( Param p , int i , boolean inMethod |
496510 paramPos ( this .getParamList ( ) , p , i , inMethod ) and
497511 dpos = TPositionalDeclarationPosition ( i , inMethod ) and
498- result = inferAnnotatedTypeInclSelf ( p .getPat ( ) , path )
512+ result = inferAnnotatedType ( p .getPat ( ) , path )
499513 )
500514 or
501515 exists ( SelfParam self |
502516 self = pragma [ only_bind_into ] ( this .getParamList ( ) .getSelfParam ( ) ) and
503517 dpos .isSelf ( )
504518 |
505- // `self` parameter with type annotation
506- result = inferAnnotatedTypeInclSelf ( self , path )
507- or
508- // `self` parameter without type annotation
509- result = inferImplicitSelfType ( self , path )
519+ result = inferAnnotatedType ( self , path ) // `self` parameter with type annotation
510520 or
511- // `self` parameter without type annotation should also have the special `Self` type
512- result = getRefAdjustImplicitSelfType ( self , TypePath:: nil ( ) , TSelfTypeParameter ( ) , path )
521+ result = inferImplicitSelfType ( self , path ) // `self` parameter without type annotation
513522 )
514523 }
515524
516525 override Type getReturnType ( TypePath path ) {
517- result = this .getRetType ( ) .getTypeRepr ( ) .( TypeReprMention ) .resolveTypeAtInclSelf ( path )
526+ result = this .getRetType ( ) .getTypeRepr ( ) .( TypeReprMention ) .resolveTypeAt ( path )
518527 }
519528 }
520529
0 commit comments