Skip to content

Commit 82b7452

Browse files
committed
cmd/compile/internal/staticinit: fix panic in interface conversion
This patch fixes a panic from incorrect interface conversion from *ir.BasicLit to *ir.ConstExpr. This only occurs when nounified GOEXPERIMENT is set, so ideally it should be backported to Go 1.20 and removed from master. Fixes #58339
1 parent f9da938 commit 82b7452

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -870,8 +870,10 @@ func subst(n ir.Node, m map[*ir.Name]ir.Node) (ir.Node, bool) {
870870
// on variables are not valid on constants, so we might have
871871
// generated invalid code that will trip up the rest of the compiler.
872872
// Fix those by truncating the constants.
873-
if x, ok := truncate(x.X.(*ir.ConstExpr), x.Type()); ok {
874-
return x
873+
if ce, ok := x.X.(*ir.ConstExpr); ok {
874+
if x, ok := truncate(ce, x.Type()); ok {
875+
return x
876+
}
875877
}
876878
valid = false
877879
return x

test/fixedbugs/issue58339.dir/a.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// compile -goexperiment nounified
2+
3+
// Copyright 2023 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package a
8+
9+
func Assert(msgAndArgs ...any) {
10+
}
11+
12+
func Run() int {
13+
Assert("%v")
14+
return 0
15+
}
16+
17+
func Run2() int {
18+
return Run()
19+
}

test/fixedbugs/issue58339.dir/b.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// compile -goexperiment nounified
2+
3+
// Copyright 2023 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package b
8+
9+
import "./a"
10+
11+
var A = a.Run2()
12+
13+
func main() {
14+
}

test/fixedbugs/issue58339.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// compiledir
2+
3+
// Copyright 2023 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package ignored

0 commit comments

Comments
 (0)