mirror of
https://github.com/golang/go
synced 2024-09-04 15:34:21 +00:00
cmd/compile: clearer error for invalid array/slice literal elements
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>
This commit is contained in:
parent
e36bf614c8
commit
c7a3403140
|
@ -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 {
|
||||
|
|
25
test/fixedbugs/issue13365.go
Normal file
25
test/fixedbugs/issue13365.go
Normal file
|
@ -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"
|
||||
}
|
|
@ -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\]"
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in a new issue