]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: another fix initializing blank fields in struct literal
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Wed, 6 May 2020 17:35:28 +0000 (00:35 +0700)
committerCuong Manh Le <cuong.manhle.vn@gmail.com>
Sat, 15 Aug 2020 03:09:21 +0000 (03:09 +0000)
CL 230121 fixed the bug that struct literal blank fields type array/struct
can not be initialized. But it still misses some cases when an expression
causes "candiscard(value)" return false. When these happen, we recursively
call fixedlit with "var_" set to "_", and hit the bug again.

To fix it, just making splitnode return "nblank" whenever "var_" is "nblank".

Fixes #38905

Change-Id: I281941b388acbd551a4d8ca1a235477f8d26fb6e
Reviewed-on: https://go-review.googlesource.com/c/go/+/232617
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/gc/sinit.go
test/fixedbugs/issue38905.go [new file with mode: 0644]

index 4a2edc7d21555bed11a5504589eb9692da33d8f3..71ed5584612f9263fe1b351752d78466131094f4 100644 (file)
@@ -506,6 +506,7 @@ const (
 // fixedlit handles struct, array, and slice literals.
 // TODO: expand documentation.
 func fixedlit(ctxt initContext, kind initKind, n *Node, var_ *Node, init *Nodes) {
+       isBlank := var_ == nblank
        var splitnode func(*Node) (a *Node, value *Node)
        switch n.Op {
        case OARRAYLIT, OSLICELIT:
@@ -520,6 +521,9 @@ func fixedlit(ctxt initContext, kind initKind, n *Node, var_ *Node, init *Nodes)
                        }
                        a := nod(OINDEX, var_, nodintconst(k))
                        k++
+                       if isBlank {
+                               a = nblank
+                       }
                        return a, r
                }
        case OSTRUCTLIT:
@@ -527,7 +531,7 @@ func fixedlit(ctxt initContext, kind initKind, n *Node, var_ *Node, init *Nodes)
                        if r.Op != OSTRUCTKEY {
                                Fatalf("fixedlit: rhs not OSTRUCTKEY: %v", r)
                        }
-                       if r.Sym.IsBlank() {
+                       if r.Sym.IsBlank() || isBlank {
                                return nblank, r.Left
                        }
                        setlineno(r)
diff --git a/test/fixedbugs/issue38905.go b/test/fixedbugs/issue38905.go
new file mode 100644 (file)
index 0000000..6f411b8
--- /dev/null
@@ -0,0 +1,18 @@
+// compile
+
+// Copyright 2020 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.
+
+// Make sure that literal value can be passed to struct
+// blank field with expressions where candiscard(value)
+// returns false, see #38905.
+
+package p
+
+type t struct{ _ u }
+type u [10]struct{ f int }
+
+func f(x int) t   { return t{u{{1 / x}, {1 % x}}} }
+func g(p *int) t  { return t{u{{*p}}} }
+func h(s []int) t { return t{u{{s[0]}}} }