Skip to content

Commit

Permalink
++
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Apr 1, 2023
1 parent a86f669 commit f89c9d1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
12 changes: 12 additions & 0 deletions cpg-language-go/src/main/golang/frontend/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<GoLanguage>()
}
assertNotNull(tu)

val myStruct = tu.records["MyStruct"]
assertNotNull(myStruct)

val myInterface = tu.records["MyInterface"]
assertNotNull(myInterface)
}
}
7 changes: 5 additions & 2 deletions cpg-language-go/src/test/resources/golang/type_constraints.go
Original file line number Diff line number Diff line change
@@ -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]()
}

0 comments on commit f89c9d1

Please sign in to comment.