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/types: issue with using const with implicit unint64 #66705

Closed
alanyee opened this issue Apr 5, 2024 · 2 comments
Closed

go/types: issue with using const with implicit unint64 #66705

alanyee opened this issue Apr 5, 2024 · 2 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@alanyee
Copy link

alanyee commented Apr 5, 2024

Go version

go version go1.22.2 darwin/amd64

Output of go env in your module/workspace:

GO111MODULE='on'
GOARCH='amd64'
GOBIN='/Users/admin/go_repos/bin'
GOCACHE='/Users/admin/Library/Caches/go-build'
GOENV='/Users/admin/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/admin/go_repos/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/admin/go_repos'
GOPRIVATE=''
GOPROXY=''
GOROOT='/usr/local/Cellar/go/1.22.2/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/Cellar/go/1.22.2/libexec/pkg/tool/darwin_amd64'
GOVCS=''
GOVERSION='go1.22.2'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/Users/admin/go_something/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/wq/5g8tq0nx0tn8rwrmbwg5fxzw0000gp/T/go-build1921388423=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

  1. Create a const with a value that makes it so that it has to be uint64 but do not explicitly state that it is uint64: https://go.dev/play/p/hB59P8SZ0Nd
  2. Try to use the constant value directly

What did you see happen?

https://go.dev/play/p/hB59P8SZ0Nd does not compile:

./prog.go:13:36: cannot use MaxUint64 (untyped int constant 18446744073709551615) as int value in argument to fmt.Printf (overflows)

What did you expect to see?

I expected the code to compile and to see the test output of:

Type: uint64 Value 18446744073709551615
Type: uint64 Value 18446744073709551615

If you do not use the constant value directly, the code compiles, and the business logic works fine: https://go.dev/play/p/gTNfesGnMyj

@dmitshur
Copy link
Contributor

dmitshur commented Apr 5, 2024

CC @griesemer, @findleyr.

@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Apr 5, 2024
@dmitshur dmitshur added this to the Backlog milestone Apr 5, 2024
@findleyr
Copy link
Contributor

findleyr commented Apr 5, 2024

This is working as intended. The default an untyped int is int, and so when MaxUint64 (which, despite its name, is an untyped int) is used as an argument to Printf, it is converted to an int.

By converting to a uint64 first, the code compiles:
https://go.dev/play/p/Zo6ge6kcksL

The relevant section of the spec describing this behavior is here:
https://go.dev/ref/spec#Constants

So it looks like go/types and the compiler are correctly implementing the spec.

@findleyr findleyr closed this as not planned Won't fix, can't repro, duplicate, stale Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants