Fixes #51879.
Change-Id: Ic7ac892b82a0fe4ad6f95ff8ae84e6d30c52c111
Reviewed-on: https://go-review.googlesource.com/c/go/+/438855
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
if len(e.ElemList) == 0 {
break
}
+ // Convention for error messages on invalid struct literals:
+ // we mention the struct type only if it clarifies the error
+ // (e.g., a duplicate field error doesn't need the struct type).
fields := utyp.fields
if _, ok := e.ElemList[0].(*syntax.KeyValueExpr); ok {
// all elements must have keys
}
check.expr(x, e)
if i >= len(fields) {
- check.errorf(x, _InvalidStructLit, "too many values in %s{…}", base)
+ check.errorf(x, _InvalidStructLit, "too many values in struct literal of type %s", base)
break // cannot continue
}
// i < len(fields)
fld := fields[i]
if !fld.Exported() && fld.pkg != check.pkg {
- check.errorf(x, _UnexportedLitField, "implicit assignment to unexported field %s in %s literal", fld.name, typ)
+ check.errorf(x, _UnexportedLitField, "implicit assignment to unexported field %s in struct literal of type %s", fld.name, base)
continue
}
etyp := fld.typ
check.assignment(x, etyp, "struct literal")
}
if len(e.ElemList) < len(fields) {
- check.errorf(e.Rbrace, _InvalidStructLit, "too few values in %s{…}", base)
+ check.errorf(e.Rbrace, _InvalidStructLit, "too few values in struct literal of type %s", base)
// ok to continue
}
}
if len(e.Elts) == 0 {
break
}
+ // Convention for error messages on invalid struct literals:
+ // we mention the struct type only if it clarifies the error
+ // (e.g., a duplicate field error doesn't need the struct type).
fields := utyp.fields
if _, ok := e.Elts[0].(*ast.KeyValueExpr); ok {
// all elements must have keys
}
check.expr(x, e)
if i >= len(fields) {
- check.errorf(x, _InvalidStructLit, "too many values in %s{…}", base)
+ check.errorf(x, _InvalidStructLit, "too many values in struct literal of type %s", base)
break // cannot continue
}
// i < len(fields)
if !fld.Exported() && fld.pkg != check.pkg {
check.errorf(x,
_UnexportedLitField,
- "implicit assignment to unexported field %s in %s literal", fld.name, typ)
+ "implicit assignment to unexported field %s in struct literal of type %s", fld.name, base)
continue
}
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 %s{…}", base)
+ check.errorf(inNode(e, e.Rbrace), _InvalidStructLit, "too few values in struct literal of type %s", base)
// ok to continue
}
}
_ = time.Time{}
_ = time.Time{sec /* ERROR "unknown field" */ : 0}
_ = time.Time{
- 0 /* ERROR implicit assignment to unexported field wall in time.Time literal */,
+ 0 /* ERROR implicit assignment to unexported field wall in struct literal */,
0 /* ERROR implicit assignment */ ,
nil /* ERROR implicit assignment */ ,
}
}
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} /* ERROR too few values in struct literal */
+ _ = struct{ f1, f2 int }{0} /* ERROR too few values in struct literal */
- _ = 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}{…} */}
+ _ = S{0, true, "foo" /* ERROR too many values in struct literal */}
+ _ = struct{ f1, f2 int }{0, 1, 2 /* ERROR too many values in struct literal */}
)