diff options
author | Russ Cox <rsc@golang.org> | 2011-11-22 12:30:02 -0500 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2011-11-22 12:30:02 -0500 |
commit | 178abb7ec71c2e87e35b6c2bc4b11fbb70b7fc52 (patch) | |
tree | 0e598140f62dd0ef1b909b19b6931dcd1eeb8538 /test | |
parent | a7a3d6ebd1bcb7c44af5aef82bb673e3a9e29d67 (diff) | |
download | go-178abb7ec71c2e87e35b6c2bc4b11fbb70b7fc52.tar.gz |
allow direct conversion between string and named []byte, []rune
The allowed conversions before and after are:
type Tstring string
type Tbyte []byte
type Trune []rune
string <-> string // ok
string <-> []byte // ok
string <-> []rune // ok
string <-> Tstring // ok
string <-> Tbyte // was illegal, now ok
string <-> Trune // was illegal, now ok
Tstring <-> string // ok
Tstring <-> []byte // ok
Tstring <-> []rune // ok
Tstring <-> Tstring // ok
Tstring <-> Tbyte // was illegal, now ok
Tstring <-> Trune // was illegal, now ok
Update spec, compiler, tests. Use in a few packages.
We agreed on this a few months ago but never implemented it.
Fixes issue 1707.
R=golang-dev, gri, r
CC=golang-dev
http://codereview.appspot.com/5421057
Diffstat (limited to 'test')
-rw-r--r-- | test/convert1.go | 96 | ||||
-rw-r--r-- | test/convlit.go | 6 | ||||
-rw-r--r-- | test/named1.go | 4 |
3 files changed, 101 insertions, 5 deletions
diff --git a/test/convert1.go b/test/convert1.go new file mode 100644 index 000000000..9de1b7e01 --- /dev/null +++ b/test/convert1.go @@ -0,0 +1,96 @@ +// errchk $G -e $D/$F.go + +// Copyright 2011 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. + +package main + +type Tbyte []byte +type Trune []rune +type Tint64 []int64 +type Tstring string + +func main() { + s := "hello" + sb := []byte("hello") + sr := []rune("hello") + si := []int64{'h', 'e', 'l', 'l', 'o'} + + ts := Tstring(s) + tsb := Tbyte(sb) + tsr := Trune(sr) + tsi := Tint64(si) + + _ = string(s) + _ = []byte(s) + _ = []rune(s) + _ = []int64(s) // ERROR "cannot convert.*\[\]int64" + _ = Tstring(s) + _ = Tbyte(s) + _ = Trune(s) + _ = Tint64(s) // ERROR "cannot convert.*Tint64" + + _ = string(sb) + _ = []byte(sb) + _ = []rune(sb) // ERROR "cannot convert.*\[\]rune" + _ = []int64(sb) // ERROR "cannot convert.*\[\]int64" + _ = Tstring(sb) + _ = Tbyte(sb) + _ = Trune(sb) // ERROR "cannot convert.*Trune" + _ = Tint64(sb) // ERROR "cannot convert.*Tint64" + + _ = string(sr) + _ = []byte(sr) // ERROR "cannot convert.*\[\]byte" + _ = []rune(sr) + _ = []int64(sr) // ERROR "cannot convert.*\[\]int64" + _ = Tstring(sr) + _ = Tbyte(sr) // ERROR "cannot convert.*Tbyte" + _ = Trune(sr) + _ = Tint64(sr) // ERROR "cannot convert.*Tint64" + + _ = string(si) // ERROR "cannot convert.* string" + _ = []byte(si) // ERROR "cannot convert.*\[\]byte" + _ = []rune(si) // ERROR "cannot convert.*\[\]rune" + _ = []int64(si) + _ = Tstring(si) // ERROR "cannot convert.*Tstring" + _ = Tbyte(si) // ERROR "cannot convert.*Tbyte" + _ = Trune(si) // ERROR "cannot convert.*Trune" + _ = Tint64(si) + + _ = string(ts) + _ = []byte(ts) + _ = []rune(ts) + _ = []int64(ts) // ERROR "cannot convert.*\[\]int64" + _ = Tstring(ts) + _ = Tbyte(ts) + _ = Trune(ts) + _ = Tint64(ts) // ERROR "cannot convert.*Tint64" + + _ = string(tsb) + _ = []byte(tsb) + _ = []rune(tsb) // ERROR "cannot convert.*\[\]rune" + _ = []int64(tsb) // ERROR "cannot convert.*\[\]int64" + _ = Tstring(tsb) + _ = Tbyte(tsb) + _ = Trune(tsb) // ERROR "cannot convert.*Trune" + _ = Tint64(tsb) // ERROR "cannot convert.*Tint64" + + _ = string(tsr) + _ = []byte(tsr) // ERROR "cannot convert.*\[\]byte" + _ = []rune(tsr) + _ = []int64(tsr) // ERROR "cannot convert.*\[\]int64" + _ = Tstring(tsr) + _ = Tbyte(tsr) // ERROR "cannot convert.*Tbyte" + _ = Trune(tsr) + _ = Tint64(tsr) // ERROR "cannot convert.*Tint64" + + _ = string(tsi) // ERROR "cannot convert.* string" + _ = []byte(tsi) // ERROR "cannot convert.*\[\]byte" + _ = []rune(tsi) // ERROR "cannot convert.*\[\]rune" + _ = []int64(tsi) + _ = Tstring(tsi) // ERROR "cannot convert.*Tstring" + _ = Tbyte(tsi) // ERROR "cannot convert.*Tbyte" + _ = Trune(tsi) // ERROR "cannot convert.*Trune" + _ = Tint64(tsi) +} diff --git a/test/convlit.go b/test/convlit.go index 2e3b15bda..1e82d1f2f 100644 --- a/test/convlit.go +++ b/test/convlit.go @@ -54,12 +54,12 @@ var _ = []byte(ss) var _ []rune = ss // ERROR "cannot use|incompatible|invalid" var _ []byte = ss // ERROR "cannot use|incompatible|invalid" -// named slice is not +// named slice is now ok type Trune []rune type Tbyte []byte -var _ = Trune("abc") // ERROR "convert|incompatible|invalid" -var _ = Tbyte("abc") // ERROR "convert|incompatible|invalid" +var _ = Trune("abc") // ok +var _ = Tbyte("abc") // ok // implicit is still not var _ Trune = "abc" // ERROR "cannot use|incompatible|invalid" diff --git a/test/named1.go b/test/named1.go index fe71df844..64e492886 100644 --- a/test/named1.go +++ b/test/named1.go @@ -54,8 +54,8 @@ func main() { _, bb := <-c asBool(bb) // ERROR "cannot use.*type bool.*as type Bool" - _, b = <-c // ERROR "cannot .* bool.*type Bool" + _, b = <-c // ERROR "cannot .* bool.*type Bool" _ = b - asString(String(slice)) // ERROR "cannot .*type Slice.*type String" + asString(String(slice)) // ok } |