cmd/gc: logical operators should produce untyped bool for untyped

operands

Fixes #6671 for cmd/gc.

Change-Id: I4907655b6e243960f2ceb544c63ea16513c7bd68
Reviewed-on: https://go-review.googlesource.com/1251
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
Chris Manghane 2014-12-09 07:16:38 -08:00
parent edf7258416
commit 5cc29ab95e
3 changed files with 36 additions and 14 deletions

View file

@ -644,6 +644,13 @@ reswitch:
n->left = l;
n->right = r;
}
} else if(n->op == OANDAND || n->op == OOROR) {
if(l->type == r->type)
t = l->type;
else if(l->type == idealbool)
t = r->type;
else if(r->type == idealbool)
t = l->type;
// non-comparison operators on ideal bools should make them lose their ideal-ness
} else if(t == idealbool)
t = types[TBOOL];
@ -1438,7 +1445,7 @@ reswitch:
}
switch(n->op) {
case OCONVNOP:
if(n->left->op == OLITERAL) {
if(n->left->op == OLITERAL && n->type != types[TBOOL]) {
r = nod(OXXX, N, N);
n->op = OCONV;
n->orig = r;

View file

@ -1,13 +0,0 @@
// errorcheck
// Copyright 2012 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 foo
type mybool bool
var x, y = 1, 2
var _ mybool = x < y && x < y // ERROR "cannot use"
var _ mybool = x < y || x < y // ERROR "cannot use"

View file

@ -0,0 +1,28 @@
// errorcheck
// Copyright 2014 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.
// Issue 6671: Logical operators should produce untyped bool for untyped operands.
package p
type mybool bool
func _(x, y int) {
type mybool bool
var b mybool
_ = b
b = bool(true) // ERROR "cannot use"
b = true // permitted as expected
b = bool(true) && true // ERROR "cannot use"
b = true && true // permitted => && returns an untyped bool
b = x < y // permitted => x < y returns an untyped bool
b = true && x < y // permitted => result of && returns untyped bool
b = x < y && x < y // permitted => result of && returns untyped bool
b = x < y || x < y // permitted => result of || returns untyped bool
var c bool = true && x < y // permitted => result of && is bool
c = false || x < y // permitted => result of || returns untyped bool
_ = c
}