Skip to content

Commit 2c6df2e

Browse files
committed
cmd/compile: reject misplaced go:build comments
We are converting from using error-prone ad-hoc syntax // +build lines to less error-prone, standard boolean syntax //go:build lines. The timeline is: Go 1.16: prepare for transition - Builds still use // +build for file selection. - Source files may not contain //go:build without // +build. - Builds fail when a source file contains //go:build lines without // +build lines. <<< Go 1.17: start transition - Builds prefer //go:build for file selection, falling back to // +build for files containing only // +build. - Source files may contain //go:build without // +build (but they won't build with Go 1.16). - Gofmt moves //go:build and // +build lines to proper file locations. - Gofmt introduces //go:build lines into files with only // +build lines. - Go vet rejects files with mismatched //go:build and // +build lines. Go 1.18: complete transition - Go fix removes // +build lines, leaving behind equivalent // +build lines. This CL provides part of the <<< marked line above in the Go 1.16 step: rejecting files containing //go:build but not // +build. The standard go command checks only consider the top of the file. This compiler check, along with a separate go vet check for ignored files, handles the remainder of the file. For #41184. Change-Id: I014006eebfc84ab5943de18bc90449e534f150a2 Reviewed-on: https://go-review.googlesource.com/c/go/+/240601 Trust: Russ Cox <[email protected]> Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent ec095f1 commit 2c6df2e

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ const (
5050

5151
// Runtime and cgo type pragmas
5252
NotInHeap // values of this type must not be heap allocated
53+
54+
// Go command pragmas
55+
GoBuildPragma
5356
)
5457

5558
const (
@@ -71,6 +74,8 @@ const (
7174

7275
func pragmaFlag(verb string) PragmaFlag {
7376
switch verb {
77+
case "go:build":
78+
return GoBuildPragma
7479
case "go:nointerface":
7580
if objabi.Fieldtrack_enabled != 0 {
7681
return Nointerface

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

+1
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ func (p *noder) node() {
242242
mkpackage(p.file.PkgName.Value)
243243

244244
if pragma, ok := p.file.Pragma.(*Pragma); ok {
245+
pragma.Flag &^= GoBuildPragma
245246
p.checkUnused(pragma)
246247
}
247248

test/directive.go

+7
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66

77
// Verify that misplaced directives are diagnosed.
88

9+
// ok
10+
//go:build !ignore
11+
912
//go:noinline // ERROR "misplaced compiler directive"
1013

1114
//go:noinline // ERROR "misplaced compiler directive"
1215
package main
1316

17+
//go:build bad // ERROR "misplaced compiler directive"
18+
1419
//go:nosplit
1520
func f1() {}
1621

@@ -93,3 +98,5 @@ type T6 = int
9398

9499
// EOF
95100
//go:noinline // ERROR "misplaced compiler directive"
101+
102+
//go:build bad // ERROR "misplaced compiler directive"

0 commit comments

Comments
 (0)