aboutsummaryrefslogtreecommitdiff
path: root/src/embed
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2021-01-08 15:35:19 -0500
committerRuss Cox <rsc@golang.org>2021-01-15 20:37:17 +0000
commit54198b04dbdf424d8aec922c1f8870ce0e9b7332 (patch)
tree3c39168d2b93a338f172bb91a2ec00e9df9a6ffb /src/embed
parentb386c735e7582d08a938ce2bc582f931946854b4 (diff)
downloadgo-54198b04dbdf424d8aec922c1f8870ce0e9b7332.tar.gz
go-54198b04dbdf424d8aec922c1f8870ce0e9b7332.zip
cmd/compile: disallow embed of var inside func
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>
Diffstat (limited to 'src/embed')
-rw-r--r--src/embed/internal/embedtest/embed_test.go32
-rw-r--r--src/embed/internal/embedtest/embedx_test.go14
2 files changed, 11 insertions, 35 deletions
diff --git a/src/embed/internal/embedtest/embed_test.go b/src/embed/internal/embedtest/embed_test.go
index c6a7bea7a3..40f65ffc3f 100644
--- a/src/embed/internal/embedtest/embed_test.go
+++ b/src/embed/internal/embedtest/embed_test.go
@@ -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")
diff --git a/src/embed/internal/embedtest/embedx_test.go b/src/embed/internal/embedtest/embedx_test.go
index 20d5a28c11..27fa11614e 100644
--- a/src/embed/internal/embedtest/embedx_test.go
+++ b/src/embed/internal/embedtest/embedx_test.go
@@ -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")
-}