]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gc: logical operators should produce untyped bool for untyped
authorChris Manghane <cmang@golang.org>
Tue, 9 Dec 2014 15:16:38 +0000 (07:16 -0800)
committerChris Manghane <cmang@golang.org>
Mon, 29 Dec 2014 23:36:30 +0000 (23:36 +0000)
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>
src/cmd/gc/typecheck.c
test/fixedbugs/issue3924.go [deleted file]
test/fixedbugs/issue6671.go [new file with mode: 0644]

index 72f4d6f3ebd8d603cee4b063343d8dcd6da59cff..828ca34df5133a5859d0598eafe1efeafbdca1ae 100644 (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;
diff --git a/test/fixedbugs/issue3924.go b/test/fixedbugs/issue3924.go
deleted file mode 100644 (file)
index d4739b2..0000000
+++ /dev/null
@@ -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"
diff --git a/test/fixedbugs/issue6671.go b/test/fixedbugs/issue6671.go
new file mode 100644 (file)
index 0000000..b88faa4
--- /dev/null
@@ -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
+}