Files
go/test/newexpr.go
Alan Donovan a8291eb614 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 <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2026-01-20 13:48:41 -08:00

47 lines
863 B
Go

// run
// Copyright 2025 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.
package main
// Issue #45624 is the proposal to accept new(expr) in go1.26.
// Here we test its run-time behavior.
func main() {
{
p := new(123) // untyped constant expr
if *p != 123 {
panic("wrong value")
}
}
{
x := 42
p := new(x) // non-constant expr
if *p != x {
panic("wrong value")
}
}
{
x := [2]int{123, 456}
p := new(x) // composite value
if *p != x {
panic("wrong value")
}
}
{
var i int
v := new(i > 0) // untyped expression, see issue #75617
if *v != false {
panic("wrong value")
}
}
}
// Regression test for ICE in staticdata.GlobalLinksym from
// use of autotemp outside a function (go.dev/issue/77237).
var (
x = new(0)
y = x
)