Skip to content

Commit 0575e35

Browse files
committed
cmd/compile: require 'go 1.16' go.mod line for //go:embed
This will produce better errors when earlier versions of Go compile code using //go:embed. (The import will cause a compilation error but then the go command will add to the output that the Go toolchain in use looks too old and maybe that's the problem.) This CL also adds a test for disallowing embed of a var inside a func. It's a bit too difficult to rebase down into that CL. The build system configuration check is delayed in order to make it possible to use errorcheck for these tests. Change-Id: I12ece4ff2d8d53380b63f54866e8f3497657d54c Reviewed-on: https://go-review.googlesource.com/c/go/+/282718 Trust: Russ Cox <[email protected]> Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> Reviewed-by: Jay Conrod <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent ccb2e90 commit 0575e35

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

src/cmd/compile/internal/gc/embed.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ func varEmbed(p *noder, names []*Node, typ *Node, exprs []*Node, embeds []Pragma
6767
p.yyerrorpos(pos, "invalid go:embed: missing import \"embed\"")
6868
return
6969
}
70-
if embedCfg.Patterns == nil {
71-
p.yyerrorpos(pos, "invalid go:embed: build system did not supply embed configuration")
72-
return
73-
}
7470
if len(names) > 1 {
7571
p.yyerrorpos(pos, "go:embed cannot apply to multiple vars")
7672
return
@@ -186,6 +182,18 @@ func dumpembeds() {
186182
// initEmbed emits the init data for a //go:embed variable,
187183
// which is either a string, a []byte, or an embed.FS.
188184
func initEmbed(v *Node) {
185+
commentPos := v.Name.Param.EmbedList()[0].Pos
186+
if !langSupported(1, 16, localpkg) {
187+
lno := lineno
188+
lineno = commentPos
189+
yyerrorv("go1.16", "go:embed")
190+
lineno = lno
191+
return
192+
}
193+
if embedCfg.Patterns == nil {
194+
yyerrorl(commentPos, "invalid go:embed: build system did not supply embed configuration")
195+
return
196+
}
189197
kind := embedKind(v.Type)
190198
if kind == embedUnknown {
191199
yyerrorl(v.Pos, "go:embed cannot apply to var of type %v", v.Type)

src/go/types/stdlib_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ func TestStdTest(t *testing.T) {
155155
testTestDir(t, filepath.Join(runtime.GOROOT(), "test"),
156156
"cmplxdivide.go", // also needs file cmplxdivide1.go - ignore
157157
"directive.go", // tests compiler rejection of bad directive placement - ignore
158+
"embedfunc.go", // tests //go:embed
159+
"embedvers.go", // tests //go:embed
158160
)
159161
}
160162

test/embedfunc.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// errorcheck
2+
3+
// Copyright 2021 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package p
8+
9+
import _ "embed"
10+
11+
func f() {
12+
//go:embed x.txt // ERROR "go:embed cannot apply to var inside func"
13+
var x string
14+
_ = x
15+
}

test/embedvers.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// errorcheck -lang=go1.15
2+
3+
// Copyright 2021 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package p
8+
9+
import _ "embed"
10+
11+
//go:embed x.txt // ERROR "go:embed requires go1.16 or later"
12+
var x string

0 commit comments

Comments
 (0)