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

proposal: Go 2: change len, cap to return untyped ints if result is constant #31795

Open
griesemer opened this issue May 2, 2019 · 5 comments

Comments

Projects
None yet
5 participants
@griesemer
Copy link
Contributor

commented May 2, 2019

Reminder issue.

Can we change len, cap, etc. to return an untyped value rather than an int if they are constant expressions? Investigate if this is possible in a backward-compatible way.

@bradfitz

This comment has been minimized.

Copy link
Member

commented May 7, 2019

And unsafe.Sizeof etc.

@ianlancetaylor ianlancetaylor changed the title spec: built-ins such as len to return untyped result for constant expressions (investigation) proposal: Go 2: change len, cap, unsafe.Sizeof, unsafe.OffsetOf to return untyped constants May 14, 2019

@gopherbot gopherbot added the Proposal label May 14, 2019

@ianlancetaylor ianlancetaylor changed the title proposal: Go 2: change len, cap, unsafe.Sizeof, unsafe.OffsetOf to return untyped constants proposal: Go 2: change len, cap, unsafe.Sizeof, unsafe.OffsetOf to return untyped constants if constant May 14, 2019

@MichaelTJones

This comment has been minimized.

Copy link
Contributor

commented May 28, 2019

Yes, this meets every use case that has frustrated me in code.

@griesemer griesemer changed the title proposal: Go 2: change len, cap, unsafe.Sizeof, unsafe.OffsetOf to return untyped constants if constant proposal: Go 2: change len, cap, unsafe.AlignOf, unsafe.Sizeof, unsafe.OffsetOf to return untyped constants if constant May 29, 2019

@griesemer griesemer changed the title proposal: Go 2: change len, cap, unsafe.AlignOf, unsafe.Sizeof, unsafe.OffsetOf to return untyped constants if constant proposal: Go 2: change len, cap, unsafe.Alignof, unsafe.Sizeof, unsafe.OffsetOf to return untyped constants if constant May 29, 2019

@gopherbot

This comment has been minimized.

Copy link

commented May 29, 2019

Change https://golang.org/cl/179184 mentions this issue: go/types: change cap, len to return untyped int if result is constant (experiment)

@griesemer

This comment has been minimized.

Copy link
Contributor Author

commented May 29, 2019

It turns out that per the spec, unsafe.Sizeof (and its siblings unsafe.Offsetof, unsafe.Alignof) return a uintptr rather than an int, which makes it impossible to change these in a backward-compatible way: even though they always return a constant, if that were an untyped int it would not default to uintptr. A place where this is causing problems is for instance in file https://golang.org/src/cmd/vendor/golang.org/x/sys/unix/syscall_darwin.go on line 353 where n is supposed to be of type uintptr.

But we should be able to do it for len and cap without problems - and indeed there aren't any in the stdlib as a type-checking it via a modified type checker shows (apply CL 179184 and run go test go/types which will run the go/types over all packages of the std lib, among other tests).

@griesemer griesemer changed the title proposal: Go 2: change len, cap, unsafe.Alignof, unsafe.Sizeof, unsafe.OffsetOf to return untyped constants if constant proposal: Go 2: change len, cap to return untyped constants if constant May 29, 2019

@griesemer griesemer changed the title proposal: Go 2: change len, cap to return untyped constants if constant proposal: Go 2: change len, cap to return untyped ints if result is constant May 29, 2019

@griesemer

This comment has been minimized.

Copy link
Contributor Author

commented May 29, 2019

As an aside, changing len and cap as described is in the spirit of the built-ins complex, real and imag which also return an untyped result if the argument(s) are untyped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.