cmd/compile: allow composite literal structs with _ fields

Given code such as

type T struct {
  _ string
}

func f() {
  var x = T{"space"}
  // ...
}

the compiler rewrote the 'var x' line as

var x T
x._ = "space"

The compiler then rejected the assignment to
a blank field, thus rejecting valid code.

It also failed to catch a number of invalid assignments.
And there were insufficient checks for validity
when emitting static data, leading to ICEs.

To fix, check earlier for explicit blanks field names,
explicitly handle legit blanks in sinit,
and don't try to emit static data for nodes
for which typechecking has failed.

Fixes #19482

Change-Id: I594476171d15e6e8ecc6a1749e3859157fe2c929
Reviewed-on: https://go-review.googlesource.com/38006
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Josh Bleecher Snyder 2017-03-09 22:41:32 -08:00
parent 44a0681ae4
commit c9446398e8
4 changed files with 44 additions and 3 deletions

View file

@ -725,6 +725,9 @@ func fixedlit(ctxt initContext, kind initKind, n *Node, var_ *Node, init *Nodes)
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:
@ -1346,8 +1349,12 @@ func isvaluelit(n *Node) bool {
}
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)
}