Skip to content

Commit

Permalink
[dev.unified] cmd/compile/internal/noder: implicit conversions for co…
Browse files Browse the repository at this point in the history
…mplits

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>
  • Loading branch information
mdempsky committed Jun 23, 2022
1 parent 5f5422a commit a3e474f
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/cmd/compile/internal/noder/writer.go
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit a3e474f

Please sign in to comment.