diff --git a/src/cmd/compile/internal/gc/const.go b/src/cmd/compile/internal/gc/const.go index 0a00825b856..d30515a87b6 100644 --- a/src/cmd/compile/internal/gc/const.go +++ b/src/cmd/compile/internal/gc/const.go @@ -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() diff --git a/src/cmd/compile/internal/gc/typecheck.go b/src/cmd/compile/internal/gc/typecheck.go index 70560d405df..224480279c8 100644 --- a/src/cmd/compile/internal/gc/typecheck.go +++ b/src/cmd/compile/internal/gc/typecheck.go @@ -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 index 00000000000..7d5024893f9 --- /dev/null +++ b/test/fixedbugs/issue13821.go @@ -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 index 00000000000..3b0e2d2287e --- /dev/null +++ b/test/fixedbugs/issue13821b.go @@ -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"