]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: reject unknown //go: comments in std library
authorKeith Randall <khr@golang.org>
Tue, 6 Jun 2017 20:23:59 +0000 (13:23 -0700)
committerKeith Randall <khr@golang.org>
Tue, 6 Jun 2017 22:28:17 +0000 (22:28 +0000)
Fixes #18331

Change-Id: Ie5c6685be3002533b84604ff1f13f2f0850f29e2
Reviewed-on: https://go-review.googlesource.com/45010
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
src/cmd/compile/internal/gc/go.go
src/cmd/compile/internal/gc/main.go
src/cmd/compile/internal/gc/noder.go
src/cmd/go/internal/work/build.go
src/go/types/stdlib_test.go
test/fixedbugs/issue18331.go [new file with mode: 0644]

index 44dd3058308012ac390aa9d673d161d692f9062e..65d8946849727caea901521f02d95aa3889b64f3 100644 (file)
@@ -198,6 +198,9 @@ var typecheckok bool
 
 var compiling_runtime bool
 
+// Compiling the standard library
+var compiling_std bool
+
 var compiling_wrappers int
 
 var use_writebarrier bool
index 8c3878e354521910c9f689eea0c28a8b340d0946..87d8440d1c93b1d7340c295cf53c5a6051971c78 100644 (file)
@@ -174,6 +174,7 @@ func Main(archInit func(*Arch)) {
        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
index f00095c866ac6c4d7a7ddb11b9d1b352c25b2f25..3977be1d733db08ae5bd95199bdc7f39fe9e12dd 100644 (file)
@@ -1153,6 +1153,18 @@ func (p *noder) error(err error) {
        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 {
@@ -1181,6 +1193,9 @@ func (p *noder) pragma(pos src.Pos, text string) syntax.Pragma {
                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
        }
 
index c6dd6799a294506ad0ecf86fd3fd969392892dd4..25d9b36cc6738503762bca7503fbdb77f58631fa 100644 (file)
@@ -2195,6 +2195,9 @@ func (gcToolchain) gc(b *Builder, p *load.Package, archive, obj string, asmhdr b
        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
index 345c7897f36b3426a811a80e2fa9ee5941b23540..34029b8681bed022290e8630d15ca6525f062de7 100644 (file)
@@ -104,10 +104,10 @@ func testTestDir(t *testing.T, path string, ignore ...string) {
                        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
diff --git a/test/fixedbugs/issue18331.go b/test/fixedbugs/issue18331.go
new file mode 100644 (file)
index 0000000..a527bce
--- /dev/null
@@ -0,0 +1,20 @@
+// 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"
+}