Skip to content

Commit

Permalink
cmd/compile: reject misplaced go:build comments
Browse files Browse the repository at this point in the history
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 <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
rsc committed Oct 13, 2020
1 parent ec095f1 commit 2c6df2e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/cmd/compile/internal/gc/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ const (

// Runtime and cgo type pragmas
NotInHeap // values of this type must not be heap allocated

// Go command pragmas
GoBuildPragma
)

const (
Expand All @@ -71,6 +74,8 @@ const (

func pragmaFlag(verb string) PragmaFlag {
switch verb {
case "go:build":
return GoBuildPragma
case "go:nointerface":
if objabi.Fieldtrack_enabled != 0 {
return Nointerface
Expand Down
1 change: 1 addition & 0 deletions src/cmd/compile/internal/gc/noder.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ func (p *noder) node() {
mkpackage(p.file.PkgName.Value)

if pragma, ok := p.file.Pragma.(*Pragma); ok {
pragma.Flag &^= GoBuildPragma
p.checkUnused(pragma)
}

Expand Down
7 changes: 7 additions & 0 deletions test/directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@

// Verify that misplaced directives are diagnosed.

// ok
//go:build !ignore

//go:noinline // ERROR "misplaced compiler directive"

//go:noinline // ERROR "misplaced compiler directive"
package main

//go:build bad // ERROR "misplaced compiler directive"

//go:nosplit
func f1() {}

Expand Down Expand Up @@ -93,3 +98,5 @@ type T6 = int

// EOF
//go:noinline // ERROR "misplaced compiler directive"

//go:build bad // ERROR "misplaced compiler directive"

0 comments on commit 2c6df2e

Please sign in to comment.