1
0
mirror of https://github.com/golang/go synced 2024-11-11 19:21:37 -07:00

[release-branch.go1.17] 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().

Cherry-picked from https://go-review.googlesource.com/c/go/+/348569

Fixes #49511

Change-Id: Id5b736daa8e90afda88aaa3769dde801db294c0d
Reviewed-on: https://go-review.googlesource.com/c/go/+/363664
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
This commit is contained in:
Dan Scales 2021-09-08 08:41:54 -07:00 committed by Heschi Kreinick
parent 4536558f16
commit 4eaf0b7a58
3 changed files with 38 additions and 8 deletions

View File

@ -495,10 +495,11 @@ func transformSelect(sel *ir.SelectStmt) {
if ncase.Comm != nil {
n := ncase.Comm
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() {
case ir.OAS:

View File

@ -383,10 +383,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:

View File

@ -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))
}
}
}