]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: always return an underlying type from optype
authorRobert Findley <rfindley@google.com>
Thu, 21 Oct 2021 17:23:29 +0000 (13:23 -0400)
committerRobert Findley <rfindley@google.com>
Thu, 21 Oct 2021 17:42:54 +0000 (17:42 +0000)
Optype should never return a defined type.

Change-Id: I37b29e0c958e127e75e834e71d6392ea80827773
Reviewed-on: https://go-review.googlesource.com/c/go/+/357694
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>

src/cmd/compile/internal/types2/testdata/examples/types.go2
src/cmd/compile/internal/types2/type.go
src/go/types/testdata/examples/types.go2
src/go/types/type.go

index 55b1b0da57318bc72d16941b2e0a72bf8779ae84..f177c78d06edd3c8f512331d9290c0b34c71228f 100644 (file)
@@ -304,3 +304,11 @@ func _[P interface{ ~[]E }, E interface{ map[string]P } ]() P {
        return P{E{"foo": x}}
        return P{{"foo": x}, {}}
 }
+
+// This is a degenerate case with a singleton type set, but we can create
+// composite literals even if the structural type is a defined type.
+type MyInts []int
+
+func _[P MyInts]() P {
+       return P{}
+}
index 400d6f7128763cb87cb0aca432e577ebcbdc4d8d..3fb05e9d63bffd13b6aadf418ffa3670ac9da182 100644 (file)
@@ -57,7 +57,7 @@ func optype(typ Type) Type {
                // See also issue #39680.
                if u := t.structuralType(); u != nil {
                        assert(u != typ) // "naked" type parameters cannot be embedded
-                       return u
+                       return under(u)  // optype should always return an underlying type
                }
                return theTop
        }
index 2e6eeb22044a49fa4ecaeecf91f1c5a62f0d122d..807c03238fff206b7c259b1cf9439ab88037b971 100644 (file)
@@ -310,3 +310,11 @@ func _[P interface{ ~[]E }, E interface{ map[string]P } ]() P {
        return P{E{"foo": x}}
        return P{{"foo": x}, {}}
 }
+
+// This is a degenerate case with a singleton type set, but we can create
+// composite literals even if the structural type is a defined type.
+type MyInts []int
+
+func _[P MyInts]() P {
+       return P{}
+}
index 31149cfd366a97896591f950b4490f6959ac0cc0..502c9b2d52d557de6969c2aeb852cbd4cebec229 100644 (file)
@@ -57,7 +57,7 @@ func optype(typ Type) Type {
                // See also issue #39680.
                if u := t.structuralType(); u != nil {
                        assert(u != typ) // "naked" type parameters cannot be embedded
-                       return u
+                       return under(u)  // optype should always return an underlying type
                }
                return theTop
        }