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

go/constant: Int64Val(Make(big.NewInt(10))) returns (10, false), not (10, true) #42640

Closed
mdempsky opened this issue Nov 16, 2020 · 2 comments
Closed

Comments

@mdempsky
Copy link
Member

mdempsky commented Nov 16, 2020

Given v := constant.Make(big.NewInt(10)), constant.Int64Val(v) returns (10, false), but I'd expect it to return (10, true). See https://play.golang.org/p/SPzai9ii_7_f.

This is because go/constant expects that intVal will only be used to represent values that don't fit into an int64, because it always uses makeInt to convert a big.Int into a Value, which satisfies this expectation.

I see three possible solutions:

  1. Change Make to use makeInt (and maybe makeRat and makeFloat) to ensure the internal representations match how go/constant would have represented them normally. This will require TestMake to be updated though, because Val(Make(big.NewInt(10))) would now return an int64 instead of *big.Int.

  2. Change Int64Val to return Int64Val to return x.val.IsInt64() instead of false for intVal.

  3. Document that callers of Make should use an int64 instead of big.Int when possible, and maybe panic if provided a big.Int that fits int64.

/cc @griesemer @stamblerre

@mdempsky mdempsky changed the title go/constant: constant.Int64Val(constant.Make(big.NewInt(10))) returns (10, false), not (10, true) go/constant: Int64Val(Make(big.NewInt(10))) returns (10, false), not (10, true) Nov 16, 2020
@griesemer griesemer self-assigned this Nov 25, 2020
@griesemer
Copy link
Contributor

griesemer commented Nov 25, 2020

Solution 1 seems like the right approach. Per the documentation, this is perfectly valid and expected.

@gopherbot
Copy link

Change https://golang.org/cl/273086 mentions this issue: go/constant: constant.Make should produce "smallest" const representation

@golang golang locked and limited conversation to collaborators Nov 25, 2021
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