Skip to content

Commit 3161081

Browse files
sywhanggopherbot
authored andcommitted
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 Change-Id: I357069d7ee1707d5cc6811bd2fbdd7b0456323ae GitHub-Last-Rev: 641dedb GitHub-Pull-Request: #58389 Reviewed-on: https://go-review.googlesource.com/c/go/+/466175 Reviewed-by: Matthew Dempsky <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]> Auto-Submit: Cuong Manh Le <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent f3e0f1c commit 3161081

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
@@ -867,13 +867,7 @@ func subst(n ir.Node, m map[*ir.Name]ir.Node) (ir.Node, bool) {
867867
x = ir.Copy(x)
868868
ir.EditChildrenWithHidden(x, edit)
869869
if x, ok := x.(*ir.ConvExpr); ok && x.X.Op() == ir.OLITERAL {
870-
// A conversion of variable or expression involving variables
871-
// may become a conversion of constant after inlining the parameters
872-
// and doing constant evaluation. Truncations that were valid
873-
// on variables are not valid on constants, so we might have
874-
// generated invalid code that will trip up the rest of the compiler.
875-
// Fix those by truncating the constants.
876-
if x, ok := truncate(x.X.(*ir.ConstExpr), x.Type()); ok {
870+
if x, ok := truncate(x.X, x.Type()); ok {
877871
return x
878872
}
879873
valid = false
@@ -888,7 +882,7 @@ func subst(n ir.Node, m map[*ir.Name]ir.Node) (ir.Node, bool) {
888882
// truncate returns the result of force converting c to type t,
889883
// truncating its value as needed, like a conversion of a variable.
890884
// If the conversion is too difficult, truncate returns nil, false.
891-
func truncate(c *ir.ConstExpr, t *types.Type) (*ir.ConstExpr, bool) {
885+
func truncate(c ir.Node, t *types.Type) (ir.Node, bool) {
892886
ct := c.Type()
893887
cv := c.Val()
894888
if ct.Kind() != t.Kind() {
@@ -910,7 +904,7 @@ func truncate(c *ir.ConstExpr, t *types.Type) (*ir.ConstExpr, bool) {
910904
}
911905
}
912906
}
913-
c = ir.NewConstExpr(cv, c).(*ir.ConstExpr)
907+
c = ir.NewConstExpr(cv, c)
914908
c.SetType(t)
915909
return c, true
916910
}

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)