diff --git a/cpg-language-go/src/main/golang/frontend/handler.go b/cpg-language-go/src/main/golang/frontend/handler.go index 8d594a2234..923ced3e45 100644 --- a/cpg-language-go/src/main/golang/frontend/handler.go +++ b/cpg-language-go/src/main/golang/frontend/handler.go @@ -1494,6 +1494,18 @@ func (this *GoLanguageFrontend) handleType(fset *token.FileSet, typeExpr ast.Exp (*cpg.ObjectType)(t).AddGeneric(genericType) + return t + case *ast.IndexListExpr: + // This is a type with two type parameters. First we need to parse the "X" expression as a type + var t = this.handleType(fset, v.X) + + // Then we parse the "Indices" as a type parameter + for _, index := range v.Indices { + var genericType = this.handleType(fset, index) + + (*cpg.ObjectType)(t).AddGeneric(genericType) + } + return t default: this.LogError("Not parsing type of type %T yet. Defaulting to unknown type", v) diff --git a/cpg-language-go/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/DeclarationTest.kt b/cpg-language-go/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/DeclarationTest.kt index 1b6a378389..5e379141c1 100644 --- a/cpg-language-go/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/DeclarationTest.kt +++ b/cpg-language-go/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/DeclarationTest.kt @@ -170,4 +170,24 @@ class DeclarationTest { // We have eight assignments in total (6 initializers + 2 assign expressions) assertEquals(8, tu.assignments.size) } + + @Test + fun testTypeConstraints() { + val topLevel = Path.of("src", "test", "resources", "golang") + val tu = + TestUtils.analyzeAndGetFirstTU( + listOf(topLevel.resolve("type_constraints.go").toFile()), + topLevel, + true + ) { + it.registerLanguage() + } + assertNotNull(tu) + + val myStruct = tu.records["MyStruct"] + assertNotNull(myStruct) + + val myInterface = tu.records["MyInterface"] + assertNotNull(myInterface) + } } diff --git a/cpg-language-go/src/test/resources/golang/type_constraints.go b/cpg-language-go/src/test/resources/golang/type_constraints.go index a060f41d9d..485f29bc2e 100644 --- a/cpg-language-go/src/test/resources/golang/type_constraints.go +++ b/cpg-language-go/src/test/resources/golang/type_constraints.go @@ -1,8 +1,11 @@ package main -type MyStruct[T any] {} -type MyInterface{} +type MyStruct[T any] struct {} +type MyInterface interface {} + +func SomeFunc[T any, S MyInterface]() {} func main() { _ := &MyStruct[MyInterface]{} + SomeFunc[any, MyInterface]() } \ No newline at end of file