Skip to content

Commit

Permalink
Merge pull request #17429 from owen-mc/go/fix/multiple-anonymous-type…
Browse files Browse the repository at this point in the history
…-parameters

Go: fix multiple anonymous type parameters
  • Loading branch information
owen-mc authored Sep 11, 2024
2 parents f9e4c0a + 13f8488 commit 076dd07
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 1 deletion.
3 changes: 2 additions & 1 deletion go/extractor/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1776,7 +1776,8 @@ func getTypeLabel(tw *trap.Writer, tp types.Type) (trap.Label, bool) {
lbl = tw.Labeler.GlobalID(fmt.Sprintf("{%s};namedtype", entitylbl))
case *types.TypeParam:
parentlbl := getTypeParamParentLabel(tw, tp)
lbl = tw.Labeler.GlobalID(fmt.Sprintf("{%v},%s;typeparamtype", parentlbl, tp.Obj().Name()))
idx := tp.Index()
lbl = tw.Labeler.GlobalID(fmt.Sprintf("{%v},%d,%s;typeparamtype", parentlbl, idx, tp.Obj().Name()))
case *types.Union:
var b strings.Builder
for i := 0; i < tp.Len(); i++ {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* 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.
31 changes: 31 additions & 0 deletions go/ql/test/library-tests/semmle/go/Function/TypeParamType.expected
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
numberOfTypeParameters
| genericFunctions2.go:3:6:3:33 | GenericFunctionInAnotherFile | 1 |
| genericFunctions.go:9:6:9:32 | GenericFunctionOneTypeParam | 1 |
| genericFunctions.go:15:6:15:33 | GenericFunctionTwoTypeParams | 2 |
| genericFunctions.go:81:6:81:19 | GenericStruct1 | 1 |
| genericFunctions.go:84:6:84:19 | GenericStruct2 | 2 |
| genericFunctions.go:87:30:87:31 | f1 | 1 |
| genericFunctions.go:92:31:92:32 | g1 | 1 |
| genericFunctions.go:95:35:95:36 | f2 | 2 |
| genericFunctions.go:98:36:98:37 | g2 | 2 |
| genericFunctions.go:111:6:111:12 | Element | 1 |
| genericFunctions.go:115:6:115:9 | List | 1 |
| genericFunctions.go:120:19:120:23 | MyLen | 1 |
| genericFunctions.go:124:6:124:19 | NodeConstraint | 1 |
| genericFunctions.go:128:6:128:19 | EdgeConstraint | 1 |
| genericFunctions.go:132:6:132:10 | Graph | 2 |
| genericFunctions.go:134:6:134:8 | New | 2 |
| genericFunctions.go:138:29:138:40 | ShortestPath | 2 |
| genericFunctions.go:150:6:150:36 | multipleAnonymousTypeParamsFunc | 3 |
| genericFunctions.go:152:6:152:36 | multipleAnonymousTypeParamsType | 3 |
| genericFunctions.go:154:51:154:51 | f | 3 |
#select
| cmp.Compare | 0 | T | Ordered |
| cmp.Less | 0 | T | Ordered |
| cmp.Or | 0 | T | comparable |
Expand Down Expand Up @@ -26,6 +48,15 @@
| codeql-go-tests/function.New | 0 | Node | NodeConstraint |
| codeql-go-tests/function.New | 1 | Edge | EdgeConstraint |
| codeql-go-tests/function.NodeConstraint | 0 | Edge | interface { } |
| codeql-go-tests/function.multipleAnonymousTypeParamsFunc | 0 | _ | interface { } |
| codeql-go-tests/function.multipleAnonymousTypeParamsFunc | 1 | _ | interface { string } |
| codeql-go-tests/function.multipleAnonymousTypeParamsFunc | 2 | _ | interface { } |
| codeql-go-tests/function.multipleAnonymousTypeParamsType | 0 | _ | interface { } |
| codeql-go-tests/function.multipleAnonymousTypeParamsType | 1 | _ | interface { string } |
| codeql-go-tests/function.multipleAnonymousTypeParamsType | 2 | _ | interface { } |
| codeql-go-tests/function.multipleAnonymousTypeParamsType.f | 0 | _ | interface { } |
| codeql-go-tests/function.multipleAnonymousTypeParamsType.f | 1 | _ | interface { string } |
| codeql-go-tests/function.multipleAnonymousTypeParamsType.f | 2 | _ | interface { } |
| github.com/anotherpkg.GenericFunctionInAnotherPackage | 0 | T | interface { } |
| internal/bytealg.HashStr | 0 | T | interface { string \| []uint8 } |
| internal/bytealg.HashStrRev | 0 | T | interface { string \| []uint8 } |
Expand Down
5 changes: 5 additions & 0 deletions go/ql/test/library-tests/semmle/go/Function/TypeParamType.ql
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import go

query predicate numberOfTypeParameters(TypeParamParentEntity parent, int n) {
exists(string file | file != "" | parent.hasLocationInfo(file, _, _, _, _)) and
n = strictcount(TypeParamType tpt | tpt.getParent() = parent)
}

from TypeParamType tpt, TypeParamParentEntity ty
where ty = tpt.getParent()
select ty.getQualifiedName(), tpt.getIndex(), tpt.getParamName(), tpt.getConstraint().pp()
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,9 @@ func callFunctionsInAnotherPackage() {
_ = anotherpkg.GenericFunctionInAnotherPackage[string]("world")
_ = anotherpkg.GenericFunctionInAnotherPackage("world")
}

func multipleAnonymousTypeParamsFunc[_ any, _ string, _ any]() {}

type multipleAnonymousTypeParamsType[_ any, _ string, _ any] struct{}

func (x multipleAnonymousTypeParamsType[_, _, _]) f() {}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
| genericFunctions.go:138:29:138:40 | ShortestPath | 0 | genericFunctions.go:138:42:138:45 | from |
| genericFunctions.go:138:29:138:40 | ShortestPath | 1 | genericFunctions.go:138:48:138:49 | to |
| genericFunctions.go:138:29:138:40 | ShortestPath | -1 | genericFunctions.go:138:7:138:7 | g |
| genericFunctions.go:154:51:154:51 | f | -1 | genericFunctions.go:154:7:154:7 | x |
| main.go:7:6:7:7 | f1 | 0 | main.go:7:9:7:9 | x |
| main.go:9:12:9:13 | f2 | 0 | main.go:9:15:9:15 | x |
| main.go:9:12:9:13 | f2 | 1 | main.go:9:18:9:18 | y |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@
| 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 |
| 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 |
| 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 |
| 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 { } |
| 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 } |
| 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 { } |
| 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 { } |
| 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 } |
| 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 comments on commit 076dd07

Please sign in to comment.