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: error if trying to print constant > int #20108

Closed
karalabe opened this issue Apr 25, 2017 · 4 comments
Closed

cmd/compile: error if trying to print constant > int #20108

karalabe opened this issue Apr 25, 2017 · 4 comments

Comments

@karalabe
Copy link
Contributor

This is mostly a funky error that got me surprised when I saw it. It's of course trivial to work around but I though it could be worth a question whether it's working as intended or not.

What version of Go are you using (go version)?

go version go1.8.1 linux/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/work:/opt/google/cloud-sdk/platform/google_appengine/goroot/src"
GORACE=""
GOROOT="/opt/google/go"
GOTOOLDIR="/opt/google/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build623255510=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

https://play.golang.org/p/5GARX4Q2Tc

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.MaxUint64)
}

What did you expect to see?

18446744073709551615

What did you see instead?

tmp/sandbox131021564/main.go:9: constant 18446744073709551615 overflows int

@davecheney
Copy link
Contributor

I think this is working as intended. It's a side effect of coercing an untyped integer constant math.MaxUint64 to an int while being boxed into the ...interface{} for fmt.Println.

@karalabe
Copy link
Contributor Author

I guess the compiler needs to make some reproducible decision as to what to box the constant to and can't just pick whatever is the best based on value. That being said, boxing to int would still have an interesting corner case:

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.MaxUint32)
}
$ GOARCH=amd64 go run main.go 
4294967295

$ GOARCH=386 go run main.go 
main.go:9: constant 4294967295 overflows int

The reason is again obvious, but I'd expect a bit more "portability" than to choke so easily between 32/64 bits.

@karalabe
Copy link
Contributor Author

Anyway, if you think it works as intended feel free to close :) At least you have a new slide for your quiz collection @davecheney ;)

@davecheney
Copy link
Contributor

The spec is the spec is the spec, https://golang.org/ref/spec#Constants

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