]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: better error for generic type decl. with missing constraint
authorRobert Griesemer <gri@golang.org>
Thu, 6 Oct 2022 20:17:53 +0000 (13:17 -0700)
committerGopher Robot <gobot@golang.org>
Mon, 10 Oct 2022 15:17:24 +0000 (15:17 +0000)
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>

src/cmd/compile/internal/types2/typexpr.go
src/go/types/typexpr.go
src/internal/types/testdata/fixedbugs/issue43527.go

index 7375eed5c5ffb47a0090d46c022bcb3b536babc4..934336cf495c6ad6fae92a27ee8aed02f9f0cd88 100644 (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 {
index dcf6302d6f735593251ef5cc9e57a55d75c1c220..5d935192d05b7593cc7ff3dd96e92d0de2ba8c76 100644 (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 {
index f9ff4b016333198df9611ff9c74b5e81621bb0dd..b515100d3dfc6e16bc88f56ef34b27f7b485df72 100644 (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{}