1
0
mirror of https://github.com/golang/go synced 2024-11-26 11:08:38 -07:00

cmd/compile/internal/types2: allow composite literals of type parameter type

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>
This commit is contained in:
Robert Griesemer 2021-08-16 18:06:18 -07:00
parent d3deb2c359
commit a304273d74
2 changed files with 18 additions and 2 deletions

View File

@ -1214,7 +1214,7 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
goto Error goto Error
} }
switch utyp := under(base).(type) { switch utyp := optype(base).(type) {
case *Struct: case *Struct:
if len(e.ElemList) == 0 { if len(e.ElemList) == 0 {
break break

View File

@ -185,7 +185,7 @@ type _ struct {
// } // }
// It is not permitted to declare a local type whose underlying // 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]() { func _[T any]() {
type _ T // ERROR cannot use function type parameter T as RHS in type declaration 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 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 var _ T = 1
_ = T(0) _ = 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}, {}}
}