aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCuong Manh Le <cuong.manhle.vn@gmail.com>2019-11-01 04:07:23 +0700
committerMatthew Dempsky <mdempsky@google.com>2019-11-01 01:51:26 +0000
commitefd395f9fb429ea3e677d9e378b858cd083eec11 (patch)
tree1cb5fcf4bff376d2ba34a0b2aebecab71579ea5f /src
parent4a09a9b054bf9ffe7bcd1ecb90837ff5dc04aea6 (diff)
downloadgo-efd395f9fb429ea3e677d9e378b858cd083eec11.tar.xz
cmd/compile: make duplicate index error distinguish arrays and slices
Fixes #35291 Change-Id: I11ae367b6e972cd9e7a22bbc2cb23d32f4d72b98 Reviewed-on: https://go-review.googlesource.com/c/go/+/204617 Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Diffstat (limited to 'src')
-rw-r--r--src/cmd/compile/internal/gc/typecheck.go13
1 files changed, 7 insertions, 6 deletions
diff --git a/src/cmd/compile/internal/gc/typecheck.go b/src/cmd/compile/internal/gc/typecheck.go
index 140acb9062..7fb4a51817 100644
--- a/src/cmd/compile/internal/gc/typecheck.go
+++ b/src/cmd/compile/internal/gc/typecheck.go
@@ -2782,7 +2782,7 @@ func typecheckcomplit(n *Node) (res *Node) {
}
elemType := n.Right.Right.Type
- length := typecheckarraylit(elemType, -1, n.List.Slice())
+ length := typecheckarraylit(elemType, -1, n.List.Slice(), "array literal")
n.Op = OARRAYLIT
n.Type = types.NewArray(elemType, length)
@@ -2804,12 +2804,12 @@ func typecheckcomplit(n *Node) (res *Node) {
n.Type = nil
case TARRAY:
- typecheckarraylit(t.Elem(), t.NumElem(), n.List.Slice())
+ typecheckarraylit(t.Elem(), t.NumElem(), n.List.Slice(), "array literal")
n.Op = OARRAYLIT
n.Right = nil
case TSLICE:
- length := typecheckarraylit(t.Elem(), -1, n.List.Slice())
+ length := typecheckarraylit(t.Elem(), -2, n.List.Slice(), "slice literal")
n.Op = OSLICELIT
n.Right = nodintconst(length)
@@ -2960,7 +2960,8 @@ func typecheckcomplit(n *Node) (res *Node) {
return n
}
-func typecheckarraylit(elemType *types.Type, bound int64, elts []*Node) int64 {
+// typecheckarraylit type-checks a sequence of slice/array literal elements.
+func typecheckarraylit(elemType *types.Type, bound int64, elts []*Node, ctx string) int64 {
// If there are key/value pairs, create a map to keep seen
// keys so we can check for duplicate indices.
var indices map[int64]bool
@@ -2995,12 +2996,12 @@ func typecheckarraylit(elemType *types.Type, bound int64, elts []*Node) int64 {
r := *vp
r = pushtype(r, elemType)
r = typecheck(r, ctxExpr)
- *vp = assignconv(r, elemType, "array or slice literal")
+ *vp = assignconv(r, elemType, ctx)
if key >= 0 {
if indices != nil {
if indices[key] {
- yyerror("duplicate index in array literal: %d", key)
+ yyerror("duplicate index in %s: %d", ctx, key)
} else {
indices[key] = true
}