diff --git a/src/cmd/compile/internal/gc/typecheck.go b/src/cmd/compile/internal/gc/typecheck.go index 354a2fadd2..f04578ef8f 100644 --- a/src/cmd/compile/internal/gc/typecheck.go +++ b/src/cmd/compile/internal/gc/typecheck.go @@ -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 index 0000000000..379f9b6586 --- /dev/null +++ b/test/fixedbugs/issue13365.go @@ -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" +} diff --git a/test/fixedbugs/issue7150.go b/test/fixedbugs/issue7150.go index 264958a089..05e8d75514 100644 --- a/test/fixedbugs/issue7150.go +++ b/test/fixedbugs/issue7150.go @@ -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\]" } diff --git a/test/fixedbugs/issue7153.go b/test/fixedbugs/issue7153.go index d70d8582a5..f238f78e84 100644 --- a/test/fixedbugs/issue7153.go +++ b/test/fixedbugs/issue7153.go @@ -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"