}
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)
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)
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
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
}
_ = [10]int{100: 0} // ERROR "array index 100 out of bounds"
_ = [...]int{100: 0}
- _ = []int{t} // ERROR "cannot use .* as type int in array or slice literal"
- _ = [10]int{t} // ERROR "cannot use .* as type int in array or slice literal"
- _ = [...]int{t} // ERROR "cannot use .* as type int in array or slice literal"
+ _ = []int{t} // ERROR "cannot use .* as type int in slice literal"
+ _ = [10]int{t} // ERROR "cannot use .* as type int in array literal"
+ _ = [...]int{t} // ERROR "cannot use .* as type int in array literal"
}
--- /dev/null
+// errorcheck
+
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Check error message for duplicated index in slice literal
+
+package p
+
+var s = []string{
+ 1: "dup",
+ 1: "dup", // ERROR "duplicate index in slice literal: 1"
+}