From: Alan Donovan Date: Tue, 20 Jan 2026 21:18:59 +0000 (-0500) Subject: cmd/compile/internal/staticinit: fix bug in global new(expr) X-Git-Tag: go1.26rc3~11^2~1 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=a8291eb61402d4549b69803fc8f834ded45b1f6c;p=gostls13.git cmd/compile/internal/staticinit: fix bug in global new(expr) 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 Reviewed-by: Keith Randall LUCI-TryBot-Result: Go LUCI --- diff --git a/src/cmd/compile/internal/staticinit/sched.go b/src/cmd/compile/internal/staticinit/sched.go index 5e39bb512f..c79715be46 100644 --- a/src/cmd/compile/internal/staticinit/sched.go +++ b/src/cmd/compile/internal/staticinit/sched.go @@ -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 } diff --git a/test/newexpr.go b/test/newexpr.go index c9a8804d4e..00d753c572 100644 --- a/test/newexpr.go +++ b/test/newexpr.go @@ -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 +)