]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: report struct type for literals with too few/many elements
authorRobert Griesemer <gri@golang.org>
Wed, 23 Mar 2022 04:28:32 +0000 (21:28 -0700)
committerRobert Griesemer <gri@golang.org>
Wed, 23 Mar 2022 18:22:52 +0000 (18:22 +0000)
This change essentially matches the 1.17 compiler error message for
this error.

Fixes #51877.

Change-Id: I24ec2f9cc93d8cd2283d097332a39bc1a0eed3a1
Reviewed-on: https://go-review.googlesource.com/c/go/+/394914
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/expr.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue51877.go [new file with mode: 0644]
src/go/types/expr.go
src/go/types/testdata/fixedbugs/issue51877.go [new file with mode: 0644]

index 7d2a7ba46bc16f4b159bd55a055199a50a3cb48e..23225c8d0d66fbee0fceec55c21c61f124134005 100644 (file)
@@ -1429,7 +1429,7 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
                                        }
                                        check.expr(x, e)
                                        if i >= len(fields) {
-                                               check.error(x, "too many values in struct literal")
+                                               check.errorf(x, "too many values in %s{…}", base)
                                                break // cannot continue
                                        }
                                        // i < len(fields)
@@ -1442,7 +1442,7 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
                                        check.assignment(x, etyp, "struct literal")
                                }
                                if len(e.ElemList) < len(fields) {
-                                       check.error(e.Rbrace, "too few values in struct literal")
+                                       check.errorf(e.Rbrace, "too few values in %s{…}", base)
                                        // ok to continue
                                }
                        }
diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue51877.go b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue51877.go
new file mode 100644 (file)
index 0000000..06f054b
--- /dev/null
@@ -0,0 +1,18 @@
+// Copyright 2013 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
+
+type S struct {
+       f1 int
+       f2 bool
+}
+
+var (
+       _ = S{0}                    /* ERROR too few values in S{…} */
+       _ = struct{ f1, f2 int }{0} /* ERROR too few values in struct{f1 int; f2 int}{…} */
+
+       _ = S{0, true, "foo" /* ERROR too many values in S{…} */}
+       _ = struct{ f1, f2 int }{0, 1, 2 /* ERROR too many values in struct{f1 int; f2 int}{…} */}
+)
index 160dcc35d01b2583112824548b4774406cb3bda6..a3c9041bddfba60d105427c79119f0d01bbfd5e0 100644 (file)
@@ -1404,7 +1404,7 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
                                        }
                                        check.expr(x, e)
                                        if i >= len(fields) {
-                                               check.error(x, _InvalidStructLit, "too many values in struct literal")
+                                               check.errorf(x, _InvalidStructLit, "too many values in %s{…}", base)
                                                break // cannot continue
                                        }
                                        // i < len(fields)
@@ -1419,7 +1419,7 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
                                        check.assignment(x, etyp, "struct literal")
                                }
                                if len(e.Elts) < len(fields) {
-                                       check.error(inNode(e, e.Rbrace), _InvalidStructLit, "too few values in struct literal")
+                                       check.errorf(inNode(e, e.Rbrace), _InvalidStructLit, "too few values in %s{…}", base)
                                        // ok to continue
                                }
                        }
diff --git a/src/go/types/testdata/fixedbugs/issue51877.go b/src/go/types/testdata/fixedbugs/issue51877.go
new file mode 100644 (file)
index 0000000..06f054b
--- /dev/null
@@ -0,0 +1,18 @@
+// Copyright 2013 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
+
+type S struct {
+       f1 int
+       f2 bool
+}
+
+var (
+       _ = S{0}                    /* ERROR too few values in S{…} */
+       _ = struct{ f1, f2 int }{0} /* ERROR too few values in struct{f1 int; f2 int}{…} */
+
+       _ = S{0, true, "foo" /* ERROR too many values in S{…} */}
+       _ = struct{ f1, f2 int }{0, 1, 2 /* ERROR too many values in struct{f1 int; f2 int}{…} */}
+)