]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: fix constructing expr side-effects when comparing 0-size types
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Wed, 4 May 2022 18:00:38 +0000 (01:00 +0700)
committerGopher Robot <gobot@golang.org>
Thu, 5 May 2022 04:53:27 +0000 (04:53 +0000)
In walkCompare, any ir.OCONVNOP was removed from both operands. So when
constructing assignments for them to preserve any side-effects, using
temporary variables can cause type mismatched with original type.

Instead, using blank assignments will prevent that issue and still make
sure that the operands will be evaluated.

Fixes #52701

Change-Id: I229046acb154890bb36fe441d258563687fdce37
Reviewed-on: https://go-review.googlesource.com/c/go/+/403997
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Robert Griesemer <gri@google.com>
src/cmd/compile/internal/walk/compare.go
test/fixedbugs/issue52701.go [new file with mode: 0644]

index fef2d710c0594d1baf75a80af22d4c2b2667b640..d271698c5193827df538419da10cdceb3a41b9f3 100644 (file)
@@ -268,9 +268,8 @@ func walkCompare(n *ir.BinaryExpr, init *ir.Nodes) ir.Node {
                expr = ir.NewBool(n.Op() == ir.OEQ)
                // We still need to use cmpl and cmpr, in case they contain
                // an expression which might panic. See issue 23837.
-               t := typecheck.Temp(cmpl.Type())
-               a1 := typecheck.Stmt(ir.NewAssignStmt(base.Pos, t, cmpl))
-               a2 := typecheck.Stmt(ir.NewAssignStmt(base.Pos, t, cmpr))
+               a1 := typecheck.Stmt(ir.NewAssignStmt(base.Pos, ir.BlankNode, cmpl))
+               a2 := typecheck.Stmt(ir.NewAssignStmt(base.Pos, ir.BlankNode, cmpr))
                init.Append(a1, a2)
        }
        return finishCompare(n, expr, init)
diff --git a/test/fixedbugs/issue52701.go b/test/fixedbugs/issue52701.go
new file mode 100644 (file)
index 0000000..f1de16c
--- /dev/null
@@ -0,0 +1,16 @@
+// compile
+
+// Copyright 2022 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 T1 struct{}
+type T2 struct{}
+
+func f() {
+       switch (T1{}) {
+       case T1(T2{}):
+       }
+}