mirror of
https://github.com/golang/go
synced 2024-11-17 18:54:42 -07:00
cmd/compile/internal/types2: better error message for invalid array decls
Fixes #43527. Change-Id: I988a4d49f2f54b4b1741688fb52a55bf313d39e1 Reviewed-on: https://go-review.googlesource.com/c/go/+/348731 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
parent
da1aa65053
commit
5a94a90d84
16
src/cmd/compile/internal/types2/testdata/fixedbugs/issue43527.go2
vendored
Normal file
16
src/cmd/compile/internal/types2/testdata/fixedbugs/issue43527.go2
vendored
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
// Copyright 2021 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
|
||||||
|
|
||||||
|
const L = 10
|
||||||
|
|
||||||
|
type (
|
||||||
|
_ [L]struct{}
|
||||||
|
_ [A /* ERROR undeclared name A for array length */ ]struct{}
|
||||||
|
_ [B /* ERROR not an expression */ ]struct{}
|
||||||
|
_[A any] struct{}
|
||||||
|
|
||||||
|
B int
|
||||||
|
)
|
@ -428,6 +428,14 @@ func (check *Checker) instantiatedType(x syntax.Expr, targsx []syntax.Expr, def
|
|||||||
// and returns the constant length >= 0, or a value < 0
|
// and returns the constant length >= 0, or a value < 0
|
||||||
// to indicate an error (and thus an unknown length).
|
// to indicate an error (and thus an unknown length).
|
||||||
func (check *Checker) arrayLength(e syntax.Expr) int64 {
|
func (check *Checker) arrayLength(e syntax.Expr) int64 {
|
||||||
|
// If e is an undeclared identifier, the array declaration might be an
|
||||||
|
// attempt at a parameterized type declaration with missing constraint.
|
||||||
|
// Provide a better error message than just "undeclared name: X".
|
||||||
|
if name, _ := e.(*syntax.Name); name != nil && check.lookup(name.Value) == nil {
|
||||||
|
check.errorf(name, "undeclared name %s for array length", name.Value)
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
var x operand
|
var x operand
|
||||||
check.expr(&x, e)
|
check.expr(&x, e)
|
||||||
if x.mode != constant_ {
|
if x.mode != constant_ {
|
||||||
@ -436,6 +444,7 @@ func (check *Checker) arrayLength(e syntax.Expr) int64 {
|
|||||||
}
|
}
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
if isUntyped(x.typ) || isInteger(x.typ) {
|
if isUntyped(x.typ) || isInteger(x.typ) {
|
||||||
if val := constant.ToInt(x.val); val.Kind() == constant.Int {
|
if val := constant.ToInt(x.val); val.Kind() == constant.Int {
|
||||||
if representableConst(val, check, Typ[Int], nil) {
|
if representableConst(val, check, Typ[Int], nil) {
|
||||||
@ -447,6 +456,7 @@ func (check *Checker) arrayLength(e syntax.Expr) int64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
check.errorf(&x, "array length %s must be integer", &x)
|
check.errorf(&x, "array length %s must be integer", &x)
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
16
src/go/types/testdata/fixedbugs/issue43527.go2
vendored
Normal file
16
src/go/types/testdata/fixedbugs/issue43527.go2
vendored
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
// Copyright 2021 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
|
||||||
|
|
||||||
|
const L = 10
|
||||||
|
|
||||||
|
type (
|
||||||
|
_ [L]struct{}
|
||||||
|
_ [A /* ERROR undeclared name A for array length */ ]struct{}
|
||||||
|
_ [B /* ERROR not an expression */ ]struct{}
|
||||||
|
_[A any] struct{}
|
||||||
|
|
||||||
|
B int
|
||||||
|
)
|
@ -412,6 +412,14 @@ func (check *Checker) instantiatedType(x ast.Expr, targsx []ast.Expr, def *Named
|
|||||||
// and returns the constant length >= 0, or a value < 0
|
// and returns the constant length >= 0, or a value < 0
|
||||||
// to indicate an error (and thus an unknown length).
|
// to indicate an error (and thus an unknown length).
|
||||||
func (check *Checker) arrayLength(e ast.Expr) int64 {
|
func (check *Checker) arrayLength(e ast.Expr) int64 {
|
||||||
|
// If e is an undeclared identifier, the array declaration might be an
|
||||||
|
// attempt at a parameterized type declaration with missing constraint.
|
||||||
|
// Provide a better error message than just "undeclared name: X".
|
||||||
|
if name, _ := e.(*ast.Ident); name != nil && check.lookup(name.Name) == nil {
|
||||||
|
check.errorf(name, _InvalidArrayLen, "undeclared name %s for array length", name.Name)
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
var x operand
|
var x operand
|
||||||
check.expr(&x, e)
|
check.expr(&x, e)
|
||||||
if x.mode != constant_ {
|
if x.mode != constant_ {
|
||||||
@ -420,6 +428,7 @@ func (check *Checker) arrayLength(e ast.Expr) int64 {
|
|||||||
}
|
}
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
if isUntyped(x.typ) || isInteger(x.typ) {
|
if isUntyped(x.typ) || isInteger(x.typ) {
|
||||||
if val := constant.ToInt(x.val); val.Kind() == constant.Int {
|
if val := constant.ToInt(x.val); val.Kind() == constant.Int {
|
||||||
if representableConst(val, check, Typ[Int], nil) {
|
if representableConst(val, check, Typ[Int], nil) {
|
||||||
@ -431,6 +440,7 @@ func (check *Checker) arrayLength(e ast.Expr) int64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
check.errorf(&x, _InvalidArrayLen, "array length %s must be integer", &x)
|
check.errorf(&x, _InvalidArrayLen, "array length %s must be integer", &x)
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user