]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: make duplicate index error distinguish arrays and slices
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Thu, 31 Oct 2019 21:07:23 +0000 (04:07 +0700)
committerMatthew Dempsky <mdempsky@google.com>
Fri, 1 Nov 2019 01:51:26 +0000 (01:51 +0000)
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>
src/cmd/compile/internal/gc/typecheck.go
test/fixedbugs/issue13365.go
test/fixedbugs/issue35291.go [new file with mode: 0644]
test/fixedbugs/issue7153.go

index 140acb90627f42cf1b5352cb77c0fab4288c94bf..7fb4a518177f393b07a89af83d0a447215ec7629 100644 (file)
@@ -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
                                }
index 379f9b6586d521d8850b3969cf2b9c987ce8d71f..4bd103e38d6dd701930d78e500c332055beba212 100644 (file)
@@ -19,7 +19,7 @@ func main() {
        _ = [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"
 }
diff --git a/test/fixedbugs/issue35291.go b/test/fixedbugs/issue35291.go
new file mode 100644 (file)
index 0000000..3cbdbf9
--- /dev/null
@@ -0,0 +1,14 @@
+// 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"
+}
index 215387732b320a87dd0eac9fd10c390978fd891a..66b1338496f298658cf969ea5f2dcc4135b12604 100644 (file)
@@ -8,4 +8,4 @@
 
 package p
 
-var _ = []int{a: true, true} // ERROR "undefined: a" "cannot use true \(type untyped bool\) as type int in array or slice literal"
+var _ = []int{a: true, true} // ERROR "undefined: a" "cannot use true \(type untyped bool\) as type int in slice literal"