mirror of
https://github.com/golang/go
synced 2024-09-04 15:34:21 +00:00
cmd/compile: recognize bool(true) as a constant expression
Fixes #13821. Change-Id: I4a28a92d137edac3061537af25ac9d7aba411a66 Reviewed-on: https://go-review.googlesource.com/18262 Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
parent
e24b2445b4
commit
be20948e27
|
@ -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
|
||||
|
|
15
test/fixedbugs/issue13821.go
Normal file
15
test/fixedbugs/issue13821.go
Normal file
|
@ -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)
|
||||
)
|
22
test/fixedbugs/issue13821b.go
Normal file
22
test/fixedbugs/issue13821b.go
Normal file
|
@ -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"
|
Loading…
Reference in a new issue