Commit 27691fa4 authored by Matthew Dempsky's avatar Matthew Dempsky

cmd/compile: recognize !typedbool is typed

Adding the evconst(n) call for OANDAND and OOROR in
golang.org/cl/18262 was originally just to parallel the above iscmp
branch, but upon further inspection it seemed odd that removing it
caused test/fixedbugs/issue6671.go's

    var b mybool
    // ...
    b = bool(true) && true // ERROR "cannot use"

to start failing (i.e., by not emitting the expected "cannot use"
error).

The problem is that evconst(n)'s settrue and setfalse paths always
reset n.Type to idealbool, even for logical operators where n.Type
should preserve the operand type.  Adding the evconst(n) call for
OANDAND/OOROR inadvertantly worked around this by turning the later
evconst(n) call at line 2167 into a noop, so the "n.Type = t"
assignment at line 739 would preserve the operand type.

However, that means evconst(n) was still clobbering n.Type for ONOT,
so declarations like:

    const _ bool = !mybool(true)

were erroneously accepted.

Update #13821.

Change-Id: I18e37287f05398fdaeecc0f0d23984e244f025da
Reviewed-on: https://go-review.googlesource.com/18362
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
parent 3f22adec
......@@ -634,6 +634,7 @@ func evconst(n *Node) {
var wr int
var v Val
var norig *Node
var nn *Node
if nr == nil {
// copy numeric value to avoid modifying
// nl, in case someone still refers to it (e.g. iota).
......@@ -1115,15 +1116,21 @@ ret:
return
settrue:
norig = saveorig(n)
*n = *Nodbool(true)
n.Orig = norig
nn = Nodbool(true)
nn.Orig = saveorig(n)
if !iscmp[n.Op] {
nn.Type = nl.Type
}
*n = *nn
return
setfalse:
norig = saveorig(n)
*n = *Nodbool(false)
n.Orig = norig
nn = Nodbool(false)
nn.Orig = saveorig(n)
if !iscmp[n.Op] {
nn.Type = nl.Type
}
*n = *nn
return
illegal:
......
......@@ -687,8 +687,6 @@ OpSwitch:
n.Left = l
n.Right = r
}
} else if n.Op == OANDAND || n.Op == OOROR {
evconst(n)
}
if et == TSTRING {
......
......@@ -20,3 +20,5 @@ 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"
var x8 = b && !B2(true) // ERROR "mismatched types B and B2"
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