From: Dan Scales Date: Mon, 19 Jul 2021 19:41:30 +0000 (-0700) Subject: [dev.typeparams] Fix problem with 14.go X-Git-Tag: go1.18beta1~1818^2^2~124 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=8e9109e95a;p=gostls13.git [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 Reviewed-by: Keith Randall --- diff --git a/src/cmd/compile/internal/ir/expr.go b/src/cmd/compile/internal/ir/expr.go index 9c800dcd1a..09d6d87f06 100644 --- a/src/cmd/compile/internal/ir/expr.go +++ b/src/cmd/compile/internal/ir/expr.go @@ -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] } diff --git a/src/cmd/compile/internal/noder/transform.go b/src/cmd/compile/internal/noder/transform.go index efbc8f68ce..86bdb91395 100644 --- a/src/cmd/compile/internal/noder/transform.go +++ b/src/cmd/compile/internal/noder/transform.go @@ -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. diff --git a/test/run.go b/test/run.go index 55c508dd42..1e01d16067 100644 --- a/test/run.go +++ b/test/run.go @@ -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( diff --git a/test/typeparam/mdempsky/14.go b/test/typeparam/mdempsky/14.go index 61f9436910..ba685bc35c 100644 --- a/test/typeparam/mdempsky/14.go +++ b/test/typeparam/mdempsky/14.go @@ -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