Skip to content

Commit 26d5f03

Browse files
cuonglmmdempsky
authored andcommitted
cmd/compile: add test for skipping empty init functions
CL 200958 adds skipping empty init function feature without any tests for it. A codegen test sounds ideal, but it's unlikely that we can make one for now, so use a program to manipulate runtime/proc.go:initTask directly. Updates #34869 Change-Id: I2683b9a1ace36af6861af02a3a9fb18b3110b282 Reviewed-on: https://go-review.googlesource.com/c/go/+/204217 Run-TryBot: Cuong Manh Le <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> Reviewed-by: Keith Randall <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 210e367 commit 26d5f03

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/runtime/proc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5361,6 +5361,7 @@ func gcd(a, b uint32) uint32 {
53615361
}
53625362

53635363
// An initTask represents the set of initializations that need to be done for a package.
5364+
// Keep in sync with ../../test/initempty.go:initTask
53645365
type initTask struct {
53655366
// TODO: pack the first 3 fields more tightly?
53665367
state uintptr // 0 = uninitialized, 1 = in progress, 2 = done

test/initempty.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// run
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+
// Test that empty init functions are skipped.
8+
9+
package main
10+
11+
import _ "unsafe" // for go:linkname
12+
13+
type initTask struct {
14+
state uintptr
15+
ndeps uintptr
16+
nfns uintptr
17+
}
18+
19+
//go:linkname main_inittask main..inittask
20+
var main_inittask initTask
21+
22+
func main() {
23+
if nfns := main_inittask.nfns; nfns != 0 {
24+
println(nfns)
25+
panic("unexpected init funcs")
26+
}
27+
}
28+
29+
func init() {
30+
}
31+
32+
func init() {
33+
if false {
34+
}
35+
}
36+
37+
func init() {
38+
for false {
39+
}
40+
}

0 commit comments

Comments
 (0)