Skip to content

Commit efd395f

Browse files
cuonglmmdempsky
authored andcommitted
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 4a09a9b commit efd395f

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

src/cmd/compile/internal/gc/typecheck.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -2782,7 +2782,7 @@ func typecheckcomplit(n *Node) (res *Node) {
27822782
}
27832783
elemType := n.Right.Right.Type
27842784

2785-
length := typecheckarraylit(elemType, -1, n.List.Slice())
2785+
length := typecheckarraylit(elemType, -1, n.List.Slice(), "array literal")
27862786

27872787
n.Op = OARRAYLIT
27882788
n.Type = types.NewArray(elemType, length)
@@ -2804,12 +2804,12 @@ func typecheckcomplit(n *Node) (res *Node) {
28042804
n.Type = nil
28052805

28062806
case TARRAY:
2807-
typecheckarraylit(t.Elem(), t.NumElem(), n.List.Slice())
2807+
typecheckarraylit(t.Elem(), t.NumElem(), n.List.Slice(), "array literal")
28082808
n.Op = OARRAYLIT
28092809
n.Right = nil
28102810

28112811
case TSLICE:
2812-
length := typecheckarraylit(t.Elem(), -1, n.List.Slice())
2812+
length := typecheckarraylit(t.Elem(), -2, n.List.Slice(), "slice literal")
28132813
n.Op = OSLICELIT
28142814
n.Right = nodintconst(length)
28152815

@@ -2960,7 +2960,8 @@ func typecheckcomplit(n *Node) (res *Node) {
29602960
return n
29612961
}
29622962

2963-
func typecheckarraylit(elemType *types.Type, bound int64, elts []*Node) int64 {
2963+
// typecheckarraylit type-checks a sequence of slice/array literal elements.
2964+
func typecheckarraylit(elemType *types.Type, bound int64, elts []*Node, ctx string) int64 {
29642965
// If there are key/value pairs, create a map to keep seen
29652966
// keys so we can check for duplicate indices.
29662967
var indices map[int64]bool
@@ -2995,12 +2996,12 @@ func typecheckarraylit(elemType *types.Type, bound int64, elts []*Node) int64 {
29952996
r := *vp
29962997
r = pushtype(r, elemType)
29972998
r = typecheck(r, ctxExpr)
2998-
*vp = assignconv(r, elemType, "array or slice literal")
2999+
*vp = assignconv(r, elemType, ctx)
29993000

30003001
if key >= 0 {
30013002
if indices != nil {
30023003
if indices[key] {
3003-
yyerror("duplicate index in array literal: %d", key)
3004+
yyerror("duplicate index in %s: %d", ctx, key)
30043005
} else {
30053006
indices[key] = true
30063007
}

test/fixedbugs/issue13365.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func main() {
1919
_ = [10]int{100: 0} // ERROR "array index 100 out of bounds"
2020
_ = [...]int{100: 0}
2121

22-
_ = []int{t} // ERROR "cannot use .* as type int in array or slice literal"
23-
_ = [10]int{t} // ERROR "cannot use .* as type int in array or slice literal"
24-
_ = [...]int{t} // ERROR "cannot use .* as type int in array or slice literal"
22+
_ = []int{t} // ERROR "cannot use .* as type int in slice literal"
23+
_ = [10]int{t} // ERROR "cannot use .* as type int in array literal"
24+
_ = [...]int{t} // ERROR "cannot use .* as type int in array literal"
2525
}

test/fixedbugs/issue35291.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// errorcheck
2+
3+
// Copyright 2019 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
// Check error message for duplicated index in slice literal
8+
9+
package p
10+
11+
var s = []string{
12+
1: "dup",
13+
1: "dup", // ERROR "duplicate index in slice literal: 1"
14+
}

test/fixedbugs/issue7153.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99
package p
1010

11-
var _ = []int{a: true, true} // ERROR "undefined: a" "cannot use true \(type untyped bool\) as type int in array or slice literal"
11+
var _ = []int{a: true, true} // ERROR "undefined: a" "cannot use true \(type untyped bool\) as type int in slice literal"

0 commit comments

Comments
 (0)