]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.17] cmd/compile: allow embed into any byte slice type
authorKeith Randall <khr@golang.org>
Tue, 17 Aug 2021 11:34:40 +0000 (19:34 +0800)
committerCherry Mui <cherryyz@google.com>
Wed, 1 Sep 2021 16:49:05 +0000 (16:49 +0000)
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>
src/cmd/compile/internal/staticdata/embed.go
src/embed/internal/embedtest/embed_test.go

index 8936c4f5b44e44c7f03edf81a137e49eb68ac564..cebc22ddc31fa456ed6cfe2cfe35f2edfa540be5 100644 (file)
@@ -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
index 2d50f5e01f1fafe1975dad2fc43c0fa83720e696..b41359f4c2822552fad425240bdb747e521bf935 100644 (file)
@@ -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)
+}