]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: allow composite literals of type parameter type
authorRobert Findley <rfindley@google.com>
Tue, 31 Aug 2021 18:03:33 +0000 (14:03 -0400)
committerRobert Findley <rfindley@google.com>
Tue, 31 Aug 2021 20:39:39 +0000 (20:39 +0000)
This is a port of CL 342690 to go/types.

Change-Id: I27dcde237e400a84c3394a3579805014777830bc
Reviewed-on: https://go-review.googlesource.com/c/go/+/346432
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/go/types/expr.go
src/go/types/testdata/examples/types.go2

index b0e2a27085f8237e0b351fee7a65c30228126701..61d57cc4fa78906fd48a1182394dd9d2f73ca9ad 100644 (file)
@@ -1184,7 +1184,7 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
                        goto Error
                }
 
-               switch utyp := under(base).(type) {
+               switch utyp := optype(base).(type) {
                case *Struct:
                        if len(e.Elts) == 0 {
                                break
index 82f17a3263df8808341d898bff6a52c129375579..1aebb411c69da0fb215465693d069929b88a3e15 100644 (file)
@@ -191,7 +191,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
@@ -294,3 +294,18 @@ func _[T interface {~int|~float64}]() {
        _ = 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}, {}}
+}