]> Cypherpunks repositories - gostls13.git/commitdiff
spec: fix spec on conversions to match implementations
authorRobert Griesemer <gri@golang.org>
Fri, 21 Jun 2013 23:11:13 +0000 (16:11 -0700)
committerRobert Griesemer <gri@golang.org>
Fri, 21 Jun 2013 23:11:13 +0000 (16:11 -0700)
The existing compilers convert empty strings to empty
but non-nil byte and rune slices. The spec required
a nil byte and rune slice in those cases. That seems
an odd additional requirement. Adjust the spec to
match the reality.

Also, removed over-specification for conversions of
nil []byte and []rune: such nil slices already act
like empty slices and thus don't need extra language.
Added extra examples instead.

Fixes #5704.

R=rsc, r, iant
CC=golang-dev
https://golang.org/cl/10440045

doc/go_spec.html
test/fixedbugs/issue5704.go [new file with mode: 0644]

index 1e45e73d485d328be5b40a44f4a5bd10497254cc..a1dc7d8b1900d96604608e360befe26f59896982 100644 (file)
@@ -1,6 +1,6 @@
 <!--{
        "Title": "The Go Programming Language Specification",
-       "Subtitle": "Version of June 11, 2013",
+       "Subtitle": "Version of June 21, 2013",
        "Path": "/ref/spec"
 }-->
 
@@ -3735,11 +3735,12 @@ MyString(0x65e5)  // "\u65e5" == "日" == "\xe6\x97\xa5"
 
 <li>
 Converting a slice of bytes to a string type yields
-a string whose successive bytes are the elements of the slice.  If
-the slice value is <code>nil</code>, the result is the empty string.
+a string whose successive bytes are the elements of the slice.
 
 <pre>
-string([]byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'})  // "hellø"
+string([]byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'})   // "hellø"
+string([]byte{})                                     // ""
+string([]byte(nil))                                  // ""
 
 type MyBytes []byte
 string(MyBytes{'h', 'e', 'l', 'l', '\xc3', '\xb8'})  // "hellø"
@@ -3749,11 +3750,12 @@ string(MyBytes{'h', 'e', 'l', 'l', '\xc3', '\xb8'})  // "hellø"
 <li>
 Converting a slice of runes to a string type yields
 a string that is the concatenation of the individual rune values
-converted to strings.  If the slice value is <code>nil</code>, the
-result is the empty string.
+converted to strings.
 
 <pre>
-string([]rune{0x767d, 0x9d6c, 0x7fd4})  // "\u767d\u9d6c\u7fd4" == "白鵬翔"
+string([]rune{0x767d, 0x9d6c, 0x7fd4})   // "\u767d\u9d6c\u7fd4" == "白鵬翔"
+string([]rune{})                         // ""
+string([]rune(nil))                      // ""
 
 type MyRunes []rune
 string(MyRunes{0x767d, 0x9d6c, 0x7fd4})  // "\u767d\u9d6c\u7fd4" == "白鵬翔"
@@ -3763,10 +3765,11 @@ string(MyRunes{0x767d, 0x9d6c, 0x7fd4})  // "\u767d\u9d6c\u7fd4" == "白鵬翔"
 <li>
 Converting a value of a string type to a slice of bytes type
 yields a slice whose successive elements are the bytes of the string.
-If the string is empty, the result is <code>[]byte(nil)</code>.
 
 <pre>
 []byte("hellø")   // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}
+[]byte("")        // []byte{}
+
 MyBytes("hellø")  // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}
 </pre>
 </li>
@@ -3774,9 +3777,11 @@ MyBytes("hellø")  // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}
 <li>
 Converting a value of a string type to a slice of runes type
 yields a slice containing the individual Unicode code points of the string.
-If the string is empty, the result is <code>[]rune(nil)</code>.
+
 <pre>
 []rune(MyString("白鵬翔"))  // []rune{0x767d, 0x9d6c, 0x7fd4}
+[]rune("")                 // []rune{}
+
 MyRunes("白鵬翔")           // []rune{0x767d, 0x9d6c, 0x7fd4}
 </pre>
 </li>
diff --git a/test/fixedbugs/issue5704.go b/test/fixedbugs/issue5704.go
new file mode 100644 (file)
index 0000000..1dfa072
--- /dev/null
@@ -0,0 +1,46 @@
+// run
+
+// Copyright 2012 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Issue 5704: Conversions of empty strings to byte
+// or rune slices return empty but non-nil slices.
+
+package main
+
+type (
+       mystring string
+       mybytes  []byte
+       myrunes  []rune
+)
+
+func checkBytes(s []byte, arg string) {
+       if len(s) != 0 {
+               panic("len(" + arg + ") != 0")
+       }
+       if s == nil {
+               panic(arg + " == nil")
+       }
+}
+
+func checkRunes(s []rune, arg string) {
+       if len(s) != 0 {
+               panic("len(" + arg + ") != 0")
+       }
+       if s == nil {
+               panic(arg + " == nil")
+       }
+}
+
+func main() {
+       checkBytes([]byte(""), `[]byte("")`)
+       checkBytes([]byte(mystring("")), `[]byte(mystring(""))`)
+       checkBytes(mybytes(""), `mybytes("")`)
+       checkBytes(mybytes(mystring("")), `mybytes(mystring(""))`)
+
+       checkRunes([]rune(""), `[]rune("")`)
+       checkRunes([]rune(mystring("")), `[]rune(mystring(""))`)
+       checkRunes(myrunes(""), `myrunes("")`)
+       checkRunes(myrunes(mystring("")), `myrunes(mystring(""))`)
+}