mirror of https://github.com/golang/go.git
[dev.unified] cmd/compile/internal/noder: implicit conversions for complits
Operands within a composite literal must be implicitly converted to their respective key/element type. Change-Id: Idc12eba1559e9c9ffebd03395cd91473dd5fc2db Reviewed-on: https://go-review.googlesource.com/c/go/+/413364 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
parent
5f5422a2dd
commit
a3e474f867
|
|
@ -1574,30 +1574,45 @@ func (w *writer) compLit(lit *syntax.CompositeLit) {
|
|||
if ptr, ok := types2.CoreType(typ).(*types2.Pointer); ok {
|
||||
typ = ptr.Elem()
|
||||
}
|
||||
str, isStruct := types2.CoreType(typ).(*types2.Struct)
|
||||
var keyType, elemType types2.Type
|
||||
var structType *types2.Struct
|
||||
switch typ := types2.CoreType(typ).(type) {
|
||||
default:
|
||||
w.p.fatalf(lit, "unexpected composite literal type: %v", typ)
|
||||
case *types2.Array:
|
||||
elemType = typ.Elem()
|
||||
case *types2.Map:
|
||||
keyType, elemType = typ.Key(), typ.Elem()
|
||||
case *types2.Slice:
|
||||
elemType = typ.Elem()
|
||||
case *types2.Struct:
|
||||
structType = typ
|
||||
}
|
||||
|
||||
w.Len(len(lit.ElemList))
|
||||
for i, elem := range lit.ElemList {
|
||||
if isStruct {
|
||||
elemType := elemType
|
||||
if structType != nil {
|
||||
if kv, ok := elem.(*syntax.KeyValueExpr); ok {
|
||||
// use position of expr.Key rather than of elem (which has position of ':')
|
||||
w.pos(kv.Key)
|
||||
w.Len(fieldIndex(w.p.info, str, kv.Key.(*syntax.Name)))
|
||||
i = fieldIndex(w.p.info, structType, kv.Key.(*syntax.Name))
|
||||
elem = kv.Value
|
||||
} else {
|
||||
w.pos(elem)
|
||||
w.Len(i)
|
||||
}
|
||||
elemType = structType.Field(i).Type()
|
||||
w.Len(i)
|
||||
} else {
|
||||
if kv, ok := elem.(*syntax.KeyValueExpr); w.Bool(ok) {
|
||||
// use position of expr.Key rather than of elem (which has position of ':')
|
||||
w.pos(kv.Key)
|
||||
w.expr(kv.Key) // TODO(mdempsky): Implicit conversion to (map) key type.
|
||||
w.implicitExpr(kv.Key, keyType, kv.Key)
|
||||
elem = kv.Value
|
||||
}
|
||||
}
|
||||
w.pos(elem)
|
||||
w.expr(elem) // TODO(mdempsky): Implicit conversion to element type.
|
||||
w.implicitExpr(elem, elemType, elem)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue