]> Cypherpunks repositories - gostls13.git/commitdiff
spec: allow conversion from slice to array ptr
authorJosh Bleecher Snyder <josharian@gmail.com>
Sat, 25 Jan 2020 19:07:33 +0000 (11:07 -0800)
committerJosh Bleecher Snyder <josharian@gmail.com>
Tue, 20 Apr 2021 23:47:34 +0000 (23:47 +0000)
Implementation follows in subsequent changes.

Updates #395

Change-Id: Ic97ee822805e4c236fdd9d224e776cb2ae62c817
Reviewed-on: https://go-review.googlesource.com/c/go/+/216424
Trust: Josh Bleecher Snyder <josharian@gmail.com>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
doc/go_spec.html

index 8c9003434c247f32e0370c5c59a773d5b54a9973..13b8beb06c9c3b1a02f3a962dcd142671ff95579 100644 (file)
@@ -1,6 +1,6 @@
 <!--{
        "Title": "The Go Programming Language Specification",
-       "Subtitle": "Version of Mar 16, 2021",
+       "Subtitle": "Version of Apr 20, 2021",
        "Path": "/ref/spec"
 }-->
 
@@ -4164,6 +4164,10 @@ in any of these cases:
        <li>
        <code>x</code> is a string and <code>T</code> is a slice of bytes or runes.
        </li>
+       <li>
+       <code>x</code> is a slice, <code>T</code> is a pointer to an array,
+       and the slice and array types have <a href="#Type_identity">identical</a> element types.
+       </li>
 </ul>
 
 <p>
@@ -4314,6 +4318,24 @@ MyRunes("白鵬翔")           // []rune{0x767d, 0x9d6c, 0x7fd4}
 </li>
 </ol>
 
+<h4 id="Conversions_from_slice_to_array_pointer">Conversions from slice to array pointer</h4>
+
+<p>
+Converting a slice to an array pointer yields a pointer to the underlying array of the slice.
+If the <a href="#Length_and_capacity">length</a> of the slice is less than the length of the array,
+a <a href="#Run_time_panics">run-time panic<a/> occurs.
+</p>
+
+<pre>
+s := make([]byte, 2, 4)
+s0 := (*[0]byte)(s)      // s0 != nil
+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(s)
+</pre>
 
 <h3 id="Constant_expressions">Constant expressions</h3>