Skip to content

Commit 2d97a87

Browse files
randall77cherrymui
authored andcommitted
[release-branch.go1.17] cmd/compile: allow embed into any byte slice type
Fixes #47754 Change-Id: Ia21ea9a67f36a3edfef1b299ae4f3b00c306cd68 Reviewed-on: https://go-review.googlesource.com/c/go/+/345092 Trust: Keith Randall <[email protected]> Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 3969694 commit 2d97a87

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/cmd/compile/internal/staticdata/embed.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func embedKind(typ *types.Type) int {
7373
if typ.Kind() == types.TSTRING {
7474
return embedString
7575
}
76-
if typ.Sym() == nil && typ.IsSlice() && typ.Elem().Kind() == types.TUINT8 {
76+
if typ.IsSlice() && typ.Elem().Kind() == types.TUINT8 {
7777
return embedBytes
7878
}
7979
return embedUnknown

src/embed/internal/embedtest/embed_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,43 @@ func TestUninitialized(t *testing.T) {
129129
t.Errorf("in uninitialized embed.FS, . is not a directory")
130130
}
131131
}
132+
133+
var (
134+
//go:embed "testdata/hello.txt"
135+
helloT []T
136+
//go:embed "testdata/hello.txt"
137+
helloUint8 []uint8
138+
//go:embed "testdata/hello.txt"
139+
helloEUint8 []EmbedUint8
140+
//go:embed "testdata/hello.txt"
141+
helloBytes EmbedBytes
142+
//go:embed "testdata/hello.txt"
143+
helloString EmbedString
144+
)
145+
146+
type T byte
147+
type EmbedUint8 uint8
148+
type EmbedBytes []byte
149+
type EmbedString string
150+
151+
// golang.org/issue/47735
152+
func TestAliases(t *testing.T) {
153+
all := testDirAll
154+
want, e := all.ReadFile("testdata/hello.txt")
155+
if e != nil {
156+
t.Fatal("ReadFile:", e)
157+
}
158+
check := func(g interface{}) {
159+
got := reflect.ValueOf(g)
160+
for i := 0; i < got.Len(); i++ {
161+
if byte(got.Index(i).Uint()) != want[i] {
162+
t.Fatalf("got %v want %v", got.Bytes(), want)
163+
}
164+
}
165+
}
166+
check(helloT)
167+
check(helloUint8)
168+
check(helloEUint8)
169+
check(helloBytes)
170+
check(helloString)
171+
}

0 commit comments

Comments
 (0)