1
0
mirror of https://github.com/golang/go synced 2024-11-23 14:40:02 -07:00
go/test/devirt.go
Keith Randall 57668b84ff [dev.typeparams] cmd/compile: simplify interface conversions
Simplify the implementation of interface conversions in the compiler.
Don't pass fields that aren't needed (the data word, usually) to the runtime.

For generics, we need to put a dynamic type in an interface. The new
dataWord function is exactly what we need (the type word will come
from a dictionary).

Change-Id: Iade5de5c174854b65ad248f35c7893c603f7be3d
Reviewed-on: https://go-review.googlesource.com/c/go/+/340029
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-08-09 16:10:20 +00:00

39 lines
574 B
Go

// errorcheck -0 -d=ssa/opt/debug=1
package main
// Trivial interface call devirtualization test.
type real struct {
value int
}
func (r *real) Value() int { return r.value }
type Valuer interface {
Value() int
}
type indirectiface struct {
a, b, c int
}
func (i indirectiface) Value() int {
return i.a + i.b + i.c
}
func main() {
var r Valuer
rptr := &real{value: 3}
r = rptr
if r.Value() != 3 { // ERROR "de-virtualizing call$"
panic("not 3")
}
r = indirectiface{3, 4, 5}
if r.Value() != 12 { // ERROR "de-virtualizing call$"
panic("not 12")
}
}