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
go/constant: passing nil to BinaryOp or Compare panics with unreachable
#17812
Comments
nil is not a valid go/constant Value. In general, we don't describe in every package that nil is not valid - we usually describe when nil is valid. We also don't test every single impossible value (see https://play.golang.org/p/O62KzUF86T for instance). The documentation says "The comparison must be defined for the operands." - admittedly vague but it's clear to expect the unexpected otherwise. |
@griesemer Should I file a bug for go/ssa instead, or is it also expected that an |
In retrospect I agree that the error message is wrong: That code clearly is reachable after all, even if it means incorrect input. I will fix that. And yes, if go/ssa produces a nil constant Value it probably should say that either nil is possible and means "no constant value", or there's a bug (documentation or otherwise). |
I.e. is https://play.golang.org/p/ONBGaMJD1d valid and expected to disagree with |
The ssa and The constant package does not allow nil operands to its functions. (When a Go function accepts optional arguments or nil, this is usually explicitly documented and in the absence of documentation it should be assumed unsafe.) The ssa package uses a nil
As a result, you must check the ssa.Const.Value field is non-nil before passing it to a function such as |
CL https://golang.org/cl/32870 mentions this issue. |
CL https://golang.org/cl/32872 mentions this issue. |
For #17812. Change-Id: I58411aaa0e8b2250a16ddb20c951e39da3d601e8 Reviewed-on: https://go-review.googlesource.com/32872 Reviewed-by: Alan Donovan <adonovan@google.com>
What version of Go are you using (
go version
)?go version go1.7.3 linux/amd64
What did you do?
https://play.golang.org/p/G0V0HApS-t
What did you expect to see?
true
What did you see instead?
I don't know if an untyped nil is a valid constant.Value – I obtained it from a
*ssa.Const.Value
from the following typechecked package, the BinOp specifically:which compiles to
If nil is valid, no panic should occur. If nil is not valid, it should have a more descriptive panic, and go/ssa might have a bug of its own.
/cc @alandonovan @griesemer
The text was updated successfully, but these errors were encountered: