]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: don't bother compiling functions named "_"
authorKeith Randall <khr@google.com>
Tue, 22 Jan 2019 21:49:47 +0000 (13:49 -0800)
committerKeith Randall <khr@golang.org>
Tue, 22 Jan 2019 22:06:11 +0000 (22:06 +0000)
They can't be used, so we don't need code generated for them. We just
need to report errors in their bodies.

This is the minimal CL for 1.12. For 1.13, CL 158845 will remove
a bunch of special cases sprinkled about the compiler to handle "_"
functions, which should (after this CL) be unnecessary.

Update #29870

Change-Id: Iaa1c194bd0017dffdce86589fe2d36726ee83c13
Reviewed-on: https://go-review.googlesource.com/c/158820
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/compile/internal/gc/pgen.go
test/fixedbugs/issue29870.go [new file with mode: 0644]
test/fixedbugs/issue29870b.go [new file with mode: 0644]

index 63e586095052fca39ee6c4aa0d687dda93076344..1dc4b5342716b501eb63051aa8f33c9bfb4ef123 100644 (file)
@@ -243,6 +243,14 @@ func compile(fn *Node) {
        // From this point, there should be no uses of Curfn. Enforce that.
        Curfn = nil
 
+       if fn.funcname() == "_" {
+               // We don't need to generate code for this function, just report errors in its body.
+               // At this point we've generated any errors needed.
+               // (Beyond here we generate only non-spec errors, like "stack frame too large".)
+               // See issue 29870.
+               return
+       }
+
        // Set up the function's LSym early to avoid data races with the assemblers.
        fn.Func.initLSym(true)
 
diff --git a/test/fixedbugs/issue29870.go b/test/fixedbugs/issue29870.go
new file mode 100644 (file)
index 0000000..b79860c
--- /dev/null
@@ -0,0 +1,15 @@
+// compile
+
+// Copyright 2019 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.
+
+// Make sure we can compile "_" functions without crashing.
+
+package main
+
+import "log"
+
+func _() {
+       log.Println("%2F")
+}
diff --git a/test/fixedbugs/issue29870b.go b/test/fixedbugs/issue29870b.go
new file mode 100644 (file)
index 0000000..1bac566
--- /dev/null
@@ -0,0 +1,14 @@
+// errorcheck
+
+// Copyright 2019 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.
+
+// Make sure we're compiling "_" functions at least enough
+// to get to an error which is generated during walk.
+
+package main
+
+func _() {
+       x := 7 // ERROR "x declared and not used"
+}