Commit be20948e authored by Matthew Dempsky's avatar Matthew Dempsky Committed by Russ Cox

cmd/compile: recognize bool(true) as a constant expression

Fixes #13821.

Change-Id: I4a28a92d137edac3061537af25ac9d7aba411a66
Reviewed-on: https://go-review.googlesource.com/18262Reviewed-by: default avatarRuss Cox <rsc@golang.org>
parent e24b2445
......@@ -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()
......
......@@ -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
......
// 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)
)
// 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"
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