Skip to content

Commit 641dedb

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 golang#58339
1 parent f9da938 commit 641dedb

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

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

+3-9
Original file line numberDiff line numberDiff line change
@@ -864,13 +864,7 @@ func subst(n ir.Node, m map[*ir.Name]ir.Node) (ir.Node, bool) {
864864
x = ir.Copy(x)
865865
ir.EditChildrenWithHidden(x, edit)
866866
if x, ok := x.(*ir.ConvExpr); ok && x.X.Op() == ir.OLITERAL {
867-
// A conversion of variable or expression involving variables
868-
// may become a conversion of constant after inlining the parameters
869-
// and doing constant evaluation. Truncations that were valid
870-
// on variables are not valid on constants, so we might have
871-
// generated invalid code that will trip up the rest of the compiler.
872-
// Fix those by truncating the constants.
873-
if x, ok := truncate(x.X.(*ir.ConstExpr), x.Type()); ok {
867+
if x, ok := truncate(x.X, x.Type()); ok {
874868
return x
875869
}
876870
valid = false
@@ -885,7 +879,7 @@ func subst(n ir.Node, m map[*ir.Name]ir.Node) (ir.Node, bool) {
885879
// truncate returns the result of force converting c to type t,
886880
// truncating its value as needed, like a conversion of a variable.
887881
// If the conversion is too difficult, truncate returns nil, false.
888-
func truncate(c *ir.ConstExpr, t *types.Type) (*ir.ConstExpr, bool) {
882+
func truncate(c ir.Node, t *types.Type) (ir.Node, bool) {
889883
ct := c.Type()
890884
cv := c.Val()
891885
if ct.Kind() != t.Kind() {
@@ -907,7 +901,7 @@ func truncate(c *ir.ConstExpr, t *types.Type) (*ir.ConstExpr, bool) {
907901
}
908902
}
909903
}
910-
c = ir.NewConstExpr(cv, c).(*ir.ConstExpr)
904+
c = ir.NewConstExpr(cv, c)
911905
c.SetType(t)
912906
return c, true
913907
}

test/fixedbugs/issue58339.dir/a.go

+17
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

+9
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

+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)