Skip to content

Commit 89baedc

Browse files
authored
compiler/prelude: Keep nil slice nil when slicing. (gopherjs#852)
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. Slicing a nil slice would incorrectly make it an empty but non-nil slice. This change fixes that. The minified prelude was regenerated with: go generate ./compiler/prelude Fixes gopherjs#851.
1 parent 0892b62 commit 89baedc

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

compiler/prelude/prelude.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ var $subslice = function(slice, low, high, max) {
107107
if (low < 0 || high < low || max < high || high > slice.$capacity || max > slice.$capacity) {
108108
$throwRuntimeError("slice bounds out of range");
109109
}
110+
if (slice === slice.constructor.nil) {
111+
return slice;
112+
}
110113
var s = new slice.constructor(slice.$array);
111114
s.$offset = slice.$offset + low;
112115
s.$length = high - low;

0 commit comments

Comments
 (0)