1
0
mirror of https://github.com/golang/go synced 2024-11-11 23:10:23 -07:00

go/types: allow composite literals of type parameter type

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>
This commit is contained in:
Robert Findley 2021-08-31 14:03:33 -04:00
parent 891470fbf7
commit aed59d172a
2 changed files with 17 additions and 2 deletions

View 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

View 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}, {}}
}