]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/staticinit: fix bug in global new(expr)
authorAlan Donovan <adonovan@google.com>
Tue, 20 Jan 2026 21:18:59 +0000 (16:18 -0500)
committerAlan Donovan <adonovan@google.com>
Tue, 20 Jan 2026 21:48:41 +0000 (13:48 -0800)
The StaticInit pass asserts that the operand of &v is a global,
but this is not so for the &autotemp desugaring of new(expr).

(The variable has by that point escaped to the heap, so
the object code calls runtime.newobject. A future optimization
would be to statically allocate the variable when it is safe
and advantageous to do so.)

Thanks to khr for suggesting the fix.

+ static test

Fixes #77237

Change-Id: I71b34a1353fe0f3e297beab9851f8f87d765d8f1
Reviewed-on: https://go-review.googlesource.com/c/go/+/737680
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/cmd/compile/internal/staticinit/sched.go
test/newexpr.go

index 5e39bb512f45f4cfe251ede6b244c4d665eb4605..c79715be4699c26d30e37438f4e1ddaf4344e8eb 100644 (file)
@@ -228,6 +228,9 @@ func (s *Schedule) staticcopy(l *ir.Name, loff int64, rn *ir.Name, typ *types.Ty
        case ir.OADDR:
                r := r.(*ir.AddrExpr)
                if a, ok := r.X.(*ir.Name); ok && a.Op() == ir.ONAME {
+                       if a.Class != ir.PEXTERN {
+                               return false // e.g. local from new(expr)
+                       }
                        staticdata.InitAddr(l, loff, staticdata.GlobalLinksym(a))
                        return true
                }
index c9a8804d4eb31ab6775b9a97141b12978625d30e..00d753c57267357c0b01bdc68e32fedd9cf9a774 100644 (file)
@@ -37,3 +37,10 @@ func main() {
                }
        }
 }
+
+// Regression test for ICE in staticdata.GlobalLinksym from
+// use of autotemp outside a function (go.dev/issue/77237).
+var (
+       x = new(0)
+       y = x
+)