mirror of
https://github.com/golang/go.git
synced 2025-05-05 23:53:05 +00:00
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: 641dedb5f9f95e6f8d46723d445a8c9609719ce4 GitHub-Pull-Request: golang/go#58389 Reviewed-on: https://go-review.googlesource.com/c/go/+/466175 Reviewed-by: Matthew Dempsky <mdempsky@google.com> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@google.com>
This commit is contained in:
parent
f3e0f1c077
commit
3161081c12
@ -867,13 +867,7 @@ func subst(n ir.Node, m map[*ir.Name]ir.Node) (ir.Node, bool) {
|
|||||||
x = ir.Copy(x)
|
x = ir.Copy(x)
|
||||||
ir.EditChildrenWithHidden(x, edit)
|
ir.EditChildrenWithHidden(x, edit)
|
||||||
if x, ok := x.(*ir.ConvExpr); ok && x.X.Op() == ir.OLITERAL {
|
if x, ok := x.(*ir.ConvExpr); ok && x.X.Op() == ir.OLITERAL {
|
||||||
// A conversion of variable or expression involving variables
|
if x, ok := truncate(x.X, x.Type()); ok {
|
||||||
// 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 {
|
|
||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
valid = false
|
valid = false
|
||||||
@ -888,7 +882,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,
|
// truncate returns the result of force converting c to type t,
|
||||||
// truncating its value as needed, like a conversion of a variable.
|
// truncating its value as needed, like a conversion of a variable.
|
||||||
// If the conversion is too difficult, truncate returns nil, false.
|
// 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()
|
ct := c.Type()
|
||||||
cv := c.Val()
|
cv := c.Val()
|
||||||
if ct.Kind() != t.Kind() {
|
if ct.Kind() != t.Kind() {
|
||||||
@ -910,7 +904,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)
|
c.SetType(t)
|
||||||
return c, true
|
return c, true
|
||||||
}
|
}
|
||||||
|
17
test/fixedbugs/issue58339.dir/a.go
Normal file
17
test/fixedbugs/issue58339.dir/a.go
Normal file
@ -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
test/fixedbugs/issue58339.dir/b.go
Normal file
9
test/fixedbugs/issue58339.dir/b.go
Normal file
@ -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
test/fixedbugs/issue58339.go
Normal file
7
test/fixedbugs/issue58339.go
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user