var compiling_runtime bool
+// Compiling the standard library
+var compiling_std bool
+
var compiling_wrappers int
var use_writebarrier bool
Nacl = objabi.GOOS == "nacl"
flag.BoolVar(&compiling_runtime, "+", false, "compiling runtime")
+ flag.BoolVar(&compiling_std, "std", false, "compiling standard library")
objabi.Flagcount("%", "debug non-static initializers", &Debug['%'])
objabi.Flagcount("B", "disable bounds checking", &Debug['B'])
objabi.Flagcount("C", "disable printing of columns in error messages", &Debug['C']) // TODO(gri) remove eventually
p.err <- err.(syntax.Error)
}
+// pragmas that are allowed in the std lib, but don't have
+// a syntax.Pragma value (see lex.go) associated with them.
+var allowedStdPragmas = map[string]bool{
+ "go:cgo_export_static": true,
+ "go:cgo_export_dynamic": true,
+ "go:cgo_import_static": true,
+ "go:cgo_import_dynamic": true,
+ "go:cgo_ldflag": true,
+ "go:cgo_dynamic_linker": true,
+ "go:generate": true,
+}
+
// pragma is called concurrently if files are parsed concurrently.
func (p *noder) pragma(pos src.Pos, text string) syntax.Pragma {
switch {
if !compiling_runtime && prag&runtimePragmas != 0 {
p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("//%s only allowed in runtime", verb)})
}
+ if prag == 0 && !allowedStdPragmas[verb] && compiling_std {
+ p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("//%s is not allowed in the standard library", verb)})
+ }
return prag
}
if p.Name == "main" {
gcargs[1] = "main"
}
+ if p.Standard {
+ gcargs = append(gcargs, "-std")
+ }
compilingRuntime := p.Standard && (p.ImportPath == "runtime" || strings.HasPrefix(p.ImportPath, "runtime/internal"))
if compilingRuntime {
// runtime compiles with a special gc flag to emit
case "errorcheck":
expectErrors = true
for _, arg := range fields[1:] {
- if arg == "-0" || arg == "-+" {
+ if arg == "-0" || arg == "-+" || arg == "-std" {
// Marked explicitly as not expected errors (-0),
- // or marked as compiling_runtime, which is only done
- // to trigger runtime-only error output.
+ // or marked as compiling runtime/stdlib, which is only done
+ // to trigger runtime/stdlib-only error output.
// In both cases, the code should typecheck.
expectErrors = false
break
--- /dev/null
+// errorcheck -std
+// Copyright 2017 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.
+// Issue 18331: We should catch invalid pragma verbs
+// for code that resides in the standard library.
+package issue18331
+
+//go:unknown // ERROR "//go:unknown is not allowed in the standard library"
+func foo()
+
+//go:nowritebarrierc // ERROR "//go:nowritebarrierc is not allowed in the standard library"
+func bar()
+
+//go:noesape // ERROR "//go:noesape is not allowed in the standard library"
+func groot()
+
+//go:noescape
+func hey() { // ERROR "can only use //go:noescape with external func implementations"
+}