From 1519271a939ad27da133318dc4bde7e6a41a35b5 Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Fri, 25 Jun 2021 11:17:43 -0700 Subject: [PATCH] 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 Trust: Matthew Dempsky Trust: Robert Griesemer Reviewed-by: Robert Griesemer --- doc/go_spec.html | 10 ++++++++-- src/unsafe/unsafe.go | 5 ++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index b59b37fd55..e0602418e8 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -6789,11 +6789,17 @@ and whose length and capacity are len: (*[len]ArbitraryType)(unsafe.Pointer(ptr))[:] +

+As a special case, if ptr is nil and len is zero, +Slice returns nil. +

+

The len argument must be of integer type or an untyped constant. A constant len argument must be non-negative and representable by a value of type int; if it is an untyped constant it is given type int. -If ptr is nil or len is negative at run time, +At run time, if len is negative, +or if ptr is nil and len is not zero, a run-time panic occurs.

diff --git a/src/unsafe/unsafe.go b/src/unsafe/unsafe.go index ecbd28c523..eaf72c9618 100644 --- a/src/unsafe/unsafe.go +++ b/src/unsafe/unsafe.go @@ -221,8 +221,11 @@ func Add(ptr Pointer, len IntegerType) Pointer // // (*[len]ArbitraryType)(unsafe.Pointer(ptr))[:] // +// As a special case, if ptr is nil and len is zero, Slice returns nil. +// // The len argument must be of integer type or an untyped constant. // A constant len argument must be non-negative and representable by a value of type int; // if it is an untyped constant it is given type int. -// If ptr is nil or len is negative at run time, a run-time panic occurs. +// At run time, if len is negative, or if ptr is nil and len is not zero, +// a run-time panic occurs. func Slice(ptr *ArbitraryType, len IntegerType) []ArbitraryType -- 2.50.0