aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeith Randall <khr@golang.org>2021-08-17 19:34:40 +0800
committerCherry Mui <cherryyz@google.com>2021-09-01 16:49:05 +0000
commit2d97a87287366f384a08ad1bb4da7b630fd589a0 (patch)
tree615859965fd6803bccb925a4a0a5b32b8d1c852a
parent396969420323bf52f120d842931dd8029e3fdf28 (diff)
downloadgo-2d97a87287366f384a08ad1bb4da7b630fd589a0.tar.gz
go-2d97a87287366f384a08ad1bb4da7b630fd589a0.zip
[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 <khr@golang.org> Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
-rw-r--r--src/cmd/compile/internal/staticdata/embed.go2
-rw-r--r--src/embed/internal/embedtest/embed_test.go40
2 files changed, 41 insertions, 1 deletions
diff --git a/src/cmd/compile/internal/staticdata/embed.go b/src/cmd/compile/internal/staticdata/embed.go
index 8936c4f5b44..cebc22ddc31 100644
--- a/src/cmd/compile/internal/staticdata/embed.go
+++ b/src/cmd/compile/internal/staticdata/embed.go
@@ -73,7 +73,7 @@ func embedKind(typ *types.Type) int {
if typ.Kind() == types.TSTRING {
return embedString
}
- if typ.Sym() == nil && typ.IsSlice() && typ.Elem().Kind() == types.TUINT8 {
+ if typ.IsSlice() && typ.Elem().Kind() == types.TUINT8 {
return embedBytes
}
return embedUnknown
diff --git a/src/embed/internal/embedtest/embed_test.go b/src/embed/internal/embedtest/embed_test.go
index 2d50f5e01f1..b41359f4c28 100644
--- a/src/embed/internal/embedtest/embed_test.go
+++ b/src/embed/internal/embedtest/embed_test.go
@@ -129,3 +129,43 @@ func TestUninitialized(t *testing.T) {
t.Errorf("in uninitialized embed.FS, . is not a directory")
}
}
+
+var (
+ //go:embed "testdata/hello.txt"
+ helloT []T
+ //go:embed "testdata/hello.txt"
+ helloUint8 []uint8
+ //go:embed "testdata/hello.txt"
+ helloEUint8 []EmbedUint8
+ //go:embed "testdata/hello.txt"
+ helloBytes EmbedBytes
+ //go:embed "testdata/hello.txt"
+ helloString EmbedString
+)
+
+type T byte
+type EmbedUint8 uint8
+type EmbedBytes []byte
+type EmbedString string
+
+// golang.org/issue/47735
+func TestAliases(t *testing.T) {
+ all := testDirAll
+ want, e := all.ReadFile("testdata/hello.txt")
+ if e != nil {
+ t.Fatal("ReadFile:", e)
+ }
+ check := func(g interface{}) {
+ got := reflect.ValueOf(g)
+ for i := 0; i < got.Len(); i++ {
+ if byte(got.Index(i).Uint()) != want[i] {
+ t.Fatalf("got %v want %v", got.Bytes(), want)
+ }
+ }
+ }
+ check(helloT)
+ check(helloUint8)
+ check(helloEUint8)
+ check(helloBytes)
+ check(helloString)
+}