From d3f6d11d845fafe849d33505194b3ea1787e73a8 Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Wed, 14 Feb 2018 14:08:17 -0800 Subject: [PATCH] cmd/compile: fix typechecking of untyped boolean expressions Previously, if we typechecked a statement like var x bool = p1.f == p2.f && p1.g == p2.g we would correctly update the '&&' node's type from 'untyped bool' to 'bool', but the '==' nodes would stay 'untyped bool'. This is inconsistent, and caused consistency checks during walk to fail. This CL doesn't pass toolstash because it seems to slightly affect the register allocator's heuristics. (Presumably 'untyped bool's were previously making it all the way through SSA?) Fixes #23414. Change-Id: Ia85f8cfc69b5ba35dfeb157f4edf57612ecc3285 Reviewed-on: https://go-review.googlesource.com/94022 Run-TryBot: Matthew Dempsky TryBot-Result: Gobot Gobot Reviewed-by: Robert Griesemer --- src/cmd/compile/internal/gc/const.go | 14 ++++++++++---- test/fixedbugs/issue23414.go | 13 +++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 test/fixedbugs/issue23414.go diff --git a/src/cmd/compile/internal/gc/const.go b/src/cmd/compile/internal/gc/const.go index dcc16b6dec..af84005908 100644 --- a/src/cmd/compile/internal/gc/const.go +++ b/src/cmd/compile/internal/gc/const.go @@ -232,11 +232,17 @@ func convlit1(n *Node, t *types.Type, explicit bool, reuse canReuseNode) *Node { switch n.Op { default: if n.Type == types.Idealbool { - if t.IsBoolean() { - n.Type = t - } else { - n.Type = types.Types[TBOOL] + if !t.IsBoolean() { + t = types.Types[TBOOL] } + switch n.Op { + case ONOT: + n.Left = convlit(n.Left, t) + case OANDAND, OOROR: + n.Left = convlit(n.Left, t) + n.Right = convlit(n.Right, t) + } + n.Type = t } if n.Type.Etype == TIDEAL { diff --git a/test/fixedbugs/issue23414.go b/test/fixedbugs/issue23414.go new file mode 100644 index 0000000000..7ef3d831fd --- /dev/null +++ b/test/fixedbugs/issue23414.go @@ -0,0 +1,13 @@ +// compile + +// Copyright 2018 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 x struct{} + +func f() bool { + return x == x && x == x +}