Skip to content

Commit bf9d9b7

Browse files
griesemergopherbot
authored andcommitted
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: Robert Griesemer <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent d49b11b commit bf9d9b7

File tree

7 files changed

+30
-11
lines changed

7 files changed

+30
-11
lines changed

src/cmd/compile/internal/types2/typexpr.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,13 +503,17 @@ func (check *Checker) arrayLength(e syntax.Expr) int64 {
503503
if n, ok := constant.Int64Val(val); ok && n >= 0 {
504504
return n
505505
}
506-
check.errorf(&x, InvalidArrayLen, "invalid array length %s", &x)
507-
return -1
508506
}
509507
}
510508
}
511509

512-
check.errorf(&x, InvalidArrayLen, "array length %s must be integer", &x)
510+
var msg string
511+
if isInteger(x.typ) {
512+
msg = "invalid array length %s"
513+
} else {
514+
msg = "array length %s must be integer"
515+
}
516+
check.errorf(&x, InvalidArrayLen, msg, &x)
513517
return -1
514518
}
515519

src/go/types/typexpr.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,13 +494,17 @@ func (check *Checker) arrayLength(e ast.Expr) int64 {
494494
if n, ok := constant.Int64Val(val); ok && n >= 0 {
495495
return n
496496
}
497-
check.errorf(&x, InvalidArrayLen, "invalid array length %s", &x)
498-
return -1
499497
}
500498
}
501499
}
502500

503-
check.errorf(&x, InvalidArrayLen, "array length %s must be integer", &x)
501+
var msg string
502+
if isInteger(x.typ) {
503+
msg = "invalid array length %s"
504+
} else {
505+
msg = "array length %s must be integer"
506+
}
507+
check.errorf(&x, InvalidArrayLen, msg, &x)
504508
return -1
505509
}
506510

src/internal/types/testdata/check/decls0.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type (
5555
// The error message below could be better. At the moment
5656
// we believe an integer that is too large is not an integer.
5757
// But at least we get an error.
58-
iA1 [1 /* ERROR "must be integer" */ <<100]int
58+
iA1 [1 /* ERROR "invalid array length" */ <<100]int
5959
iA2 [- /* ERROR "invalid array length" */ 1]complex128
6060
iA3 ["foo" /* ERROR "must be integer" */ ]string
6161
iA4 [float64 /* ERROR "must be integer" */ (0)]int
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2023 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package p
6+
7+
type (
8+
_ [1 /* ERROR "invalid array length" */ << 100]int
9+
_ [1.0]int
10+
_ [1.1 /* ERROR "must be integer" */ ]int
11+
)

test/fixedbugs/bug255.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var d ["abc"]int // ERROR "invalid array bound|not numeric|must be integer"
1313
var e [nil]int // ERROR "use of untyped nil|invalid array (bound|length)|not numeric|must be constant"
1414
// var f [e]int // ok with Go 1.17 because an error was reported for e; leads to an error for Go 1.18
1515
var f [ee]int // ERROR "undefined|undeclared"
16-
var g [1 << 65]int // ERROR "array bound is too large|overflows|must be integer"
16+
var g [1 << 65]int // ERROR "array bound is too large|overflows|invalid array length"
1717
var h [len(a)]int // ok
1818

1919
func ff() string

test/fixedbugs/issue49814.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
package main
88

99
// "must be integer" error is for 32-bit architectures
10-
type V [1 << 50]byte // ERROR "larger than address space|must be integer"
10+
type V [1 << 50]byte // ERROR "larger than address space|invalid array length"
1111

12-
var X [1 << 50]byte // ERROR "larger than address space|must be integer"
12+
var X [1 << 50]byte // ERROR "larger than address space|invalid array length"
1313

1414
func main() {}

test/fixedbugs/issue5609.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ package pkg
1010

1111
const Large uint64 = 18446744073709551615
1212

13-
var foo [Large]uint64 // ERROR "array bound is too large|array bound overflows|array length.*must be integer"
13+
var foo [Large]uint64 // ERROR "array bound is too large|array bound overflows|invalid array length"

0 commit comments

Comments
 (0)