]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: better error message for invalid array decls
authorRobert Griesemer <gri@golang.org>
Thu, 9 Sep 2021 05:08:14 +0000 (22:08 -0700)
committerRobert Griesemer <gri@golang.org>
Fri, 10 Sep 2021 16:51:52 +0000 (16:51 +0000)
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>
src/cmd/compile/internal/types2/testdata/fixedbugs/issue43527.go2 [new file with mode: 0644]
src/cmd/compile/internal/types2/typexpr.go
src/go/types/testdata/fixedbugs/issue43527.go2 [new file with mode: 0644]
src/go/types/typexpr.go

diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue43527.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue43527.go2
new file mode 100644 (file)
index 0000000..e4bcee5
--- /dev/null
@@ -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
+)
index f3db3bbba936014ac47d26c1c2e7ad6fdf09a894..5aacb94a605c756e0755bd21c5cf40e94ae33aa8 100644 (file)
@@ -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
 // to indicate an error (and thus an unknown length).
 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
        check.expr(&x, e)
        if x.mode != constant_ {
@@ -436,6 +444,7 @@ func (check *Checker) arrayLength(e syntax.Expr) int64 {
                }
                return -1
        }
+
        if isUntyped(x.typ) || isInteger(x.typ) {
                if val := constant.ToInt(x.val); val.Kind() == constant.Int {
                        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)
        return -1
 }
diff --git a/src/go/types/testdata/fixedbugs/issue43527.go2 b/src/go/types/testdata/fixedbugs/issue43527.go2
new file mode 100644 (file)
index 0000000..e4bcee5
--- /dev/null
@@ -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
+)
index 6b4a3538b610b0688c98294f2e5207210af0c1cb..0143f53009e77d1ae7a02b2f6f2a0bc4d9800663 100644 (file)
@@ -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
 // to indicate an error (and thus an unknown length).
 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
        check.expr(&x, e)
        if x.mode != constant_ {
@@ -420,6 +428,7 @@ func (check *Checker) arrayLength(e ast.Expr) int64 {
                }
                return -1
        }
+
        if isUntyped(x.typ) || isInteger(x.typ) {
                if val := constant.ToInt(x.val); val.Kind() == constant.Int {
                        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)
        return -1
 }