]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: fix irgen reports wrong error message for misuse of //go:embed
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Wed, 13 Oct 2021 10:11:16 +0000 (17:11 +0700)
committerCuong Manh Le <cuong.manhle.vn@gmail.com>
Fri, 15 Oct 2021 01:35:56 +0000 (01:35 +0000)
Fixes #48230

Change-Id: Ic6490e065e7e79793faa0d0201dc94f5fcea694a
Reviewed-on: https://go-review.googlesource.com/c/go/+/355529
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Dan Scales <danscales@google.com>
src/cmd/compile/internal/noder/decl.go
src/cmd/compile/internal/noder/irgen.go
src/cmd/compile/internal/types2/stdlib_test.go
src/go/types/stdlib_test.go
test/fixedbugs/issue48230.go [new file with mode: 0644]

index f2dad9c30247f18f1eabdb52d315ff987f3d365a..82455f7d4a60dc139956a8f2ddf2b1fcf8acbdc3 100644 (file)
@@ -132,7 +132,11 @@ func (g *irgen) funcDecl(out *ir.Nodes, decl *syntax.FuncDecl) {
                g.target.Inits = append(g.target.Inits, fn)
        }
 
+       haveEmbed := g.haveEmbed
        g.later(func() {
+               defer func(b bool) { g.haveEmbed = b }(g.haveEmbed)
+
+               g.haveEmbed = haveEmbed
                if fn.Type().HasTParam() {
                        g.topFuncIsGeneric = true
                }
@@ -241,12 +245,15 @@ func (g *irgen) varDecl(out *ir.Nodes, decl *syntax.VarDecl) {
 
        if decl.Pragma != nil {
                pragma := decl.Pragma.(*pragmas)
-               // TODO(mdempsky): Plumb noder.importedEmbed through to here.
-               varEmbed(g.makeXPos, names[0], decl, pragma, true)
+               varEmbed(g.makeXPos, names[0], decl, pragma, g.haveEmbed)
                g.reportUnused(pragma)
        }
 
+       haveEmbed := g.haveEmbed
        do := func() {
+               defer func(b bool) { g.haveEmbed = b }(g.haveEmbed)
+
+               g.haveEmbed = haveEmbed
                values := g.exprList(decl.Values)
 
                var as2 *ir.AssignListStmt
index a3501fb90b487291728629d729fd5f1a081fa435..982e811f5fa6b5cd42cdfb5b7ecc118398b26c3f 100644 (file)
@@ -147,6 +147,9 @@ type irgen struct {
        // laterFuncs records tasks that need to run after all declarations
        // are processed.
        laterFuncs []func()
+       // haveEmbed indicates whether the current node belongs to file that
+       // imports "embed" package.
+       haveEmbed bool
 
        // exprStmtOK indicates whether it's safe to generate expressions or
        // statements yet.
@@ -254,8 +257,11 @@ Outer:
        types.ResumeCheckSize()
 
        // 3. Process all remaining declarations.
-       for _, declList := range declLists {
+       for i, declList := range declLists {
+               old := g.haveEmbed
+               g.haveEmbed = noders[i].importedEmbed
                g.decls((*ir.Nodes)(&g.target.Decls), declList)
+               g.haveEmbed = old
        }
        g.exprStmtOK = true
 
index 6e340d0777f9cad8647a8297e34c090aa83ea6b8..9c22f0167383df01e1d03708bd55180b3fc46332 100644 (file)
@@ -193,7 +193,7 @@ func TestStdFixed(t *testing.T) {
                "issue42058a.go", // types2 does not have constraints on channel element size
                "issue42058b.go", // types2 does not have constraints on channel element size
                "issue48097.go",  // go/types doesn't check validity of //go:xxx directives, and non-init bodyless function
-
+               "issue48230.go",  // go/types doesn't check validity of //go:xxx directives
        )
 }
 
index 12ed9a54f22fdce672b29a0071cb3539184f71d7..b0d7fdd3d9c55cce7689e918d32a1b83ef839d86 100644 (file)
@@ -195,6 +195,7 @@ func TestStdFixed(t *testing.T) {
                "issue42058a.go", // go/types does not have constraints on channel element size
                "issue42058b.go", // go/types does not have constraints on channel element size
                "issue48097.go",  // go/types doesn't check validity of //go:xxx directives, and non-init bodyless function
+               "issue48230.go",  // go/types doesn't check validity of //go:xxx directives
        )
 }
 
diff --git a/test/fixedbugs/issue48230.go b/test/fixedbugs/issue48230.go
new file mode 100644 (file)
index 0000000..5f21376
--- /dev/null
@@ -0,0 +1,10 @@
+// errorcheck
+
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+//go:embed issue48230.go // ERROR `go:embed only allowed in Go files that import "embed"`
+var _ string