Skip to content

Commit ad52c1c

Browse files
timothy-kinggopherbot
authored andcommitted
go/ssa/interp: support conversions to slices of named bytes
Support for conversions from string to slices of named byte and rune types and vice versa. Fixes golang/go#55115 Change-Id: Ie0fb94385f1bc89789fe299cc0c39063db676c21 Reviewed-on: https://go-review.googlesource.com/c/tools/+/501301 Reviewed-by: Alan Donovan <[email protected]> Auto-Submit: Tim King <[email protected]> gopls-CI: kokoro <[email protected]> Run-TryBot: Tim King <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 14ec3c0 commit ad52c1c

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

go/ssa/interp/interp_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ var testdataTests = []string{
129129
"width32.go",
130130

131131
"fixedbugs/issue52342.go",
132+
"fixedbugs/issue55115.go",
132133
}
133134

134135
func init() {

go/ssa/interp/ops.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -1211,8 +1211,7 @@ func conv(t_dst, t_src types.Type, x value) value {
12111211

12121212
case *types.Slice:
12131213
// []byte or []rune -> string
1214-
// TODO(adonovan): fix: type B byte; conv([]B -> string).
1215-
switch ut_src.Elem().(*types.Basic).Kind() {
1214+
switch ut_src.Elem().Underlying().(*types.Basic).Kind() {
12161215
case types.Byte:
12171216
x := x.([]value)
12181217
b := make([]byte, 0, len(x))
@@ -1234,7 +1233,6 @@ func conv(t_dst, t_src types.Type, x value) value {
12341233
x = widen(x)
12351234

12361235
// integer -> string?
1237-
// TODO(adonovan): fix: test integer -> named alias of string.
12381236
if ut_src.Info()&types.IsInteger != 0 {
12391237
if ut_dst, ok := ut_dst.(*types.Basic); ok && ut_dst.Kind() == types.String {
12401238
return fmt.Sprintf("%c", x)
@@ -1246,8 +1244,7 @@ func conv(t_dst, t_src types.Type, x value) value {
12461244
switch ut_dst := ut_dst.(type) {
12471245
case *types.Slice:
12481246
var res []value
1249-
// TODO(adonovan): fix: test named alias of rune, byte.
1250-
switch ut_dst.Elem().(*types.Basic).Kind() {
1247+
switch ut_dst.Elem().Underlying().(*types.Basic).Kind() {
12511248
case types.Rune:
12521249
for _, r := range []rune(s) {
12531250
res = append(res, r)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2023 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import "reflect"
8+
9+
func main() {
10+
type MyByte byte
11+
type MyRune rune
12+
type MyString string
13+
14+
a := []MyByte{'a', 'b', 'c'}
15+
if s := string(a); s != "abc" {
16+
panic(s)
17+
}
18+
19+
b := []MyRune{'五', '五'}
20+
if s := string(b); s != "五五" {
21+
panic(s)
22+
}
23+
24+
c := []MyByte{'l', 'o', 'r', 'e', 'm'}
25+
if s := MyString(c); s != MyString("lorem") {
26+
panic(s)
27+
}
28+
29+
d := "lorem"
30+
if a := []MyByte(d); !reflect.DeepEqual(a, []MyByte{'l', 'o', 'r', 'e', 'm'}) {
31+
panic(a)
32+
}
33+
34+
e := 42
35+
if s := MyString(e); s != "*" {
36+
panic(s)
37+
}
38+
}

0 commit comments

Comments
 (0)