go/test/directive.go
Russ Cox 2c6df2e35d 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 <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>
2020-10-13 01:16:45 +00:00

103 lines
2.2 KiB
Go

// errorcheck
// Copyright 2020 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.
// 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() {}
//go:nosplit
//go:noinline
func f2() {}
//go:noinline // ERROR "misplaced compiler directive"
//go:noinline // ERROR "misplaced compiler directive"
var x int
//go:noinline // ERROR "misplaced compiler directive"
const c = 1
//go:noinline // ERROR "misplaced compiler directive"
type T int
// ok
//go:notinheap
type T1 int
//go:notinheap // ERROR "misplaced compiler directive"
type (
//go:notinheap
//go:noinline // ERROR "misplaced compiler directive"
T2 int //go:notinheap // ERROR "misplaced compiler directive"
T2b int
//go:notinheap
T2c int
//go:noinline // ERROR "misplaced compiler directive"
T3 int
)
//go:notinheap // ERROR "misplaced compiler directive"
type (
//go:notinheap
T4 int
)
//go:notinheap // ERROR "misplaced compiler directive"
type ()
type T5 int
func g() {} //go:noinline // ERROR "misplaced compiler directive"
// ok: attached to f (duplicated yes, but ok)
//go:noinline
//go:noinline
func f() {
//go:noinline // ERROR "misplaced compiler directive"
x := 1
//go:noinline // ERROR "misplaced compiler directive"
{
_ = x //go:noinline // ERROR "misplaced compiler directive"
}
//go:noinline // ERROR "misplaced compiler directive"
var y int //go:noinline // ERROR "misplaced compiler directive"
//go:noinline // ERROR "misplaced compiler directive"
_ = y
//go:noinline // ERROR "misplaced compiler directive"
const c = 1
//go:noinline // ERROR "misplaced compiler directive"
_ = func() {}
//go:noinline // ERROR "misplaced compiler directive"
// ok:
//go:notinheap
type T int
}
// someday there might be a directive that can apply to type aliases, but go:notinheap doesn't.
//go:notinheap // ERROR "misplaced compiler directive"
type T6 = int
// EOF
//go:noinline // ERROR "misplaced compiler directive"
//go:build bad // ERROR "misplaced compiler directive"