Skip to content

Commit cd8a5d7

Browse files
authored
Merge pull request #17378 from github/mbg/go/improve-typeparamtype
Go: Add `getParent` and `getIndex` for `TypeParamType`
2 parents 8508056 + db72bd4 commit cd8a5d7

File tree

4 files changed

+175
-34
lines changed

4 files changed

+175
-34
lines changed

go/ql/lib/semmle/go/Scopes.qll

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,11 @@ class PackageEntity extends Entity, @pkgobject { }
197197
/** A built-in or declared named type. */
198198
class TypeEntity extends Entity, @typeobject { }
199199

200+
/** The parent of a type parameter type, either a declared type or a declared function. */
201+
class TypeParamParentEntity extends Entity, @typeparamparentobject { }
202+
200203
/** A declared named type. */
201-
class DeclaredType extends TypeEntity, DeclaredEntity, @decltypeobject {
204+
class DeclaredType extends TypeEntity, DeclaredEntity, TypeParamParentEntity, @decltypeobject {
202205
/** Gets the declaration specifier declaring this type. */
203206
TypeSpec getSpec() { result.getNameExpr() = this.getDeclaration() }
204207
}
@@ -598,7 +601,7 @@ class PromotedMethod extends Method {
598601
}
599602

600603
/** A declared function. */
601-
class DeclaredFunction extends Function, DeclaredEntity, @declfunctionobject {
604+
class DeclaredFunction extends Function, DeclaredEntity, TypeParamParentEntity, @declfunctionobject {
602605
override FuncDecl getFuncDecl() { result.getNameExpr() = this.getDeclaration() }
603606

604607
override predicate mayHaveSideEffects() {

go/ql/lib/semmle/go/Types.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,12 @@ class TypeParamType extends @typeparamtype, CompositeType {
381381

382382
override InterfaceType getUnderlyingType() { result = this.getConstraint().getUnderlyingType() }
383383

384+
/** Gets the parent object of this type parameter type. */
385+
TypeParamParentEntity getParent() { typeparam(this, _, _, result, _) }
386+
387+
/** Gets the index of this type parameter type. */
388+
int getIndex() { typeparam(this, _, _, _, result) }
389+
384390
override string pp() { result = this.getParamName() }
385391

386392
/**
Lines changed: 161 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,161 @@
1-
| E | Ordered |
2-
| E | comparable |
3-
| E | interface { } |
4-
| E1 | interface { } |
5-
| E2 | interface { } |
6-
| Edge | EdgeConstraint |
7-
| Edge | interface { } |
8-
| F | floaty |
9-
| K | comparable |
10-
| Node | NodeConstraint |
11-
| Node | interface { } |
12-
| S | interface { } |
13-
| S | interface { ~[]E } |
14-
| S1 | interface { ~[]E1 } |
15-
| S2 | interface { ~[]E2 } |
16-
| SF2 | interface { } |
17-
| SG2 | interface { } |
18-
| T | Ordered |
19-
| T | comparable |
20-
| T | interface { string \| []uint8 } |
21-
| T | interface { } |
22-
| T1 | interface { } |
23-
| T2 | interface { } |
24-
| TF1 | interface { } |
25-
| TF2 | interface { } |
26-
| TG1 | interface { } |
27-
| TG2 | interface { } |
28-
| U | interface { } |
29-
| V | interface { int64 \| float64 } |
30-
| bytes | interface { []uint8 \| string } |
1+
| cmp.Compare | 0 | T | Ordered |
2+
| cmp.Less | 0 | T | Ordered |
3+
| cmp.Or | 0 | T | comparable |
4+
| cmp.isNaN | 0 | T | Ordered |
5+
| codeql-go-tests/function.EdgeConstraint | 0 | Node | interface { } |
6+
| codeql-go-tests/function.Element | 0 | S | interface { } |
7+
| codeql-go-tests/function.GenericFunctionInAnotherFile | 0 | T | interface { } |
8+
| codeql-go-tests/function.GenericFunctionOneTypeParam | 0 | T | interface { } |
9+
| codeql-go-tests/function.GenericFunctionTwoTypeParams | 0 | K | comparable |
10+
| codeql-go-tests/function.GenericFunctionTwoTypeParams | 1 | V | interface { int64 \| float64 } |
11+
| codeql-go-tests/function.GenericStruct1 | 0 | T | interface { } |
12+
| codeql-go-tests/function.GenericStruct1.f1 | 0 | TF1 | interface { } |
13+
| codeql-go-tests/function.GenericStruct1.g1 | 0 | TG1 | interface { } |
14+
| codeql-go-tests/function.GenericStruct2 | 0 | S | interface { } |
15+
| codeql-go-tests/function.GenericStruct2 | 1 | T | interface { } |
16+
| codeql-go-tests/function.GenericStruct2.f2 | 0 | SF2 | interface { } |
17+
| codeql-go-tests/function.GenericStruct2.f2 | 1 | TF2 | interface { } |
18+
| codeql-go-tests/function.GenericStruct2.g2 | 0 | SG2 | interface { } |
19+
| codeql-go-tests/function.GenericStruct2.g2 | 1 | TG2 | interface { } |
20+
| codeql-go-tests/function.Graph | 0 | Node | NodeConstraint |
21+
| codeql-go-tests/function.Graph | 1 | Edge | EdgeConstraint |
22+
| codeql-go-tests/function.Graph.ShortestPath | 0 | Node | NodeConstraint |
23+
| codeql-go-tests/function.Graph.ShortestPath | 1 | Edge | EdgeConstraint |
24+
| codeql-go-tests/function.List | 0 | T | interface { } |
25+
| codeql-go-tests/function.List.MyLen | 0 | U | interface { } |
26+
| codeql-go-tests/function.New | 0 | Node | NodeConstraint |
27+
| codeql-go-tests/function.New | 1 | Edge | EdgeConstraint |
28+
| codeql-go-tests/function.NodeConstraint | 0 | Edge | interface { } |
29+
| github.com/anotherpkg.GenericFunctionInAnotherPackage | 0 | T | interface { } |
30+
| internal/bytealg.HashStr | 0 | T | interface { string \| []uint8 } |
31+
| internal/bytealg.HashStrRev | 0 | T | interface { string \| []uint8 } |
32+
| internal/bytealg.IndexRabinKarp | 0 | T | interface { string \| []uint8 } |
33+
| internal/bytealg.LastIndexRabinKarp | 0 | T | interface { string \| []uint8 } |
34+
| runtime.fandbits | 0 | F | floaty |
35+
| runtime.fmax | 0 | F | floaty |
36+
| runtime.fmin | 0 | F | floaty |
37+
| runtime.forbits | 0 | F | floaty |
38+
| runtime.noEscapePtr | 0 | T | interface { } |
39+
| runtime/internal/atomic.Pointer.CompareAndSwap | 0 | T | interface { } |
40+
| runtime/internal/atomic.Pointer.CompareAndSwapNoWB | 0 | T | interface { } |
41+
| runtime/internal/atomic.Pointer.Load | 0 | T | interface { } |
42+
| runtime/internal/atomic.Pointer.Store | 0 | T | interface { } |
43+
| runtime/internal/atomic.Pointer.StoreNoWB | 0 | T | interface { } |
44+
| slices.BinarySearch | 0 | S | interface { ~[]E } |
45+
| slices.BinarySearch | 1 | E | Ordered |
46+
| slices.BinarySearchFunc | 0 | S | interface { ~[]E } |
47+
| slices.BinarySearchFunc | 1 | E | interface { } |
48+
| slices.BinarySearchFunc | 2 | T | interface { } |
49+
| slices.Clip | 0 | S | interface { ~[]E } |
50+
| slices.Clip | 1 | E | interface { } |
51+
| slices.Clone | 0 | S | interface { ~[]E } |
52+
| slices.Clone | 1 | E | interface { } |
53+
| slices.Compact | 0 | S | interface { ~[]E } |
54+
| slices.Compact | 1 | E | comparable |
55+
| slices.CompactFunc | 0 | S | interface { ~[]E } |
56+
| slices.CompactFunc | 1 | E | interface { } |
57+
| slices.Compare | 0 | S | interface { ~[]E } |
58+
| slices.Compare | 1 | E | Ordered |
59+
| slices.CompareFunc | 0 | S1 | interface { ~[]E1 } |
60+
| slices.CompareFunc | 1 | S2 | interface { ~[]E2 } |
61+
| slices.CompareFunc | 2 | E1 | interface { } |
62+
| slices.CompareFunc | 3 | E2 | interface { } |
63+
| slices.Concat | 0 | S | interface { ~[]E } |
64+
| slices.Concat | 1 | E | interface { } |
65+
| slices.Contains | 0 | S | interface { ~[]E } |
66+
| slices.Contains | 1 | E | comparable |
67+
| slices.ContainsFunc | 0 | S | interface { ~[]E } |
68+
| slices.ContainsFunc | 1 | E | interface { } |
69+
| slices.Delete | 0 | S | interface { ~[]E } |
70+
| slices.Delete | 1 | E | interface { } |
71+
| slices.DeleteFunc | 0 | S | interface { ~[]E } |
72+
| slices.DeleteFunc | 1 | E | interface { } |
73+
| slices.Equal | 0 | S | interface { ~[]E } |
74+
| slices.Equal | 1 | E | comparable |
75+
| slices.EqualFunc | 0 | S1 | interface { ~[]E1 } |
76+
| slices.EqualFunc | 1 | S2 | interface { ~[]E2 } |
77+
| slices.EqualFunc | 2 | E1 | interface { } |
78+
| slices.EqualFunc | 3 | E2 | interface { } |
79+
| slices.Grow | 0 | S | interface { ~[]E } |
80+
| slices.Grow | 1 | E | interface { } |
81+
| slices.Index | 0 | S | interface { ~[]E } |
82+
| slices.Index | 1 | E | comparable |
83+
| slices.IndexFunc | 0 | S | interface { ~[]E } |
84+
| slices.IndexFunc | 1 | E | interface { } |
85+
| slices.Insert | 0 | S | interface { ~[]E } |
86+
| slices.Insert | 1 | E | interface { } |
87+
| slices.IsSorted | 0 | S | interface { ~[]E } |
88+
| slices.IsSorted | 1 | E | Ordered |
89+
| slices.IsSortedFunc | 0 | S | interface { ~[]E } |
90+
| slices.IsSortedFunc | 1 | E | interface { } |
91+
| slices.Max | 0 | S | interface { ~[]E } |
92+
| slices.Max | 1 | E | Ordered |
93+
| slices.MaxFunc | 0 | S | interface { ~[]E } |
94+
| slices.MaxFunc | 1 | E | interface { } |
95+
| slices.Min | 0 | S | interface { ~[]E } |
96+
| slices.Min | 1 | E | Ordered |
97+
| slices.MinFunc | 0 | S | interface { ~[]E } |
98+
| slices.MinFunc | 1 | E | interface { } |
99+
| slices.Replace | 0 | S | interface { ~[]E } |
100+
| slices.Replace | 1 | E | interface { } |
101+
| slices.Reverse | 0 | S | interface { ~[]E } |
102+
| slices.Reverse | 1 | E | interface { } |
103+
| slices.Sort | 0 | S | interface { ~[]E } |
104+
| slices.Sort | 1 | E | Ordered |
105+
| slices.SortFunc | 0 | S | interface { ~[]E } |
106+
| slices.SortFunc | 1 | E | interface { } |
107+
| slices.SortStableFunc | 0 | S | interface { ~[]E } |
108+
| slices.SortStableFunc | 1 | E | interface { } |
109+
| slices.breakPatternsCmpFunc | 0 | E | interface { } |
110+
| slices.breakPatternsOrdered | 0 | E | Ordered |
111+
| slices.choosePivotCmpFunc | 0 | E | interface { } |
112+
| slices.choosePivotOrdered | 0 | E | Ordered |
113+
| slices.heapSortCmpFunc | 0 | E | interface { } |
114+
| slices.heapSortOrdered | 0 | E | Ordered |
115+
| slices.insertionSortCmpFunc | 0 | E | interface { } |
116+
| slices.insertionSortOrdered | 0 | E | Ordered |
117+
| slices.isNaN | 0 | T | Ordered |
118+
| slices.medianAdjacentCmpFunc | 0 | E | interface { } |
119+
| slices.medianAdjacentOrdered | 0 | E | Ordered |
120+
| slices.medianCmpFunc | 0 | E | interface { } |
121+
| slices.medianOrdered | 0 | E | Ordered |
122+
| slices.order2CmpFunc | 0 | E | interface { } |
123+
| slices.order2Ordered | 0 | E | Ordered |
124+
| slices.overlaps | 0 | E | interface { } |
125+
| slices.partialInsertionSortCmpFunc | 0 | E | interface { } |
126+
| slices.partialInsertionSortOrdered | 0 | E | Ordered |
127+
| slices.partitionCmpFunc | 0 | E | interface { } |
128+
| slices.partitionEqualCmpFunc | 0 | E | interface { } |
129+
| slices.partitionEqualOrdered | 0 | E | Ordered |
130+
| slices.partitionOrdered | 0 | E | Ordered |
131+
| slices.pdqsortCmpFunc | 0 | E | interface { } |
132+
| slices.pdqsortOrdered | 0 | E | Ordered |
133+
| slices.reverseRangeCmpFunc | 0 | E | interface { } |
134+
| slices.reverseRangeOrdered | 0 | E | Ordered |
135+
| slices.rotateCmpFunc | 0 | E | interface { } |
136+
| slices.rotateLeft | 0 | E | interface { } |
137+
| slices.rotateOrdered | 0 | E | Ordered |
138+
| slices.rotateRight | 0 | E | interface { } |
139+
| slices.siftDownCmpFunc | 0 | E | interface { } |
140+
| slices.siftDownOrdered | 0 | E | Ordered |
141+
| slices.stableCmpFunc | 0 | E | interface { } |
142+
| slices.stableOrdered | 0 | E | Ordered |
143+
| slices.startIdx | 0 | E | interface { } |
144+
| slices.swap | 0 | E | interface { } |
145+
| slices.swapRangeCmpFunc | 0 | E | interface { } |
146+
| slices.swapRangeOrdered | 0 | E | Ordered |
147+
| slices.symMergeCmpFunc | 0 | E | interface { } |
148+
| slices.symMergeOrdered | 0 | E | Ordered |
149+
| sync.OnceValue | 0 | T | interface { } |
150+
| sync.OnceValues | 0 | T1 | interface { } |
151+
| sync.OnceValues | 1 | T2 | interface { } |
152+
| sync/atomic.Pointer | 0 | T | interface { } |
153+
| sync/atomic.Pointer.CompareAndSwap | 0 | T | interface { } |
154+
| sync/atomic.Pointer.Load | 0 | T | interface { } |
155+
| sync/atomic.Pointer.Store | 0 | T | interface { } |
156+
| sync/atomic.Pointer.Swap | 0 | T | interface { } |
157+
| time.atoi | 0 | bytes | interface { []uint8 \| string } |
158+
| time.isDigit | 0 | bytes | interface { []uint8 \| string } |
159+
| time.leadingInt | 0 | bytes | interface { []uint8 \| string } |
160+
| time.parseNanoseconds | 0 | bytes | interface { []uint8 \| string } |
161+
| time.parseRFC3339 | 0 | bytes | interface { []uint8 \| string } |
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import go
22

3-
from TypeParamType tpt
4-
select tpt.getParamName(), tpt.getConstraint().pp()
3+
from TypeParamType tpt, TypeParamParentEntity ty
4+
where ty = tpt.getParent()
5+
select ty.getQualifiedName(), tpt.getIndex(), tpt.getParamName(), tpt.getConstraint().pp()

0 commit comments

Comments
 (0)