Skip to content
Snippets Groups Projects
Commit bf9d9b7d authored by Robert Griesemer's avatar Robert Griesemer Committed by Gopher Robot
Browse files

go/types, types2: better error message for some invalid integer array lengths

Don't say "array length must be integer" if it is in fact an integer.

Fixes #59209

Change-Id: If60b93a0418f5837ac334412d3838eec25eeb855
Reviewed-on: https://go-review.googlesource.com/c/go/+/479115


Reviewed-by: default avatarRobert Griesemer <gri@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: default avatarRobert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
parent d49b11be
No related branches found
No related tags found
No related merge requests found
...@@ -503,13 +503,17 @@ func (check *Checker) arrayLength(e syntax.Expr) int64 { ...@@ -503,13 +503,17 @@ func (check *Checker) arrayLength(e syntax.Expr) int64 {
if n, ok := constant.Int64Val(val); ok && n >= 0 { if n, ok := constant.Int64Val(val); ok && n >= 0 {
return n return n
} }
check.errorf(&x, InvalidArrayLen, "invalid array length %s", &x)
return -1
} }
} }
} }
check.errorf(&x, InvalidArrayLen, "array length %s must be integer", &x) var msg string
if isInteger(x.typ) {
msg = "invalid array length %s"
} else {
msg = "array length %s must be integer"
}
check.errorf(&x, InvalidArrayLen, msg, &x)
return -1 return -1
} }
......
...@@ -494,13 +494,17 @@ func (check *Checker) arrayLength(e ast.Expr) int64 { ...@@ -494,13 +494,17 @@ func (check *Checker) arrayLength(e ast.Expr) int64 {
if n, ok := constant.Int64Val(val); ok && n >= 0 { if n, ok := constant.Int64Val(val); ok && n >= 0 {
return n return n
} }
check.errorf(&x, InvalidArrayLen, "invalid array length %s", &x)
return -1
} }
} }
} }
check.errorf(&x, InvalidArrayLen, "array length %s must be integer", &x) var msg string
if isInteger(x.typ) {
msg = "invalid array length %s"
} else {
msg = "array length %s must be integer"
}
check.errorf(&x, InvalidArrayLen, msg, &x)
return -1 return -1
} }
......
...@@ -55,7 +55,7 @@ type ( ...@@ -55,7 +55,7 @@ type (
// The error message below could be better. At the moment // The error message below could be better. At the moment
// we believe an integer that is too large is not an integer. // we believe an integer that is too large is not an integer.
// But at least we get an error. // But at least we get an error.
iA1 [1 /* ERROR "must be integer" */ <<100]int iA1 [1 /* ERROR "invalid array length" */ <<100]int
iA2 [- /* ERROR "invalid array length" */ 1]complex128 iA2 [- /* ERROR "invalid array length" */ 1]complex128
iA3 ["foo" /* ERROR "must be integer" */ ]string iA3 ["foo" /* ERROR "must be integer" */ ]string
iA4 [float64 /* ERROR "must be integer" */ (0)]int iA4 [float64 /* ERROR "must be integer" */ (0)]int
......
// Copyright 2023 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.
package p
type (
_ [1 /* ERROR "invalid array length" */ << 100]int
_ [1.0]int
_ [1.1 /* ERROR "must be integer" */ ]int
)
...@@ -13,7 +13,7 @@ var d ["abc"]int // ERROR "invalid array bound|not numeric|must be integer" ...@@ -13,7 +13,7 @@ var d ["abc"]int // ERROR "invalid array bound|not numeric|must be integer"
var e [nil]int // ERROR "use of untyped nil|invalid array (bound|length)|not numeric|must be constant" var e [nil]int // ERROR "use of untyped nil|invalid array (bound|length)|not numeric|must be constant"
// var f [e]int // ok with Go 1.17 because an error was reported for e; leads to an error for Go 1.18 // var f [e]int // ok with Go 1.17 because an error was reported for e; leads to an error for Go 1.18
var f [ee]int // ERROR "undefined|undeclared" var f [ee]int // ERROR "undefined|undeclared"
var g [1 << 65]int // ERROR "array bound is too large|overflows|must be integer" var g [1 << 65]int // ERROR "array bound is too large|overflows|invalid array length"
var h [len(a)]int // ok var h [len(a)]int // ok
func ff() string func ff() string
......
...@@ -7,8 +7,8 @@ ...@@ -7,8 +7,8 @@
package main package main
// "must be integer" error is for 32-bit architectures // "must be integer" error is for 32-bit architectures
type V [1 << 50]byte // ERROR "larger than address space|must be integer" type V [1 << 50]byte // ERROR "larger than address space|invalid array length"
var X [1 << 50]byte // ERROR "larger than address space|must be integer" var X [1 << 50]byte // ERROR "larger than address space|invalid array length"
func main() {} func main() {}
...@@ -10,4 +10,4 @@ package pkg ...@@ -10,4 +10,4 @@ package pkg
const Large uint64 = 18446744073709551615 const Large uint64 = 18446744073709551615
var foo [Large]uint64 // ERROR "array bound is too large|array bound overflows|array length.*must be integer" var foo [Large]uint64 // ERROR "array bound is too large|array bound overflows|invalid array length"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment