tinygo/compiler/testdata/generics.go
Ayke van Laethem 7b1e5f6f99 compiler: implement unsafe.Alignof and unsafe.Sizeof for generic code
For some reason, these aren't lowered when a generic function is
instantiated by the SSA package.

I've left unsafe.Offsetof to be implemented later, it's a bit difficult
to do correctly the way the code is currently structured.
2022-07-28 15:43:51 +02:00

30 строки
389 Б
Go

package main
import "unsafe"
type Coord interface {
int | float32
}
type Point[T Coord] struct {
X, Y T
}
func Add[T Coord](a, b Point[T]) Point[T] {
checkSize(unsafe.Alignof(a))
checkSize(unsafe.Sizeof(a))
return Point[T]{
X: a.X + b.X,
Y: a.Y + b.Y,
}
}
func main() {
var af, bf Point[float32]
Add(af, bf)
var ai, bi Point[int]
Add(ai, bi)
}
func checkSize(uintptr)