src/cmd/compile/internal/types2/literals.go | 9 ++++++++- src/go/types/literals.go | 9 ++++++++- test/fixedbugs/issue28053.dir/a.go | 10 ++++++++++ test/fixedbugs/issue28053.dir/main.go | 11 +++++++++++ test/fixedbugs/issue28053.go | 7 +++++++ diff --git a/src/cmd/compile/internal/types2/literals.go b/src/cmd/compile/internal/types2/literals.go index ecd2c209fe2260c6035fb05967d536d8e96d5119..3cef03626959540b6d41aade9a240d6b5a18170f 100644 --- a/src/cmd/compile/internal/types2/literals.go +++ b/src/cmd/compile/internal/types2/literals.go @@ -229,7 +229,14 @@ etyp := fld.typ check.assignment(x, etyp, "struct literal") } if len(e.ElemList) < len(fields) { - check.errorf(inNode(e, e.Rbrace), InvalidStructLit, "too few values in struct literal of type %s", base) + var hint string + for _, fld := range fields { + if !fld.Exported() && fld.pkg != check.pkg { + hint = " (type has unexported fields - use key:value pairs)" + break + } + } + check.errorf(inNode(e, e.Rbrace), InvalidStructLit, "too few values in struct literal of type %s%s", base, hint) // ok to continue } } diff --git a/src/go/types/literals.go b/src/go/types/literals.go index b0f360e43c176d6ae14cdac5f547395710769641..5b5fcf64efc24d4e9ecdd151b70eb898d7327096 100644 --- a/src/go/types/literals.go +++ b/src/go/types/literals.go @@ -233,7 +233,14 @@ etyp := fld.typ check.assignment(x, etyp, "struct literal") } if len(e.Elts) < len(fields) { - check.errorf(inNode(e, e.Rbrace), InvalidStructLit, "too few values in struct literal of type %s", base) + var hint string + for _, fld := range fields { + if !fld.Exported() && fld.pkg != check.pkg { + hint = " (type has unexported fields - use key:value pairs)" + break + } + } + check.errorf(inNode(e, e.Rbrace), InvalidStructLit, "too few values in struct literal of type %s%s", base, hint) // ok to continue } } diff --git a/test/fixedbugs/issue28053.dir/a.go b/test/fixedbugs/issue28053.dir/a.go new file mode 100644 index 0000000000000000000000000000000000000000..d502011de6bd2638fa93b2cb6cc4736666d11079 --- /dev/null +++ b/test/fixedbugs/issue28053.dir/a.go @@ -0,0 +1,10 @@ +// Copyright 2026 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 { + Exported int + unexported int +} \ No newline at end of file diff --git a/test/fixedbugs/issue28053.dir/main.go b/test/fixedbugs/issue28053.dir/main.go new file mode 100644 index 0000000000000000000000000000000000000000..d6f2a8fa3890742c18569585acc43fb9319cf4e4 --- /dev/null +++ b/test/fixedbugs/issue28053.dir/main.go @@ -0,0 +1,11 @@ +// Copyright 2026 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 main + +import "./a" + +func main() { + _ = p.S{1} // ERROR "too few values.*unexported fields" +} diff --git a/test/fixedbugs/issue28053.go b/test/fixedbugs/issue28053.go new file mode 100644 index 0000000000000000000000000000000000000000..511704dad03bcf690c01d0b2f14982fe6e50ad04 --- /dev/null +++ b/test/fixedbugs/issue28053.go @@ -0,0 +1,7 @@ +// errorcheckdir + +// Copyright 2026 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 ignored