]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: allow composite literals of type parameter type
authorRobert Griesemer <gri@golang.org>
Tue, 17 Aug 2021 01:06:18 +0000 (18:06 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 17 Aug 2021 04:37:32 +0000 (04:37 +0000)
Change-Id: Iaaa2a3b462da6b121f13a10595950a8502b5f271
Reviewed-on: https://go-review.googlesource.com/c/go/+/342690
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/expr.go
src/cmd/compile/internal/types2/testdata/examples/types.go2

index 6d8b423714c3fe992f81e7e91148dc12ad58034d..d108093dacc08644588fea76bb424d4391ab2369 100644 (file)
@@ -1214,7 +1214,7 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
                        goto Error
                }
 
-               switch utyp := under(base).(type) {
+               switch utyp := optype(base).(type) {
                case *Struct:
                        if len(e.ElemList) == 0 {
                                break
index d662444ead817851e3500a16c0105393b0e58be9..9ee014452c5500c956ea4072328aaea36109c321 100644 (file)
@@ -185,7 +185,7 @@ type _ struct {
 // }
 
 // It is not permitted to declare a local type whose underlying
-// type is a type parameters not declared by that type declaration.
+// type is a type parameter not declared by that type declaration.
 func _[T any]() {
        type _ T         // ERROR cannot use function type parameter T as RHS in type declaration
        type _ [_ any] T // ERROR cannot use function type parameter T as RHS in type declaration
@@ -287,3 +287,19 @@ func _[T interface{~int|~float64}]() {
        var _ T = 1
        _ = T(0)
 }
+
+// It is possible to create composite literals of type parameter
+// type as long as it's possible to create a composite literal
+// of the structural type of the type parameter's constraint.
+func _[P interface{ ~[]int }]() P {
+       return P{}
+       return P{1, 2, 3}
+}
+
+func _[P interface{ ~[]E }, E interface{ map[string]P } ]() P {
+       x := P{}
+       return P{{}}
+       return P{E{}}
+       return P{E{"foo": x}}
+       return P{{"foo": x}, {}}
+}