Skip to content

cmd/gc: wrong order of initialization when using method calls. #3824

@axw

Description

@axw
Before filing a bug, please check whether it has been fixed since the
latest release. Search the issue tracker and check that you're running the
latest version of Go:

Run "go version" and compare against
http://golang.org/doc/devel/release.html  If a newer version of Go exists,
install it and retry what you did to reproduce the problem.

Thanks.

What steps will reproduce the problem?
If possible, include a link to a program on play.golang.org.
1. Clone math package in GOPATH, give it a different name (newmath).
2. Modify zipf.go to declare a package level variable X, type string, with an
initialiser (say "X").
3. Modify rng.go to declare another called Y which takes X and adds some string to it
(say "Y", to make "XY").
3. Modify rng.go to println(Y) at the top of rngSource.Seed.
4. Run a program that calls rand.Seed(0)

What is the expected output?

The string "XY" printed during initialisation, again when rand.Seed is called
from main.

What do you see instead?

An empty line (during init) followed by "XY" (rand.Seed called in main).

Which compiler are you using (5g, 6g, 8g, gccgo)?

8g

Which operating system are you using?

Ubuntu Linux 12.04 x86

Which version are you using?  (run 'go version')

go version weekly.2012-03-27 +d2213960ade2

Please provide any additional information below.

This all seems to stem from the initialisation of the globalRand variable in rand.go.
It appears that the compiler has missed the transitive dependency of globalRand on Y.

If you add a function, say "primethepump", to rng.go referencing Y, and call
it in
NewSource just prior to rng.Seed, then Y will be properly initialised.

I've attached the modified math package and calling program which exhibit the issue.
Sorry, I haven't been able to isolate the problem further so far.

Attachments:

  1. varinit.tar.gz (159815 bytes)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions