Skip to content

cmd/compile/internal/staticinit: fix panic in interface conversion #58389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions src/cmd/compile/internal/staticinit/sched.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,13 +864,7 @@ func subst(n ir.Node, m map[*ir.Name]ir.Node) (ir.Node, bool) {
x = ir.Copy(x)
ir.EditChildrenWithHidden(x, edit)
if x, ok := x.(*ir.ConvExpr); ok && x.X.Op() == ir.OLITERAL {
// A conversion of variable or expression involving variables
// may become a conversion of constant after inlining the parameters
// and doing constant evaluation. Truncations that were valid
// on variables are not valid on constants, so we might have
// generated invalid code that will trip up the rest of the compiler.
// Fix those by truncating the constants.
if x, ok := truncate(x.X.(*ir.ConstExpr), x.Type()); ok {
if x, ok := truncate(x.X, x.Type()); ok {
return x
}
valid = false
Expand All @@ -885,7 +879,7 @@ func subst(n ir.Node, m map[*ir.Name]ir.Node) (ir.Node, bool) {
// truncate returns the result of force converting c to type t,
// truncating its value as needed, like a conversion of a variable.
// If the conversion is too difficult, truncate returns nil, false.
func truncate(c *ir.ConstExpr, t *types.Type) (*ir.ConstExpr, bool) {
func truncate(c ir.Node, t *types.Type) (ir.Node, bool) {
ct := c.Type()
cv := c.Val()
if ct.Kind() != t.Kind() {
Expand All @@ -907,7 +901,7 @@ func truncate(c *ir.ConstExpr, t *types.Type) (*ir.ConstExpr, bool) {
}
}
}
c = ir.NewConstExpr(cv, c).(*ir.ConstExpr)
c = ir.NewConstExpr(cv, c)
c.SetType(t)
return c, true
}
Expand Down
17 changes: 17 additions & 0 deletions test/fixedbugs/issue58339.dir/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2023 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 a

func Assert(msgAndArgs ...any) {
}

func Run() int {
Assert("%v")
return 0
}

func Run2() int {
return Run()
}
9 changes: 9 additions & 0 deletions test/fixedbugs/issue58339.dir/b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2023 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 b

import "./a"

var A = a.Run2()
7 changes: 7 additions & 0 deletions test/fixedbugs/issue58339.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// compiledir

// Copyright 2023 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 ignored