Skip to content

Commit

Permalink
spec: describe an edge case of slice-to-array conversions
Browse files Browse the repository at this point in the history
Converting from nil slice to zero element array is ok, so explicitly
describe the behavior in the spec.

For #46505

Change-Id: I68f432deb6c21a7549bf7e870185fc62504b37f7
Reviewed-on: https://go-review.googlesource.com/c/go/+/430835
TryBot-Result: Gopher Robot <[email protected]>
Auto-Submit: Cuong Manh Le <[email protected]>
Reviewed-by: Robert Griesemer <[email protected]>
Reviewed-by: Cherry Mui <[email protected]>
Auto-Submit: Robert Griesemer <[email protected]>
Run-TryBot: Cuong Manh Le <[email protected]>
  • Loading branch information
cuonglm authored and gopherbot committed Sep 21, 2022
1 parent c70fd4b commit 4b58b30
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions doc/go_spec.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!--{
"Title": "The Go Programming Language Specification",
"Subtitle": "Version of September 8, 2022",
"Subtitle": "Version of September 21, 2022",
"Path": "/ref/spec"
}-->

Expand Down Expand Up @@ -5542,19 +5542,20 @@ <h4 id="Conversions_from_slice_to_array_or_array_pointer">Conversions from slice
<pre>
s := make([]byte, 2, 4)

a0 := ([0]byte)(s)
a1 := ([1]byte)(s[1:]) // a1[0] == s[1]
a2 := ([2]byte)(s) // a2[0] == s[0]
a4 := ([4]byte)(s) // panics: len([4]byte) > len(s)
a0 := [0]byte(s)
a1 := [1]byte(s[1:]) // a1[0] == s[1]
a2 := [2]byte(s) // a2[0] == s[0]
a4 := [4]byte(s) // panics: len([4]byte) > len(s)

s0 := (*[0]byte)(s) // s0 != nil
s1 := (*[1]byte)(s[1:]) // &amp;s1[0] == &amp;s[1]
s2 := (*[2]byte)(s) // &amp;s2[0] == &amp;s[0]
s4 := (*[4]byte)(s) // panics: len([4]byte) > len(s)

var t []string
t0 := (*[0]string)(t) // t0 == nil
t1 := (*[1]string)(t) // panics: len([1]string) > len(t)
t0 := [0]string(t) // ok for nil slice t
t1 := (*[0]string)(t) // t1 == nil
t2 := (*[1]string)(t) // panics: len([1]string) > len(t)

u := make([]byte, 0)
u0 := (*[0]byte)(u) // u0 != nil
Expand Down

0 comments on commit 4b58b30

Please sign in to comment.