[dev.typeparams] Fix problem with 14.go

Removed a case in transformCall() where we were setting a type on n,
which isn't needed, since noder2 already set the type of n. More
importantly, we are losing information, since the type of the results
may be a shape type, but the actual type of call is the known type
from types2, which may be a concrete type (in this case Zero[MyInt]).
That concrete type will then be used correctly if the concrete result is
converted to an interface.

If we are inlining the call to Zero[MyInt], we need to add an implicit
CONVNOP operation, since we are going to use the result variable
directly, which has a shape type. So, add an implicit CONVNOP to
remember that the known type is the concrete type.

Also cleaned up 14.go a bit, so it is more understandable. Renamed type
T to AnyInt, since T is used elsewhere as a type parameter. Reformatted
Zero function and added a comment.

Change-Id: Id917a2e054e0bbae9bd302232853fa8741d49b64
Reviewed-on: https://go-review.googlesource.com/c/go/+/336430
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
Dan Scales 2021-07-19 12:41:30 -07:00
parent ee20dff27d
commit 8e9109e95a
4 changed files with 17 additions and 9 deletions

View file

@ -349,6 +349,14 @@ func (n *InlinedCallExpr) SingleResult() Node {
if have := len(n.ReturnVars); have != 1 {
base.FatalfAt(n.Pos(), "inlined call has %v results, expected 1", have)
}
if !n.Type().HasShape() && n.ReturnVars[0].Type().HasShape() {
// If the type of the call is not a shape, but the type of the return value
// is a shape, we need to do an implicit conversion, so the real type
// of n is maintained.
r := NewConvExpr(n.Pos(), OCONVNOP, n.Type(), n.ReturnVars[0])
r.SetTypecheck(1)
return r
}
return n.ReturnVars[0]
}

View file

@ -161,8 +161,6 @@ func transformCall(n *ir.CallExpr) {
typecheck.FixMethodCall(n)
}
if t.NumResults() == 1 {
n.SetType(l.Type().Results().Field(0).Type)
if n.Op() == ir.OCALLFUNC && n.X.Op() == ir.ONAME {
if sym := n.X.(*ir.Name).Sym(); types.IsRuntimePkg(sym.Pkg) && sym.Name == "getg" {
// Emit code for runtime.getg() directly instead of calling function.

View file

@ -2174,9 +2174,8 @@ var g3Failures = setOf(
"typeparam/mdempsky/4.go", // -G=3 can't export functions with labeled breaks in loops
"typeparam/cons.go", // causes an unreachable method
"typeparam/issue44688.go", // interface conversion fails due to missing method
"typeparam/mdempsky/14.go", // interface comparison failure
"typeparam/cons.go", // causes an unreachable method
"typeparam/issue44688.go", // interface conversion fails due to missing method
)
var unifiedFailures = setOf(

View file

@ -6,11 +6,14 @@
package main
func Zero[T any]() (_ T) { return }
// Zero returns the zero value of T
func Zero[T any]() (_ T) {
return
}
type T[X any] int
type AnyInt[X any] int
func (T[X]) M() {
func (AnyInt[X]) M() {
var have interface{} = Zero[X]()
var want interface{} = Zero[MyInt]()
@ -22,7 +25,7 @@ func (T[X]) M() {
type I interface{ M() }
type MyInt int
type U = T[MyInt]
type U = AnyInt[MyInt]
var x = U(0)
var i I = x