From 8c666697646bc820beb3725b3ff4a4cd5514bbe7 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Fri, 23 Apr 2021 12:53:51 +0700 Subject: [PATCH] cmd/compile: make sure ascompatee walk lhs init statements CL 281152 improved ascompatee by removing the call to safeExpr on lhs. But we forgot that lhs int statements, if any, must be walked prior saving subexpressions, which cause the bug in #45706. Fixes #45706 Change-Id: I0064315056ef4ca92ebf3c332c2e3a9bb2b26f68 Reviewed-on: https://go-review.googlesource.com/c/go/+/312632 Trust: Cuong Manh Le Run-TryBot: Cuong Manh Le TryBot-Result: Go Bot Reviewed-by: Matthew Dempsky --- src/cmd/compile/internal/walk/assign.go | 3 +++ test/fixedbugs/issue45706.go | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 test/fixedbugs/issue45706.go diff --git a/src/cmd/compile/internal/walk/assign.go b/src/cmd/compile/internal/walk/assign.go index c8342b4fa4..3abf2a060c 100644 --- a/src/cmd/compile/internal/walk/assign.go +++ b/src/cmd/compile/internal/walk/assign.go @@ -341,6 +341,9 @@ func ascompatee(op ir.Op, nl, nr []ir.Node) []ir.Node { break } + walkStmtList(l.Init()) + early.Append(ir.TakeInit(l)...) + var name *ir.Name switch l.Op() { default: diff --git a/test/fixedbugs/issue45706.go b/test/fixedbugs/issue45706.go new file mode 100644 index 0000000000..facf488b3d --- /dev/null +++ b/test/fixedbugs/issue45706.go @@ -0,0 +1,16 @@ +// compile + +// Copyright 2021 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 + +var i int +var arr []*int +var f func() int + +func g() { + for i, *(arr[f()]) = range []int{} { + } +}