Skip to content

doc/spec: Package initialization order: initialized order d b c a, real result is d c b a #31724

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
terryding77 opened this issue Apr 28, 2019 · 3 comments

Comments

@terryding77
Copy link

terryding77 commented Apr 28, 2019

the initialization order is <code>d</code>, <code>b</code>, <code>c</code>, <code>a</code>.

the code as spec#Package_initializationthe

package main

import "fmt"

var (
    a = c + b
    b = f()
    c = f()
    d = 3
)

func f() int {
    d++
    return d
}

func main() {
    fmt.Println("a", a)
    fmt.Println("b", b)
    fmt.Println("c", c)
    fmt.Println("d", d)
}

got

result:
a 9
b 5
c 4
d 5

but with the order in spec d-> b -> c -> a, I think the expect result shoud be

a 9
b 4
c 5
d 5

if I change a = c + b to a = b + c the order is same as spec init order d b c a
so is there something wrong?

my go version is go version go1.12.4 linux/amd64

@terryding77 terryding77 changed the title the initialized order the initialized order d b c a, real result is d c b a Apr 28, 2019
@terryding77 terryding77 changed the title the initialized order d b c a, real result is d c b a doc/spec#Package_initializationthe: initialized order d b c a, real result is d c b a Apr 28, 2019
@randall77
Copy link
Contributor

I agree.
It seems very odd that this bug is here. This exact example is in the spec. There should be a test!

The gc toolchain has had this bug since at least 1.2.
The gccgo toolchain (7.3.0, at least) gets it right!

@randall77 randall77 added this to the Go1.13 milestone Apr 28, 2019
@dmitshur
Copy link
Member

I think this is the same as issue #22326.

@dmitshur
Copy link
Member

@terryding77 Thanks for reporting. I'll close this as duplicate.

@randall77 Feel free to apply release-blocker label to the other issue too, if you think it still applies.

@ALTree ALTree changed the title doc/spec#Package_initializationthe: initialized order d b c a, real result is d c b a doc/spec: Package initialization order: initialized order d b c a, real result is d c b a Apr 29, 2019
@golang golang locked and limited conversation to collaborators Apr 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants