]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile: disallow embed of var inside func
authorRuss Cox <rsc@golang.org>
Fri, 8 Jan 2021 20:35:19 +0000 (15:35 -0500)
committerRuss Cox <rsc@golang.org>
Fri, 15 Jan 2021 20:37:17 +0000 (20:37 +0000)
Allowing embedding into []byte inside a func creates an
unfortunate problem: either all calls start with the same
underlying data and can see each other's changes to the
underlying data (surprising and racy!) or all calls start
by making their own copy of the underlying data
(surprising and expensive!).

After discussion on #43216, the consensus was to remove
support for all vars embedded inside functions.

Fixes #43216.

Change-Id: I01e62b5f0dcd9e8566c6d2286218e97803f54704
Reviewed-on: https://go-review.googlesource.com/c/go/+/282714
Trust: Russ Cox <rsc@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
src/cmd/compile/internal/gc/embed.go
src/embed/internal/embedtest/embed_test.go
src/embed/internal/embedtest/embedx_test.go

index 103949c1f9f08013f37149371e99a2587f274bdc..6db246eece54b1eee2368b0fd62a6b0fac1cf5a3 100644 (file)
@@ -133,13 +133,8 @@ func varEmbed(p *noder, names []*Node, typ *Node, exprs []*Node, embeds []Pragma
 
        v := names[0]
        if dclcontext != PEXTERN {
-               numLocalEmbed++
-               v = newnamel(v.Pos, lookupN("embed.", numLocalEmbed))
-               v.Sym.Def = asTypesNode(v)
-               v.Name.Param.Ntype = typ
-               v.SetClass(PEXTERN)
-               externdcl = append(externdcl, v)
-               exprs = []*Node{v}
+               p.yyerrorpos(pos, "go:embed cannot apply to var inside func")
+               return exprs
        }
 
        v.Name.Param.SetEmbedFiles(list)
index c6a7bea7a33125419b5d59cc46e7b3de8fc51a97..40f65ffc3ff42444a1529f85defa3ce668ce8250 100644 (file)
@@ -73,24 +73,11 @@ func TestGlobal(t *testing.T) {
        testString(t, string(glass), "glass", "I can eat glass and it doesn't hurt me.\n")
 }
 
-func TestLocal(t *testing.T) {
-       //go:embed testdata/k*.txt
-       var local embed.FS
-       testFiles(t, local, "testdata/ken.txt", "If a program is too slow, it must have a loop.\n")
-
-       //go:embed testdata/k*.txt
-       var s string
-       testString(t, s, "local variable s", "If a program is too slow, it must have a loop.\n")
-
-       //go:embed testdata/h*.txt
-       var b []byte
-       testString(t, string(b), "local variable b", "hello, world\n")
-}
+//go:embed testdata
+var testDirAll embed.FS
 
 func TestDir(t *testing.T) {
-       //go:embed testdata
-       var all embed.FS
-
+       all := testDirAll
        testFiles(t, all, "testdata/hello.txt", "hello, world\n")
        testFiles(t, all, "testdata/i/i18n.txt", "internationalization\n")
        testFiles(t, all, "testdata/i/j/k/k8s.txt", "kubernetes\n")
@@ -102,12 +89,15 @@ func TestDir(t *testing.T) {
        testDir(t, all, "testdata/i/j/k", "k8s.txt")
 }
 
-func TestHidden(t *testing.T) {
-       //go:embed testdata
-       var dir embed.FS
+//go:embed testdata
+var testHiddenDir embed.FS
 
-       //go:embed testdata/*
-       var star embed.FS
+//go:embed testdata/*
+var testHiddenStar embed.FS
+
+func TestHidden(t *testing.T) {
+       dir := testHiddenDir
+       star := testHiddenStar
 
        t.Logf("//go:embed testdata")
 
index 20d5a28c11df82e7c5ae440cebeb1584581ccac2..27fa11614e9268ea19149cb5c4cabd9faa374407 100644 (file)
@@ -90,17 +90,3 @@ func TestXGlobal(t *testing.T) {
        }
        bbig[0] = old
 }
-
-func TestXLocal(t *testing.T) {
-       //go:embed testdata/*o.txt
-       var local embed.FS
-       testFiles(t, local, "testdata/hello.txt", "hello, world\n")
-
-       //go:embed testdata/k*.txt
-       var s string
-       testString(t, s, "local variable s", "If a program is too slow, it must have a loop.\n")
-
-       //go:embed testdata/h*.txt
-       var b []byte
-       testString(t, string(b), "local variable b", "hello, world\n")
-}