From 1755d1455977cb9dc6cdfa032dfd1c370e2a4097 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Sat, 5 Aug 2023 22:51:49 +0700 Subject: [PATCH] [release-branch.go1.21] cmd/compile: fix missing init nodes for len(string([]byte)) optimization CL 497276 added optimization for len(string([]byte)) by avoiding call to slicebytetostring. However, the bytes to string expression may contain init nodes, which need to be preserved. Otherwise, it would make the liveness analysis confusing about the lifetime of temporary variables created by init nodes. Fixes #61781 Change-Id: I6d1280a7d61bcc75f11132af41bda086f084ab54 Reviewed-on: https://go-review.googlesource.com/c/go/+/516375 Run-TryBot: Cuong Manh Le Reviewed-by: Keith Randall Reviewed-by: Keith Randall Auto-Submit: Cuong Manh Le TryBot-Result: Gopher Robot Reviewed-by: Matthew Dempsky Reviewed-on: https://go-review.googlesource.com/c/go/+/516535 --- src/cmd/compile/internal/walk/builtin.go | 5 ++++- test/fixedbugs/issue61778.go | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 test/fixedbugs/issue61778.go diff --git a/src/cmd/compile/internal/walk/builtin.go b/src/cmd/compile/internal/walk/builtin.go index 786c31313c..0bb5018250 100644 --- a/src/cmd/compile/internal/walk/builtin.go +++ b/src/cmd/compile/internal/walk/builtin.go @@ -255,7 +255,10 @@ func walkLenCap(n *ir.UnaryExpr, init *ir.Nodes) ir.Node { return mkcall("countrunes", n.Type(), init, typecheck.Conv(n.X.(*ir.ConvExpr).X, types.Types[types.TSTRING])) } if isByteCount(n) { - _, len := backingArrayPtrLen(cheapExpr(n.X.(*ir.ConvExpr).X, init)) + conv := n.X.(*ir.ConvExpr) + walkStmtList(conv.Init()) + init.Append(ir.TakeInit(conv)...) + _, len := backingArrayPtrLen(cheapExpr(conv.X, init)) return len } diff --git a/test/fixedbugs/issue61778.go b/test/fixedbugs/issue61778.go new file mode 100644 index 0000000000..5055c9e6a2 --- /dev/null +++ b/test/fixedbugs/issue61778.go @@ -0,0 +1,13 @@ +// compile + +// 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 p + +func f(s []byte) { + switch "" { + case string(append(s, 'a')): + } +} -- 2.50.0