Commit 5cc29ab9 authored by Chris Manghane's avatar Chris Manghane

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/1251Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
Reviewed-by: default avatarRuss Cox <rsc@golang.org>
parent edf72584
......@@ -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;
......
// 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"
// 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
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment