-
Notifications
You must be signed in to change notification settings - Fork 18k
go/constant: cannot deal with exponential notation #43165
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
Comments
I found another interesting case, for this piece of code: func main() {
x := uint64(5)
println(x / (12e9 / 1))
} It results in the following output:
In this case, (Reproduce by replacing the |
I could be mistaken, but I believe exponent notation is only for floating-point literals: https://golang.org/ref/spec#Floating-point_literals /cc @griesemer |
As @toothrot has already pointed out, The constant package doesn't care about types. It cares only about value representation. You can explicitly convert the value: This is working as expected. |
Thank you for the explanation! I have tried |
Before this change, the compiler could panic with the following message: panic: 20 not an Int That of course doesn't make much sense. But it apparently is expected behavior, see golang/go#43165 for details. This commit fixes this issue by converting the constant to an integer if needed.
Before this change, the compiler could panic with the following message: panic: 20 not an Int That of course doesn't make much sense. But it apparently is expected behavior, see golang/go#43165 for details. This commit fixes this issue by converting the constant to an integer if needed.
Before this change, the compiler could panic with the following message: panic: 20 not an Int That of course doesn't make much sense. But it apparently is expected behavior, see golang/go#43165 for details. This commit fixes this issue by converting the constant to an integer if needed.
Before this change, the compiler could panic with the following message: panic: 20 not an Int That of course doesn't make much sense. But it apparently is expected behavior, see golang/go#43165 for details. This commit fixes this issue by converting the constant to an integer if needed.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes, at least with Go 1.15.5.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
There is no easy way of reproducing this, so I made a new repository with a reproducer in code form: https://github.com/aykevl/constrepro
After cloning it, try running it:
What did you expect to see?
I expected the following output:
You can get this if you use the other
twenty
constant in ./const/const.go (see comment).What did you see instead?
Clearly,
20
is an int. But somewhere it seems to have been converted to a float. You can see this if you inspect the generated AST (see line 40):So somehow the value is interpreted as an integer by the SSA package, but in the AST it is somehow considered a float. I think the AST here is wrong (or maybe
constant.Uint64Val
), as the code typechecks just fine and is accepted by the Go toolchain.(This is the bug I was referring to at the bottom of #43163).
The text was updated successfully, but these errors were encountered: