From be152920b90dd0fd73e2b2d6ffea294e09fc36be Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Tue, 2 Jul 2024 17:17:39 +0700 Subject: [PATCH] cmd/compile: fix ICE when compiling global a, b = f() CL 327651 rewrites a, b = f() to use temporaries when types are not identical. That would leave OAS2 node appears in body of init function for global variables initialization. The staticinit pass is not updated to handle OAS2 node, causing ICE when compiling global variables. To fix this, handle OAS2 nodes like other OAS2*, since they mostly necessitate dynamic execution anyway. Fixes #68264 Change-Id: I1eff8cc3e47035738a2c70d3169e35ec36ee9242 Reviewed-on: https://go-review.googlesource.com/c/go/+/596055 Reviewed-by: Than McIntosh Reviewed-by: Cherry Mui LUCI-TryBot-Result: Go LUCI Auto-Submit: Cuong Manh Le --- src/cmd/compile/internal/staticinit/sched.go | 14 ++++++++++++++ test/fixedbugs/issue68264.go | 15 +++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 test/fixedbugs/issue68264.go diff --git a/src/cmd/compile/internal/staticinit/sched.go b/src/cmd/compile/internal/staticinit/sched.go index 7317ed1fec..91c0a27faf 100644 --- a/src/cmd/compile/internal/staticinit/sched.go +++ b/src/cmd/compile/internal/staticinit/sched.go @@ -107,6 +107,20 @@ func (s *Schedule) tryStaticInit(n ir.Node) bool { case ir.OAS: n := n.(*ir.AssignStmt) lhs, rhs = []ir.Node{n.X}, n.Y + case ir.OAS2: + // Usually OAS2 has been rewritten to separate OASes by types2. + // What's left here is "var a, b = tmp1, tmp2" as a result from rewriting + // "var a, b = f()" that needs type conversion, which is not static. + n := n.(*ir.AssignListStmt) + for _, rhs := range n.Rhs { + for rhs.Op() == ir.OCONVNOP { + rhs = rhs.(*ir.ConvExpr).X + } + if name, ok := rhs.(*ir.Name); !ok || !name.AutoTemp() { + base.FatalfAt(n.Pos(), "unexpected rhs, not an autotmp: %+v", rhs) + } + } + return false case ir.OAS2DOTTYPE, ir.OAS2FUNC, ir.OAS2MAPR, ir.OAS2RECV: n := n.(*ir.AssignListStmt) if len(n.Lhs) < 2 || len(n.Rhs) != 1 { diff --git a/test/fixedbugs/issue68264.go b/test/fixedbugs/issue68264.go new file mode 100644 index 0000000000..7d67e55f6e --- /dev/null +++ b/test/fixedbugs/issue68264.go @@ -0,0 +1,15 @@ +// compile + +// Copyright 2024 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 p + +type nat []int + +var a, b nat = y() + +func y() (nat, []int) { + return nat{0}, nat{1} +} -- 2.48.1