Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: conversion of a constant to bool not considered a constant expression #13821

Closed
momchil-velikov opened this issue Jan 5, 2016 · 2 comments
Milestone

Comments

@momchil-velikov
Copy link
Contributor

For the following program

package main
const A = true
const B = bool(A)
func main() {}

the compiler issues error message const initializer bool(A) is not a constant.

Likewise for

package main
const A = bool(true)
func main() {}
@ianlancetaylor ianlancetaylor changed the title cmd/compile: The compiler does consider conversion of a constant to bool as a constant expression cmd/compile: conversion of a constant to bool not considered a constant expression Jan 5, 2016
@ianlancetaylor ianlancetaylor added this to the Go1.7 milestone Jan 5, 2016
@gopherbot
Copy link

CL https://golang.org/cl/18262 mentions this issue.

@gopherbot
Copy link

CL https://golang.org/cl/18362 mentions this issue.

mdempsky added a commit that referenced this issue Jan 7, 2016
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: Russ Cox <rsc@golang.org>
@golang golang locked and limited conversation to collaborators Jan 7, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants