1
0
mirror of https://github.com/golang/go synced 2024-11-17 16:14:42 -07:00

go/types, types2: better error for generic type decl. with missing constraint

If a generic type declaration is missing a constraint, syntactically
it is an array type declaration with an undefined array length.
Mention the possibility of a missing constraint in the error message
for the undefined array length.

For #56064.
For #55961.
For #51145.

Change-Id: Ic161aeda9ea44faa8aa3bf3e9d62b3b13a95d4c5
Reviewed-on: https://go-review.googlesource.com/c/go/+/439559
Run-TryBot: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
This commit is contained in:
Robert Griesemer 2022-10-06 13:17:53 -07:00 committed by Gopher Robot
parent 213504e543
commit 665992b515
3 changed files with 3 additions and 3 deletions

View File

@ -478,7 +478,7 @@ func (check *Checker) arrayLength(e syntax.Expr) int64 {
if name, _ := e.(*syntax.Name); name != nil {
obj := check.lookup(name.Value)
if obj == nil {
check.errorf(name, _InvalidArrayLen, "undefined %s for array length", name.Value)
check.errorf(name, _InvalidArrayLen, "undefined array length %s or missing type constraint", name.Value)
return -1
}
if _, ok := obj.(*Const); !ok {

View File

@ -469,7 +469,7 @@ func (check *Checker) arrayLength(e ast.Expr) int64 {
if name, _ := e.(*ast.Ident); name != nil {
obj := check.lookup(name.Name)
if obj == nil {
check.errorf(name, _InvalidArrayLen, "undefined %s for array length", name.Name)
check.errorf(name, _InvalidArrayLen, "undefined array length %s or missing type constraint", name.Name)
return -1
}
if _, ok := obj.(*Const); !ok {

View File

@ -8,7 +8,7 @@ const L = 10
type (
_ [L]struct{}
_ [A /* ERROR undefined A for array length */ ]struct{}
_ [A /* ERROR undefined array length A or missing type constraint */ ]struct{}
_ [B /* ERROR invalid array length B */ ]struct{}
_[A any] struct{}