@@ -38,21 +38,42 @@ private module Input1 implements InputSig1<Location> {
38
38
}
39
39
}
40
40
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
+ }
42
63
43
64
bindingset [ apos]
44
65
bindingset [ ppos]
45
66
predicate typeArgumentParameterPositionMatch ( TypeArgumentPosition apos , TypeParameterPosition ppos ) {
46
- apos .asTypeParam ( ) = ppos
67
+ apos .asTypeParam ( ) = ppos . asTypeParam ( )
47
68
or
48
- apos .asMethodTypeArgumentPosition ( ) = ppos .getPosition ( )
69
+ apos .asMethodTypeArgumentPosition ( ) = ppos .asTypeParam ( ) . getPosition ( )
49
70
}
50
71
51
- private predicate id ( Raw:: TypeParam x , Raw:: TypeParam y ) { x = y }
72
+ private predicate id ( Raw:: AstNode x , Raw:: AstNode y ) { x = y }
52
73
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 )
54
75
55
- private int idOf ( TypeParam node ) { idOfRaw ( Synth:: convertAstNodeToRaw ( node ) , result ) }
76
+ private int idOf ( AstNode node ) { idOfRaw ( Synth:: convertAstNodeToRaw ( node ) , result ) }
56
77
57
78
int getTypeParameterId ( TypeParameter tp ) {
58
79
tp =
@@ -61,12 +82,11 @@ private module Input1 implements InputSig1<Location> {
61
82
kind = 0 and
62
83
id = 0
63
84
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
85
+ kind = 1 and
86
+ exists ( AstNode node | id = idOf ( node ) |
87
+ node = tp0 .( TypeParamTypeParameter ) .getTypeParam ( ) or
88
+ node = tp0 .( SelfTypeParameter ) .getTrait ( )
89
+ )
70
90
|
71
91
tp0 order by kind , id
72
92
)
@@ -211,15 +231,6 @@ private Type inferImplSelfType(Impl i, TypePath path) {
211
231
result = i .getSelfTy ( ) .( TypeReprMention ) .resolveTypeAt ( path )
212
232
}
213
233
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
-
223
234
/** Gets the type at `path` of the implicitly typed `self` parameter. */
224
235
pragma [ nomagic]
225
236
private Type inferImplicitSelfType ( SelfParam self , TypePath path ) {
@@ -230,7 +241,7 @@ private Type inferImplicitSelfType(SelfParam self, TypePath path) {
230
241
|
231
242
t = inferImplSelfType ( i , suffix )
232
243
or
233
- t = inferTraitSelfType ( i , suffix )
244
+ t = TSelfTypeParameter ( i ) and suffix . isEmpty ( )
234
245
)
235
246
}
236
247
@@ -273,8 +284,7 @@ private module StructExprMatchingInput implements MatchingInputSig {
273
284
abstract TypeParam getATypeParam ( ) ;
274
285
275
286
final TypeParameter getTypeParameter ( TypeParameterPosition ppos ) {
276
- result .( TypeParamTypeParameter ) .getTypeParam ( ) = ppos and
277
- ppos = this .getATypeParam ( )
287
+ typeParamMatchPosition ( this .getATypeParam ( ) , result , ppos )
278
288
}
279
289
280
290
abstract StructField getField ( string name ) ;
@@ -417,12 +427,7 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
417
427
}
418
428
419
429
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
- }
430
+ abstract TypeParameter getTypeParameter ( TypeParameterPosition ppos ) ;
426
431
427
432
pragma [ nomagic]
428
433
abstract Type getParameterType ( DeclarationPosition dpos , TypePath path ) ;
@@ -440,7 +445,9 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
440
445
private class TupleStructDecl extends Declaration , Struct {
441
446
TupleStructDecl ( ) { this .isTuple ( ) }
442
447
443
- override TypeParam getATypeParam ( ) { result = this .getGenericParamList ( ) .getATypeParam ( ) }
448
+ override TypeParameter getTypeParameter ( TypeParameterPosition ppos ) {
449
+ typeParamMatchPosition ( this .getGenericParamList ( ) .getATypeParam ( ) , result , ppos )
450
+ }
444
451
445
452
override Type getParameterType ( DeclarationPosition dpos , TypePath path ) {
446
453
exists ( int pos |
@@ -461,8 +468,8 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
461
468
private class TupleVariantDecl extends Declaration , Variant {
462
469
TupleVariantDecl ( ) { this .isTuple ( ) }
463
470
464
- override TypeParam getATypeParam ( ) {
465
- result = this .getEnum ( ) .getGenericParamList ( ) .getATypeParam ( )
471
+ override TypeParameter getTypeParameter ( TypeParameterPosition ppos ) {
472
+ typeParamMatchPosition ( this .getEnum ( ) .getGenericParamList ( ) .getATypeParam ( ) , result , ppos )
466
473
}
467
474
468
475
override Type getParameterType ( DeclarationPosition dpos , TypePath path ) {
@@ -483,38 +490,36 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
483
490
}
484
491
}
485
492
486
- pragma [ nomagic]
487
- private Type inferAnnotatedTypeInclSelf ( AstNode n , TypePath path ) {
488
- result = getTypeAnnotation ( n ) .resolveTypeAtInclSelf ( path )
489
- }
490
-
491
493
private class FunctionDecl extends Declaration , Function {
492
- override TypeParam getATypeParam ( ) { result = this .getGenericParamList ( ) .getATypeParam ( ) }
494
+ override TypeParameter getTypeParameter ( TypeParameterPosition ppos ) {
495
+ typeParamMatchPosition ( this .getGenericParamList ( ) .getATypeParam ( ) , result , ppos )
496
+ or
497
+ exists ( TraitItemNode trait | this = trait .getAnAssocItem ( ) |
498
+ typeParamMatchPosition ( trait .getTypeParam ( _) , result , ppos )
499
+ or
500
+ ppos .isSelf ( ) and result = TSelfTypeParameter ( trait )
501
+ )
502
+ }
493
503
494
504
override Type getParameterType ( DeclarationPosition dpos , TypePath path ) {
495
505
exists ( Param p , int i , boolean inMethod |
496
506
paramPos ( this .getParamList ( ) , p , i , inMethod ) and
497
507
dpos = TPositionalDeclarationPosition ( i , inMethod ) and
498
- result = inferAnnotatedTypeInclSelf ( p .getPat ( ) , path )
508
+ result = inferAnnotatedType ( p .getPat ( ) , path )
499
509
)
500
510
or
501
511
exists ( SelfParam self |
502
512
self = pragma [ only_bind_into ] ( this .getParamList ( ) .getSelfParam ( ) ) and
503
513
dpos .isSelf ( )
504
514
|
505
- // `self` parameter with type annotation
506
- result = inferAnnotatedTypeInclSelf ( self , path )
507
- or
508
- // `self` parameter without type annotation
509
- result = inferImplicitSelfType ( self , path )
515
+ result = inferAnnotatedType ( self , path ) // `self` parameter with type annotation
510
516
or
511
- // `self` parameter without type annotation should also have the special `Self` type
512
- result = getRefAdjustImplicitSelfType ( self , TypePath:: nil ( ) , TSelfTypeParameter ( ) , path )
517
+ result = inferImplicitSelfType ( self , path ) // `self` parameter without type annotation
513
518
)
514
519
}
515
520
516
521
override Type getReturnType ( TypePath path ) {
517
- result = this .getRetType ( ) .getTypeRepr ( ) .( TypeReprMention ) .resolveTypeAtInclSelf ( path )
522
+ result = this .getRetType ( ) .getTypeRepr ( ) .( TypeReprMention ) .resolveTypeAt ( path )
518
523
}
519
524
}
520
525
0 commit comments