1
0
mirror of https://github.com/golang/go synced 2024-11-26 00:07:57 -07:00

go/types, types2: better index-out-of-bounds error message (cleanup)

Use the 1.17 compiler error message, sans "array" prefix.

Change-Id: I0e70781c5ff02dca30a2004ab4d0ea82b0849eae
Reviewed-on: https://go-review.googlesource.com/c/go/+/396296
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Robert Griesemer 2022-03-28 15:30:37 -07:00
parent a2baae6851
commit 9038c24498
3 changed files with 3 additions and 7 deletions

View File

@ -368,11 +368,7 @@ func (check *Checker) index(index syntax.Expr, max int64) (typ Type, val int64)
v, ok := constant.Int64Val(x.val)
assert(ok)
if max >= 0 && v >= max {
if check.conf.CompilerErrorMessages {
check.errorf(&x, invalidArg+"array index %s out of bounds [0:%d]", x.val.String(), max)
} else {
check.errorf(&x, invalidArg+"index %s is out of bounds", &x)
}
check.errorf(&x, invalidArg+"index %s out of bounds [0:%d]", x.val.String(), max)
return
}

View File

@ -365,7 +365,7 @@ func (check *Checker) index(index ast.Expr, max int64) (typ Type, val int64) {
v, ok := constant.Int64Val(x.val)
assert(ok)
if max >= 0 && v >= max {
check.invalidArg(&x, _InvalidIndex, "index %s is out of bounds", &x)
check.invalidArg(&x, _InvalidIndex, "index %s out of bounds [0:%d]", x.val.String(), max)
return
}

View File

@ -16,7 +16,7 @@ func main() {
_ = [...]int{-1: 0} // ERROR "index must be non\-negative integer constant|index expression is negative|must not be negative"
_ = []int{100: 0}
_ = [10]int{100: 0} // ERROR "array index 100 out of bounds|out of range"
_ = [10]int{100: 0} // ERROR "index 100 out of bounds|out of range"
_ = [...]int{100: 0}
_ = []int{t} // ERROR "cannot use .* as (type )?int( in slice literal)?|incompatible type"