Skip to content

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

Merged
merged 2 commits into from
Aug 20, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
tests: Improve test error message consistency.
Use the more common "got <x>, want <y>" style.
  • Loading branch information
dmitshur committed Aug 20, 2018
commit 4ec9ef5604f49d66f23be2c94ae5af9687edd32c
14 changes: 11 additions & 3 deletions tests/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,16 +641,24 @@ func TestSlicingNilSlice(t *testing.T) {
t.Run("StaysNil", func(t *testing.T) {
var s []int
s = s[:]
if s != nil {
t.Errorf("nil slice became non-nil after slicing with s[:]: %#v, want []int(nil)", s)
}
s = nil
s = s[0:0]
if s != nil {
t.Errorf("nil slice became non-nil after slicing with s[0:0]: %#v, want []int(nil)", s)
}
s = nil
s = s[0:0:0]
Copy link
Member

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

Copy link
Member Author

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?

Copy link
Member

@hajimehoshi hajimehoshi Aug 20, 2018

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!

Copy link
Member Author

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.

if s != nil {
t.Errorf("nil slice became non-nil after slicing: %#v", s)
t.Errorf("nil slice became non-nil after slicing with s[0:0:0]: %#v, want []int(nil)", s)
}
})
t.Run("Panics", func(t *testing.T) {
defer func() {
if err := recover(); err == nil || !strings.Contains(err.(error).Error(), "slice bounds out of range") {
t.Error("slicing nil slice out of range didn't panic as expected")
t.Error("slicing nil slice out of range didn't panic, want panic")
}
}()
var s []int
Expand All @@ -660,7 +668,7 @@ func TestSlicingNilSlice(t *testing.T) {
var s = []int{}
s = s[:]
if s == nil {
t.Errorf("non-nil slice became nil after slicing: %#v", s)
t.Errorf("non-nil slice became nil after slicing: %#v, want []int{}", s)
}
})
}