Skip to content

Commit f6b2620

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 f6b2620

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
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

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

test/fixedbugs/issue58339.dir/b.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2023 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package b
6+
7+
import "./a"
8+
9+
var A = a.Run2()

test/fixedbugs/issue58339.go

Lines changed: 7 additions & 0 deletions
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)