Skip to content

Commit 1519271

Browse files
committed
spec: change unsafe.Slice((*T)(nil), 0) to return []T(nil)
Updates #46742. Change-Id: I044933a657cd1a5cdb29863e49751df5fe9c258a Reviewed-on: https://go-review.googlesource.com/c/go/+/331069 Run-TryBot: Matthew Dempsky <[email protected]> Trust: Matthew Dempsky <[email protected]> Trust: Robert Griesemer <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 5385e23 commit 1519271

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

doc/go_spec.html

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--{
22
"Title": "The Go Programming Language Specification",
3-
"Subtitle": "Version of Jun 22, 2021",
3+
"Subtitle": "Version of Jun 28, 2021",
44
"Path": "/ref/spec"
55
}-->
66

@@ -6789,11 +6789,17 @@ <h3 id="Package_unsafe">Package <code>unsafe</code></h3>
67896789
(*[len]ArbitraryType)(unsafe.Pointer(ptr))[:]
67906790
</pre>
67916791

6792+
<p>
6793+
As a special case, if <code>ptr</code> is <code>nil</code> and <code>len</code> is zero,
6794+
<code>Slice</code> returns <code>nil</code>.
6795+
</p>
6796+
67926797
<p>
67936798
The <code>len</code> argument must be of integer type or an untyped <a href="#Constants">constant</a>.
67946799
A constant <code>len</code> argument must be non-negative and <a href="#Representability">representable</a> by a value of type <code>int</code>;
67956800
if it is an untyped constant it is given type <code>int</code>.
6796-
If <code>ptr</code> is <code>nil</code> or <code>len</code> is negative at run time,
6801+
At run time, if <code>len</code> is negative,
6802+
or if <code>ptr</code> is <code>nil</code> and <code>len</code> is not zero,
67976803
a <a href="#Run_time_panics">run-time panic</a> occurs.
67986804
</p>
67996805

src/unsafe/unsafe.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,11 @@ func Add(ptr Pointer, len IntegerType) Pointer
221221
//
222222
// (*[len]ArbitraryType)(unsafe.Pointer(ptr))[:]
223223
//
224+
// As a special case, if ptr is nil and len is zero, Slice returns nil.
225+
//
224226
// The len argument must be of integer type or an untyped constant.
225227
// A constant len argument must be non-negative and representable by a value of type int;
226228
// if it is an untyped constant it is given type int.
227-
// If ptr is nil or len is negative at run time, a run-time panic occurs.
229+
// At run time, if len is negative, or if ptr is nil and len is not zero,
230+
// a run-time panic occurs.
228231
func Slice(ptr *ArbitraryType, len IntegerType) []ArbitraryType

0 commit comments

Comments
 (0)