From b9e1a24581b6282ee930a50bbe498d24ef77f486 Mon Sep 17 00:00:00 2001 From: Dan Scales Date: Wed, 8 Sep 2021 08:41:54 -0700 Subject: [PATCH] cmd/compile: fix case where init info of OAS node is dropped When an OAS node is converted to an OSELRECV2 node in tcSelect(), the possible DCL node in the Init field was being dropped, since a completely new node was being created and the Init field was not set. I don't expect n.Init() to be set for the ORECV case, but the code now deals with that too. Fixed bug in both tcSelect() and transformSelect(). Fixes #48289 Change-Id: I09918a70f7cbaa4aa9a17546169f908a8787df15 Reviewed-on: https://go-review.googlesource.com/c/go/+/348569 Trust: Dan Scales Run-TryBot: Dan Scales TryBot-Result: Go Bot Reviewed-by: Cuong Manh Le Reviewed-by: Matthew Dempsky Reviewed-by: Robert Griesemer --- src/cmd/compile/internal/noder/transform.go | 1 + src/cmd/compile/internal/typecheck/stmt.go | 9 ++++--- test/fixedbugs/issue48289.go | 28 +++++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 test/fixedbugs/issue48289.go diff --git a/src/cmd/compile/internal/noder/transform.go b/src/cmd/compile/internal/noder/transform.go index b278f3db09c..8173bfc747f 100644 --- a/src/cmd/compile/internal/noder/transform.go +++ b/src/cmd/compile/internal/noder/transform.go @@ -554,6 +554,7 @@ func transformSelect(sel *ir.SelectStmt) { } selrecv.Def = def selrecv.SetTypecheck(1) + selrecv.SetInit(n.Init()) ncase.Comm = selrecv } switch n.Op() { diff --git a/src/cmd/compile/internal/typecheck/stmt.go b/src/cmd/compile/internal/typecheck/stmt.go index c322d490e5b..9a02c1752ca 100644 --- a/src/cmd/compile/internal/typecheck/stmt.go +++ b/src/cmd/compile/internal/typecheck/stmt.go @@ -395,10 +395,11 @@ func tcSelect(sel *ir.SelectStmt) { n := Stmt(ncase.Comm) ncase.Comm = n oselrecv2 := func(dst, recv ir.Node, def bool) { - n := ir.NewAssignListStmt(n.Pos(), ir.OSELRECV2, []ir.Node{dst, ir.BlankNode}, []ir.Node{recv}) - n.Def = def - n.SetTypecheck(1) - ncase.Comm = n + selrecv := ir.NewAssignListStmt(n.Pos(), ir.OSELRECV2, []ir.Node{dst, ir.BlankNode}, []ir.Node{recv}) + selrecv.Def = def + selrecv.SetTypecheck(1) + selrecv.SetInit(n.Init()) + ncase.Comm = selrecv } switch n.Op() { default: diff --git a/test/fixedbugs/issue48289.go b/test/fixedbugs/issue48289.go new file mode 100644 index 00000000000..94dbeee34cf --- /dev/null +++ b/test/fixedbugs/issue48289.go @@ -0,0 +1,28 @@ +// run + +// 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 main + +import "fmt" + +func main() { + ch := make(chan int, 1) + + var ptrs [2]*int + for i := range ptrs { + ch <- i + select { + case x := <-ch: + ptrs[i] = &x + } + } + + for i, ptr := range ptrs { + if *ptr != i { + panic(fmt.Sprintf("got *ptr %d, want %d", *ptr, i)) + } + } +}