Skip to content

Commit f2c7e78

Browse files
committed
spec: document operations which accept []byte|string constrained types
Pre-1.18, as special cases, the built-in operations append and copy accepted strings as second arguments if the first argument was a byte slice. With Go 1.18, these two built-ins as well as slice expressions rely on the notion of core types in their specification. Because we want to permit slice expressions, append, and copy to operate on (1st or 2nd operands) that are type parameters restricted by []byte | string (and variations thereof), the simple notion of core type is not sufficient for these three operations. (The compiler already permits such more relaxed operations). In the section on core types, add a paragraph and examples introducing the (artificial) core type "bypestring", which describes the core type of type sets whose underlying types are []byte or string. Adjust the rules for slice expressions, append, and copy accordingly. Also (unrelated): Adjust prose in the only paragraph where we used personal speech ("we") to impersonal speech, to match the rest of the spec. Fixes #52859. Change-Id: I1cbda3095a1136fb99334cc3a62a9a349a27ce1e Reviewed-on: https://go-review.googlesource.com/c/go/+/412234 Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent ab422f2 commit f2c7e78

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

doc/go_spec.html

Lines changed: 36 additions & 8 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 June 14, 2022",
3+
"Subtitle": "Version of June 21, 2022",
44
"Path": "/ref/spec"
55
}-->
66

@@ -1811,6 +1811,31 @@ <h3 id="Core_types">Core types</h3>
18111811
interface{ &lt;-chan int | chan&lt;- int } // directional channels have different directions
18121812
</pre>
18131813

1814+
<p>
1815+
Some operations (<a href="#Slice_expressions">slice expressions</a>,
1816+
<a href="#Appending_and_copying_slices"><code>append</code> and <code>copy</code></a>)
1817+
rely on a slightly more loose form of core types which accept byte slices and strings.
1818+
Specifically, if there are exactly two types, <code>[]byte</code> and <code>string</code>,
1819+
which are the underlying types of all types in the type set of interface <code>T</code>,
1820+
the core type of <code>T</code> is called <code>bytestring</code>.
1821+
</p>
1822+
1823+
<p>
1824+
Examples of interfaces with <code>bytestring</code> core types:
1825+
</p>
1826+
1827+
<pre>
1828+
interface{ int } // int (same as ordinary core type)
1829+
interface{ []byte | string } // bytestring
1830+
interface{ ~[]byte | myString } // bytestring
1831+
</pre>
1832+
1833+
<p>
1834+
Note that <code>bytestring</code> is not a real type; it cannot be used to declare
1835+
variables are compose other types. It exists solely to describe the behavior of some
1836+
operations that read from a sequence of bytes, which may be a byte slice or a string.
1837+
</p>
1838+
18141839
<h3 id="Type_identity">Type identity</h3>
18151840

18161841
<p>
@@ -3837,7 +3862,8 @@ <h4>Simple slice expressions</h4>
38373862

38383863
<p>
38393864
constructs a substring or slice. The <a href="#Core_types">core type</a> of
3840-
<code>a</code> must be a string, array, pointer to array, or slice.
3865+
<code>a</code> must be a string, array, pointer to array, slice, or a
3866+
<a href="#Core_types"><code>bytestring</code></a>.
38413867
The <i>indices</i> <code>low</code> and
38423868
<code>high</code> select which elements of operand <code>a</code> appear
38433869
in the result. The result has indices starting at 0 and length equal to
@@ -5469,7 +5495,7 @@ <h4 id="Conversions_to_and_from_a_string_type">Conversions to and from a string
54695495

54705496
type myRune rune
54715497
string([]myRune{0x266b, 0x266c}) // "\u266b\u266c" == "♫♬"
5472-
myString([]myRune{0x1F30E}) // "\U0001f30e" == "🌎"
5498+
myString([]myRune{0x1f30e}) // "\U0001f30e" == "🌎"
54735499
</pre>
54745500
</li>
54755501

@@ -7197,8 +7223,9 @@ <h3 id="Appending_and_copying_slices">Appending to and copying slices</h3>
71977223
and the respective <a href="#Passing_arguments_to_..._parameters">parameter
71987224
passing rules</a> apply.
71997225
As a special case, if the core type of <code>s</code> is <code>[]byte</code>,
7200-
<code>append</code> also accepts a second argument with core type <code>string</code>
7201-
followed by <code>...</code>. This form appends the bytes of the string.
7226+
<code>append</code> also accepts a second argument with core type
7227+
<a href="#Core_types"><code>bytestring</code></a> followed by <code>...</code>.
7228+
This form appends the bytes of the byte slice or string.
72027229
</p>
72037230

72047231
<pre class="grammar">
@@ -7235,8 +7262,9 @@ <h3 id="Appending_and_copying_slices">Appending to and copying slices</h3>
72357262
The number of elements copied is the minimum of
72367263
<code>len(src)</code> and <code>len(dst)</code>.
72377264
As a special case, if the destination's core type is <code>[]byte</code>,
7238-
<code>copy</code> also accepts a source argument with core type <code>string</code>.
7239-
This form copies the bytes from the string into the byte slice.
7265+
<code>copy</code> also accepts a source argument with core type
7266+
</a> <a href="#Core_types"><code>bytestring</code></a>.
7267+
This form copies the bytes from the byte slice or string into the byte slice.
72407268
</p>
72417269

72427270
<pre class="grammar">
@@ -7550,7 +7578,7 @@ <h3 id="Import_declarations">Import declarations</h3>
75507578
</p>
75517579

75527580
<p>
7553-
Assume we have compiled a package containing the package clause
7581+
Consider a compiled a package containing the package clause
75547582
<code>package math</code>, which exports function <code>Sin</code>, and
75557583
installed the compiled package in the file identified by
75567584
<code>"lib/math"</code>.

0 commit comments

Comments
 (0)