tinygo-org / tinygo

Go compiler for small places. Microcontrollers, WebAssembly (WASM/WASI), and command-line tools. Based on LLVM.
https://tinygo.org
Other
15.25k stars 900 forks source link

reflect: getting the type name as string of a generic type #4098

Open nicksinch opened 8 months ago

nicksinch commented 8 months ago

I have a encountered an issue when trying to get the whole type name as string of a generic type and I was able to create a small example to reproduce it. When running the code in standard Go, the program prints: Herd[main.Wolf] , however, with TinyGo, it prints Herd omitting the type parameter of the generic struct:

Env: tinygo version 0.30.0 darwin/amd64 (using go version go1.21.6 and LLVM version 16.0.1)

tinygo run -target=wasi main.go

package main

import "reflect"

type Animal interface {
    sound()
}

type Wolf struct{}

func (w Wolf) sound() {}

type Herd[T Animal] struct{}

func main() {
    wolvesHerd := Herd[Wolf]{}
    herdVal := reflect.ValueOf(wolvesHerd)
    typeName := herdVal.Type().Name()
    println(typeName)
}
dgryski commented 3 months ago

We "just" need to include the TypeParams when constructing the data field in compiler/interface.go, and then pull it out correctly in src/reflect/type.go.