]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: clearer error for invalid array/slice literal elements
authorRobert Griesemer <gri@golang.org>
Wed, 25 Nov 2015 01:48:52 +0000 (17:48 -0800)
committerRobert Griesemer <gri@golang.org>
Wed, 25 Nov 2015 19:49:38 +0000 (19:49 +0000)
Fixes #13365.

Change-Id: I5a447ff806dbbb11c8c75e2b5cfa7fd4a845fb92
Reviewed-on: https://go-review.googlesource.com/17206
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/compile/internal/gc/typecheck.go
test/fixedbugs/issue13365.go [new file with mode: 0644]
test/fixedbugs/issue7150.go
test/fixedbugs/issue7153.go

index 354a2fadd2cf7ecf02d02375f66ccfbad61399a4..f04578ef8ff8cd3ca61e5b662b4d457173c2c791 100644 (file)
@@ -2971,9 +2971,8 @@ func typecheckcomplit(np **Node) {
                }
                length := int64(0)
                i := 0
-               var l *Node
                for ll := n.List; ll != nil; ll = ll.Next {
-                       l = ll.N
+                       l := ll.N
                        setlineno(l)
                        if l.Op != OKEY {
                                l = Nod(OKEY, Nodintconst(int64(i)), l)
@@ -2986,7 +2985,7 @@ func typecheckcomplit(np **Node) {
                        evconst(l.Left)
                        i = nonnegconst(l.Left)
                        if i < 0 && l.Left.Diag == 0 {
-                               Yyerror("array index must be non-negative integer constant")
+                               Yyerror("index must be non-negative integer constant")
                                l.Left.Diag = 1
                                i = -(1 << 30) // stay negative for a while
                        }
@@ -3008,7 +3007,7 @@ func typecheckcomplit(np **Node) {
                        pushtype(r, t.Type)
                        typecheck(&r, Erv)
                        defaultlit(&r, t.Type)
-                       l.Right = assignconv(r, t.Type, "array element")
+                       l.Right = assignconv(r, t.Type, "array or slice literal")
                }
 
                if t.Bound == -100 {
diff --git a/test/fixedbugs/issue13365.go b/test/fixedbugs/issue13365.go
new file mode 100644 (file)
index 0000000..379f9b6
--- /dev/null
@@ -0,0 +1,25 @@
+// errorcheck
+
+// Copyright 2015 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.
+
+// issue 13365: confusing error message (array vs slice)
+
+package main
+
+var t struct{}
+
+func main() {
+       _ = []int{-1: 0}    // ERROR "index must be non\-negative integer constant"
+       _ = [10]int{-1: 0}  // ERROR "index must be non\-negative integer constant"
+       _ = [...]int{-1: 0} // ERROR "index must be non\-negative integer constant"
+
+       _ = []int{100: 0}
+       _ = [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"
+}
index 264958a089e153a08932d25877e8adaff24c1c90..05e8d75514f8afda16c21f805f0acaecc6d293e2 100644 (file)
@@ -9,9 +9,9 @@
 package main
 
 func main() {
-       _ = [0]int{-1: 50}              // ERROR "array index must be non-negative integer constant"
-       _ = [0]int{0: 0}                // ERROR "array index 0 out of bounds \[0:0\]"
-       _ = [0]int{5: 25}               // ERROR "array index 5 out of bounds \[0:0\]"
-       _ = [10]int{2: 10, 15: 30}      // ERROR "array index 15 out of bounds \[0:10\]"
-       _ = [10]int{5: 5, 1: 1, 12: 12} // ERROR "array index 12 out of bounds \[0:10\]"
+       _ = [0]int{-1: 50}              // ERROR "index must be non-negative integer constant"
+       _ = [0]int{0: 0}                // ERROR "index 0 out of bounds \[0:0\]"
+       _ = [0]int{5: 25}               // ERROR "index 5 out of bounds \[0:0\]"
+       _ = [10]int{2: 10, 15: 30}      // ERROR "index 15 out of bounds \[0:10\]"
+       _ = [10]int{5: 5, 1: 1, 12: 12} // ERROR "index 12 out of bounds \[0:10\]"
 }
index d70d8582a55be2479b9c1cc4827041683ecd2b22..f238f78e8451353e686a5f523f67071bf627c8dc 100644 (file)
@@ -8,4 +8,4 @@
 
 package p
 
-var _ = []int{a: true, true} // ERROR "undefined: a" "cannot use true \(type bool\) as type int in array element"
+var _ = []int{a: true, true} // ERROR "undefined: a" "cannot use true \(type bool\) as type int in array or slice literal"