]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] Fix problem with 14.go
authorDan Scales <danscales@google.com>
Mon, 19 Jul 2021 19:41:30 +0000 (12:41 -0700)
committerDan Scales <danscales@google.com>
Thu, 22 Jul 2021 04:45:49 +0000 (04:45 +0000)
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>
src/cmd/compile/internal/ir/expr.go
src/cmd/compile/internal/noder/transform.go
test/run.go
test/typeparam/mdempsky/14.go

index 9c800dcd1a79ef68c0310bbf797a86667bbaa8c6..09d6d87f0603943fdb191e250678460ff57fac65 100644 (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]
 }
 
index efbc8f68ceee79b6c0cf2768f9f939d8c2811061..86bdb91395c74fb6ace7fb3f6f7573fdff83d9bf 100644 (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.
index 55c508dd4237c70cd4e2524a0c8276bb4ee73b3c..1e01d160674912d4893678cf43c55d4dacdcd784 100644 (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(
index 61f9436910cb42884fc1d9385d7868256aa5f3b0..ba685bc35c8d3fd08ae56a6f49f68fe328b3ded3 100644 (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