Skip to content

Commit 076dd07

Browse files
authored
Merge pull request github#17429 from owen-mc/go/fix/multiple-anonymous-type-parameters
Go: fix multiple anonymous type parameters
2 parents f9e4c0a + 13f8488 commit 076dd07

File tree

7 files changed

+55
-1
lines changed

7 files changed

+55
-1
lines changed

go/extractor/extractor.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1776,7 +1776,8 @@ func getTypeLabel(tw *trap.Writer, tp types.Type) (trap.Label, bool) {
17761776
lbl = tw.Labeler.GlobalID(fmt.Sprintf("{%s};namedtype", entitylbl))
17771777
case *types.TypeParam:
17781778
parentlbl := getTypeParamParentLabel(tw, tp)
1779-
lbl = tw.Labeler.GlobalID(fmt.Sprintf("{%v},%s;typeparamtype", parentlbl, tp.Obj().Name()))
1779+
idx := tp.Index()
1780+
lbl = tw.Labeler.GlobalID(fmt.Sprintf("{%v},%d,%s;typeparamtype", parentlbl, idx, tp.Obj().Name()))
17801781
case *types.Union:
17811782
var b strings.Builder
17821783
for i := 0; i < tp.Len(); i++ {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* When a function or type has more than one anonymous type parameters, they were mistakenly being treated as the same type parameter. This has now been fixed.

go/ql/test/library-tests/semmle/go/Function/TypeParamType.expected

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
numberOfTypeParameters
2+
| genericFunctions2.go:3:6:3:33 | GenericFunctionInAnotherFile | 1 |
3+
| genericFunctions.go:9:6:9:32 | GenericFunctionOneTypeParam | 1 |
4+
| genericFunctions.go:15:6:15:33 | GenericFunctionTwoTypeParams | 2 |
5+
| genericFunctions.go:81:6:81:19 | GenericStruct1 | 1 |
6+
| genericFunctions.go:84:6:84:19 | GenericStruct2 | 2 |
7+
| genericFunctions.go:87:30:87:31 | f1 | 1 |
8+
| genericFunctions.go:92:31:92:32 | g1 | 1 |
9+
| genericFunctions.go:95:35:95:36 | f2 | 2 |
10+
| genericFunctions.go:98:36:98:37 | g2 | 2 |
11+
| genericFunctions.go:111:6:111:12 | Element | 1 |
12+
| genericFunctions.go:115:6:115:9 | List | 1 |
13+
| genericFunctions.go:120:19:120:23 | MyLen | 1 |
14+
| genericFunctions.go:124:6:124:19 | NodeConstraint | 1 |
15+
| genericFunctions.go:128:6:128:19 | EdgeConstraint | 1 |
16+
| genericFunctions.go:132:6:132:10 | Graph | 2 |
17+
| genericFunctions.go:134:6:134:8 | New | 2 |
18+
| genericFunctions.go:138:29:138:40 | ShortestPath | 2 |
19+
| genericFunctions.go:150:6:150:36 | multipleAnonymousTypeParamsFunc | 3 |
20+
| genericFunctions.go:152:6:152:36 | multipleAnonymousTypeParamsType | 3 |
21+
| genericFunctions.go:154:51:154:51 | f | 3 |
22+
#select
123
| cmp.Compare | 0 | T | Ordered |
224
| cmp.Less | 0 | T | Ordered |
325
| cmp.Or | 0 | T | comparable |
@@ -26,6 +48,15 @@
2648
| codeql-go-tests/function.New | 0 | Node | NodeConstraint |
2749
| codeql-go-tests/function.New | 1 | Edge | EdgeConstraint |
2850
| codeql-go-tests/function.NodeConstraint | 0 | Edge | interface { } |
51+
| codeql-go-tests/function.multipleAnonymousTypeParamsFunc | 0 | _ | interface { } |
52+
| codeql-go-tests/function.multipleAnonymousTypeParamsFunc | 1 | _ | interface { string } |
53+
| codeql-go-tests/function.multipleAnonymousTypeParamsFunc | 2 | _ | interface { } |
54+
| codeql-go-tests/function.multipleAnonymousTypeParamsType | 0 | _ | interface { } |
55+
| codeql-go-tests/function.multipleAnonymousTypeParamsType | 1 | _ | interface { string } |
56+
| codeql-go-tests/function.multipleAnonymousTypeParamsType | 2 | _ | interface { } |
57+
| codeql-go-tests/function.multipleAnonymousTypeParamsType.f | 0 | _ | interface { } |
58+
| codeql-go-tests/function.multipleAnonymousTypeParamsType.f | 1 | _ | interface { string } |
59+
| codeql-go-tests/function.multipleAnonymousTypeParamsType.f | 2 | _ | interface { } |
2960
| github.com/anotherpkg.GenericFunctionInAnotherPackage | 0 | T | interface { } |
3061
| internal/bytealg.HashStr | 0 | T | interface { string \| []uint8 } |
3162
| internal/bytealg.HashStrRev | 0 | T | interface { string \| []uint8 } |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import go
22

3+
query predicate numberOfTypeParameters(TypeParamParentEntity parent, int n) {
4+
exists(string file | file != "" | parent.hasLocationInfo(file, _, _, _, _)) and
5+
n = strictcount(TypeParamType tpt | tpt.getParent() = parent)
6+
}
7+
38
from TypeParamType tpt, TypeParamParentEntity ty
49
where ty = tpt.getParent()
510
select ty.getQualifiedName(), tpt.getIndex(), tpt.getParamName(), tpt.getConstraint().pp()

go/ql/test/library-tests/semmle/go/Function/genericFunctions.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,9 @@ func callFunctionsInAnotherPackage() {
146146
_ = anotherpkg.GenericFunctionInAnotherPackage[string]("world")
147147
_ = anotherpkg.GenericFunctionInAnotherPackage("world")
148148
}
149+
150+
func multipleAnonymousTypeParamsFunc[_ any, _ string, _ any]() {}
151+
152+
type multipleAnonymousTypeParamsType[_ any, _ string, _ any] struct{}
153+
154+
func (x multipleAnonymousTypeParamsType[_, _, _]) f() {}

go/ql/test/library-tests/semmle/go/Function/getParameter.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
| genericFunctions.go:138:29:138:40 | ShortestPath | 0 | genericFunctions.go:138:42:138:45 | from |
1111
| genericFunctions.go:138:29:138:40 | ShortestPath | 1 | genericFunctions.go:138:48:138:49 | to |
1212
| genericFunctions.go:138:29:138:40 | ShortestPath | -1 | genericFunctions.go:138:7:138:7 | g |
13+
| genericFunctions.go:154:51:154:51 | f | -1 | genericFunctions.go:154:7:154:7 | x |
1314
| main.go:7:6:7:7 | f1 | 0 | main.go:7:9:7:9 | x |
1415
| main.go:9:12:9:13 | f2 | 0 | main.go:9:15:9:15 | x |
1516
| main.go:9:12:9:13 | f2 | 1 | main.go:9:18:9:18 | y |

go/ql/test/library-tests/semmle/go/Function/getTypeParameter.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@
1313
| genericFunctions.go:132:6:132:73 | type declaration specifier | TypeSpec | 1 | genericFunctions.go:132:39:132:63 | type parameter declaration | 0 | genericFunctions.go:132:39:132:42 | Edge | genericFunctions.go:132:44:132:63 | generic type instantiation expression | EdgeConstraint |
1414
| genericFunctions.go:134:1:136:1 | function declaration | FuncDecl | 0 | genericFunctions.go:134:10:134:34 | type parameter declaration | 0 | genericFunctions.go:134:10:134:13 | Node | genericFunctions.go:134:15:134:34 | generic type instantiation expression | NodeConstraint |
1515
| genericFunctions.go:134:1:136:1 | function declaration | FuncDecl | 1 | genericFunctions.go:134:37:134:61 | type parameter declaration | 0 | genericFunctions.go:134:37:134:40 | Edge | genericFunctions.go:134:42:134:61 | generic type instantiation expression | EdgeConstraint |
16+
| genericFunctions.go:150:1:150:65 | function declaration | FuncDecl | 0 | genericFunctions.go:150:38:150:42 | type parameter declaration | 0 | genericFunctions.go:150:38:150:38 | _ | genericFunctions.go:150:40:150:42 | any | interface { } |
17+
| genericFunctions.go:150:1:150:65 | function declaration | FuncDecl | 1 | genericFunctions.go:150:45:150:52 | type parameter declaration | 0 | genericFunctions.go:150:45:150:45 | _ | genericFunctions.go:150:47:150:52 | string | interface { string } |
18+
| genericFunctions.go:150:1:150:65 | function declaration | FuncDecl | 2 | genericFunctions.go:150:55:150:59 | type parameter declaration | 0 | genericFunctions.go:150:55:150:55 | _ | genericFunctions.go:150:57:150:59 | any | interface { } |
19+
| genericFunctions.go:152:6:152:69 | type declaration specifier | TypeSpec | 0 | genericFunctions.go:152:38:152:42 | type parameter declaration | 0 | genericFunctions.go:152:38:152:38 | _ | genericFunctions.go:152:40:152:42 | any | interface { } |
20+
| genericFunctions.go:152:6:152:69 | type declaration specifier | TypeSpec | 1 | genericFunctions.go:152:45:152:52 | type parameter declaration | 0 | genericFunctions.go:152:45:152:45 | _ | genericFunctions.go:152:47:152:52 | string | interface { string } |
21+
| genericFunctions.go:152:6:152:69 | type declaration specifier | TypeSpec | 2 | genericFunctions.go:152:55:152:59 | type parameter declaration | 0 | genericFunctions.go:152:55:152:55 | _ | genericFunctions.go:152:57:152:59 | any | interface { } |

0 commit comments

Comments
 (0)