1
0
mirror of https://github.com/golang/go synced 2024-09-23 17:20:13 -06:00

cmd/compile: fix irgen reports wrong error message for misuse of //go:embed

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>
This commit is contained in:
Cuong Manh Le 2021-10-13 17:11:16 +07:00
parent 9e8ed86813
commit 0c45ed0561
5 changed files with 28 additions and 4 deletions

View 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

View 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

View 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
)
}

View 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
)
}

View File

@ -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