From 641dedb5f9f95e6f8d46723d445a8c9609719ce4 Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Tue, 7 Feb 2023 12:08:30 -0800 Subject: [PATCH] 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 --- src/cmd/compile/internal/staticinit/sched.go | 12 +++--------- test/fixedbugs/issue58339.dir/a.go | 17 +++++++++++++++++ test/fixedbugs/issue58339.dir/b.go | 9 +++++++++ test/fixedbugs/issue58339.go | 7 +++++++ 4 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 test/fixedbugs/issue58339.dir/a.go create mode 100644 test/fixedbugs/issue58339.dir/b.go create mode 100644 test/fixedbugs/issue58339.go diff --git a/src/cmd/compile/internal/staticinit/sched.go b/src/cmd/compile/internal/staticinit/sched.go index f954c246f7..78524ae704 100644 --- a/src/cmd/compile/internal/staticinit/sched.go +++ b/src/cmd/compile/internal/staticinit/sched.go @@ -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 @@ -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() { @@ -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 } diff --git a/test/fixedbugs/issue58339.dir/a.go b/test/fixedbugs/issue58339.dir/a.go new file mode 100644 index 0000000000..22cbe0c6f9 --- /dev/null +++ b/test/fixedbugs/issue58339.dir/a.go @@ -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() +} diff --git a/test/fixedbugs/issue58339.dir/b.go b/test/fixedbugs/issue58339.dir/b.go new file mode 100644 index 0000000000..1736ec7adb --- /dev/null +++ b/test/fixedbugs/issue58339.dir/b.go @@ -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() diff --git a/test/fixedbugs/issue58339.go b/test/fixedbugs/issue58339.go new file mode 100644 index 0000000000..5c4c5c0a8f --- /dev/null +++ b/test/fixedbugs/issue58339.go @@ -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