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

encoding/binary, cmd/compile: improve bounds checking hints #14900

Closed
josharian opened this issue Mar 21, 2016 · 3 comments
Closed

encoding/binary, cmd/compile: improve bounds checking hints #14900

josharian opened this issue Mar 21, 2016 · 3 comments
Milestone

Comments

@josharian
Copy link
Contributor

CL 20981 added bounds checking hints to encoding/binary, of the form

b = b[:8:len(b)]

It would be clearer and simpler and generate less code to use:

_ = b[7]

However, the compiler isn't yet able to use such a hint.

This issue is to teach the compiler to use such a hint and to update whatever sites it has already been used. At a minimum, this is encoding/binary, but #14884 suggests using it in the image package, and I wouldn't be surprised to find it popping up other places soon as well.

For more background, see the discussion at https://groups.google.com/forum/#!topic/golang-dev/jVP6h21OyL8

Marking tentatively as Go 1.7, because once this gets released, lots of folks will start adding bounds checking hints of one kind or another, and it'd be nice if they were of the shorter form.

@rasky
Copy link
Member

rasky commented Mar 26, 2016

I think an even clearer hint would be:

if len(b) < 8 {
    panic("slice too short")
}

Obviously, it would be great if both worked, so that each developer can choose the best for their code.

@gopherbot
Copy link

CL https://golang.org/cl/21008 mentions this issue.

@randall77
Copy link
Contributor

21008 will handle both the _ = b[7] and if len(b) < 8 { panic/return } idioms.

@golang golang locked and limited conversation to collaborators Mar 31, 2017
@rsc rsc unassigned brtzsnr Jun 23, 2022
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

5 participants