Skip to content

Commit 1fb5961

Browse files
committed
cmd/compile: don't bother compiling functions named "_"
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 <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent ef82ecd commit 1fb5961

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/cmd/compile/internal/gc/pgen.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,14 @@ func compile(fn *Node) {
243243
// From this point, there should be no uses of Curfn. Enforce that.
244244
Curfn = nil
245245

246+
if fn.funcname() == "_" {
247+
// We don't need to generate code for this function, just report errors in its body.
248+
// At this point we've generated any errors needed.
249+
// (Beyond here we generate only non-spec errors, like "stack frame too large".)
250+
// See issue 29870.
251+
return
252+
}
253+
246254
// Set up the function's LSym early to avoid data races with the assemblers.
247255
fn.Func.initLSym(true)
248256

test/fixedbugs/issue29870.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// compile
2+
3+
// Copyright 2019 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
// Make sure we can compile "_" functions without crashing.
8+
9+
package main
10+
11+
import "log"
12+
13+
func _() {
14+
log.Println("%2F")
15+
}

test/fixedbugs/issue29870b.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// errorcheck
2+
3+
// Copyright 2019 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
// Make sure we're compiling "_" functions at least enough
8+
// to get to an error which is generated during walk.
9+
10+
package main
11+
12+
func _() {
13+
x := 7 // ERROR "x declared and not used"
14+
}

0 commit comments

Comments
 (0)