-
Notifications
You must be signed in to change notification settings - Fork 570
compiler/prelude: Keep nil slice nil when slicing. #852
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
Conversation
According to the Go specification: > If the sliced operand of a valid slice expression is a nil slice, > the result is a nil slice. This behavior was not implemented and not caught previously. This change fixes that. The minified prelude was regenerated with: go generate ./compiler/prelude Fixes #851.
tests/misc_test.go
Outdated
var s []int | ||
s = s[:] | ||
s = s[0:0] | ||
s = s[0:0:0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd add the same if s != nil
above this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you suggesting this in case one of the assigments makes s
non-nil, but the following line makes it nil again, and we don't catch it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As s
is expected to be nil, I thought you don't have to reassign nil, but probably reassiging would be better. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I reassigned just to be sure that the error messages would be accurate if printed.
tests/misc_test.go
Outdated
s = s[0:0] | ||
s = s[0:0:0] | ||
if s != nil { | ||
t.Errorf("nil slice became non-nil after slicing: %#v", s) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know the expectation, but would it be better to reword this including expectation?
tests/misc_test.go
Outdated
var s = []int{} | ||
s = s[:] | ||
if s == nil { | ||
t.Errorf("non-nil slice became nil after slicing: %#v", s) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
Use the more common "got <x>, want <y>" style.
Done, PTAL. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
According to the Go specification:
This behavior was not implemented and not caught previously. Slicing a nil slice would incorrectly make it an empty but non-nil slice. This change fixes that.
The minified prelude was regenerated with:
Fixes #851.