Skip to content

cmd/compile (and gccgo): wrong package initialization order #43444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mdempsky opened this issue Dec 31, 2020 · 2 comments
Closed

cmd/compile (and gccgo): wrong package initialization order #43444

mdempsky opened this issue Dec 31, 2020 · 2 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@mdempsky
Copy link
Contributor

For the program below, my reading of the Go spec is that the output should be e a b c d. This is what go/types computes for its InitOrder.

cmd/compile instead prints a d e b c.

gccgo prints a b d e c.

package main

var sp = ""

func f(name string, _ ...interface{}) int {
	print(sp, name)
	sp = " "
	return 0
}

var a = f("a", x)
var b = f("b", y)
var c = f("c", z)
var d = func() int {
	if false {
		_ = z
	}
	return f("d")
}()
var e = f("e")

var x int
var y int = 42
var z int = func() int { return 42 }()

func main() { println() }

/cc @griesemer @ianlancetaylor

@mdempsky mdempsky added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 31, 2020
@gopherbot gopherbot added this to the Gccgo milestone Dec 31, 2020
@mdempsky
Copy link
Contributor Author

mdempsky commented Dec 31, 2020

For cmd/compile, there are two issues here:

  1. The Go spec says (emphasis added): "More precisely, a package-level variable is considered ready for initialization if it is not yet initialized and either has no initialization expression or its initialization expression has no dependencies on uninitialized variables." That is, even variables without initialization expressions still start out not yet initialized and have to wait their turn for initialization just like a variable declaration that uses an explicit zero initializer.

    cmd/compile gets this wrong because it doesn't emit assignments to zero initialize the variables (since package-level variables are zero initialized anyway), but then the sorting algorithm treats a variable without any pending assignments as already initialized. Consequently, cmd/compile fails to respect a's dependency on x.

  2. cmd/compile applies its early deadcode optimization before applying the sorting algorithm. Consequently, cmd/compile fails to respect d's dependency on z.

I suspect gccgo's issues are similar.

@gopherbot
Copy link
Contributor

Change https://golang.org/cl/280976 mentions this issue: [dev.regabi] cmd/compile: fix package-initialization order

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

2 participants