if r.Op != OSTRUCTKEY {
Fatalf("fixedlit: rhs not OSTRUCTKEY: %v", r)
}
+ if isblanksym(r.Sym) {
+ return nblank, r.Left
+ }
return nodSym(ODOT, var_, r.Sym), r.Left
}
default:
}
func genAsStatic(as *Node) {
+ if as.Left.Type == nil {
+ Fatalf("genAsStatic as.Left not typechecked")
+ }
+
var nam Node
- if !stataddr(&nam, as.Left) || nam.Class != PEXTERN {
+ if !stataddr(&nam, as.Left) || (nam.Class != PEXTERN && as.Left != nblank) {
Fatalf("genAsStatic: lhs %v", as.Left)
}
// the field to the right of the dot,
// so s will be non-nil, but an OXDOT
// is never a valid struct literal key.
- if key.Sym == nil || key.Op == OXDOT {
+ if key.Sym == nil || key.Op == OXDOT || isblanksym(key.Sym) {
yyerror("invalid field name %v in struct initializer", key)
l.Left = typecheck(l.Left, Erv)
continue
// prepwrite prepares to write data of size siz into s at offset off.
func (s *LSym) prepwrite(ctxt *Link, off int64, siz int) {
if off < 0 || siz < 0 || off >= 1<<30 {
- log.Fatalf("prepwrite: bad off=%d siz=%d s=%v", off, siz, s)
+ ctxt.Diag("prepwrite: bad off=%d siz=%d s=%v", off, siz, s)
}
if s.Type == SBSS || s.Type == STLSBSS {
ctxt.Diag("cannot supply data for BSS var")
--- /dev/null
+// errorcheck
+
+// Copyright 2017 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.
+
+// Compiler rejected initialization of structs to composite literals
+// in a non-static setting (e.g. in a function)
+// when the struct contained a field named _.
+
+package p
+
+type T struct {
+ _ string
+}
+
+func ok() {
+ var x = T{"check"}
+ _ = x
+ _ = T{"et"}
+}
+
+var (
+ y = T{"stare"}
+ w = T{_: "look"} // ERROR "invalid field name _ in struct initializer"
+ _ = T{"page"}
+ _ = T{_: "out"} // ERROR "invalid field name _ in struct initializer"
+)
+
+func bad() {
+ var z = T{_: "verse"} // ERROR "invalid field name _ in struct initializer"
+ _ = z
+ _ = T{_: "itinerary"} // ERROR "invalid field name _ in struct initializer"
+}