]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: recognize bool(true) as a constant expression
authorMatthew Dempsky <mdempsky@google.com>
Tue, 5 Jan 2016 20:26:55 +0000 (12:26 -0800)
committerRuss Cox <rsc@golang.org>
Wed, 6 Jan 2016 14:38:33 +0000 (14:38 +0000)
Fixes #13821.

Change-Id: I4a28a92d137edac3061537af25ac9d7aba411a66
Reviewed-on: https://go-review.googlesource.com/18262
Reviewed-by: Russ Cox <rsc@golang.org>
src/cmd/compile/internal/gc/const.go
src/cmd/compile/internal/gc/typecheck.go
test/fixedbugs/issue13821.go [new file with mode: 0644]
test/fixedbugs/issue13821b.go [new file with mode: 0644]

index 0a00825b85617ba8fc8fa35070c79ce7dce1b2b2..d30515a87b69b982d21a62dcfb4925f518c7268d 100644 (file)
@@ -664,7 +664,8 @@ func evconst(n *Node) {
                case OCONV_ | CTINT_,
                        OCONV_ | CTRUNE_,
                        OCONV_ | CTFLT_,
-                       OCONV_ | CTSTR_:
+                       OCONV_ | CTSTR_,
+                       OCONV_ | CTBOOL_:
                        convlit1(&nl, n.Type, true)
 
                        v = nl.Val()
index 70560d405df4e78bc3e47cf95dc2404fe8ec3ed3..224480279c8606a4273a9fa9468076db99e91706 100644 (file)
@@ -688,17 +688,7 @@ OpSwitch:
                                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
-                       }
-               } else
-               // non-comparison operators on ideal bools should make them lose their ideal-ness
-               if t == idealbool {
-                       t = Types[TBOOL]
+                       evconst(n)
                }
 
                if et == TSTRING {
@@ -1751,7 +1741,7 @@ OpSwitch:
 
                switch n.Op {
                case OCONVNOP:
-                       if n.Left.Op == OLITERAL && n.Type != Types[TBOOL] {
+                       if n.Left.Op == OLITERAL {
                                r := Nod(OXXX, nil, nil)
                                n.Op = OCONV
                                n.Orig = r
diff --git a/test/fixedbugs/issue13821.go b/test/fixedbugs/issue13821.go
new file mode 100644 (file)
index 0000000..7d50248
--- /dev/null
@@ -0,0 +1,15 @@
+// compile
+
+// Copyright 2015 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 13821.  Compiler rejected "bool(true)" as not a constant.
+
+package p
+
+const (
+       A = true
+       B = bool(A)
+       C = bool(true)
+)
diff --git a/test/fixedbugs/issue13821b.go b/test/fixedbugs/issue13821b.go
new file mode 100644 (file)
index 0000000..3b0e2d2
--- /dev/null
@@ -0,0 +1,22 @@
+// errorcheck
+
+// Copyright 2015 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 13821.  Additional regress tests.
+
+package p
+
+type B bool
+type B2 bool
+
+var b B
+var b2 B2
+var x1 = b && 1 < 2 // x1 has type B, not ideal bool
+var x2 = 1 < 2 && b // x2 has type B, not ideal bool
+var x3 = b && b2    // ERROR "mismatched types B and B2"
+var x4 = x1 && b2   // ERROR "mismatched types B and B2"
+var x5 = x2 && b2   // ERROR "mismatched types B and B2"
+var x6 = b2 && x1   // ERROR "mismatched types B2 and B"
+var x7 = b2 && x2   // ERROR "mismatched types B2 and B"