Skip to content
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

cmd/gc: incorrect initialization order #7134

Closed
griesemer opened this issue Jan 15, 2014 · 4 comments
Closed

cmd/gc: incorrect initialization order #7134

griesemer opened this issue Jan 15, 2014 · 4 comments
Milestone

Comments

@griesemer
Copy link
Contributor

http://play.golang.org/p/VMk-h3Js4z

produces the following output:

6 2 5 3 4 1

which appears to initialize the variables a to f in the order they are mentioned in the
composite literal in makeOrder.

However, the spec says ( http://tip.golang.org/ref/spec#Program_execution ):

"If the initializer of A depends on B, A will be set after B. Dependency analysis
does not depend on the actual values of the items being initialized, only on their
appearance in the source. A depends on B if the value of A contains a mention of B,
contains a value whose initializer mentions B, or mentions a function that mentions B,
recursively. It is an error if such dependencies form a cycle. If two items are not
interdependent, they will be initialized in the order they appear in the source,
possibly in multiple files, as presented to the compiler."

- dependency only depends on the appearance in the source: makeOrder depends on a...f
because it contains a mention of a...f. It doesn't say it depends on the order of those
mentions.

- not interdependent items are initialized in the order they appear in the source: a...f
are not interdependent, so they should be initialized in the order they appear: a, b, c,
d, e, f

Therefore the correct result should be:

1 2 3 4 5 6

gccgo produces this correct order.
@extemporalgenome
Copy link
Contributor

Comment 1:

The order seems semi-arbitrary. If it were fully dependent on makeOrder, then the output
should be `6 2 4 5 3 1`, not `6 2 5 3 4 1`.
Is the reason gccgo produces the correct order that it's optimizing away the blank
assignment?

@ianlancetaylor
Copy link
Contributor

Comment 2:

gccgo is not optimizing away the blank assignment.
It's clear that the blank assignment depends on a,b,c,d,e,f, therefore it must be
initialized after they are all initialized.  However, there are no dependencies among
a,b,c,d,e,f, therefore they must be initialized in source code order.  So the spec only
permits one order for this program, and gccgo implements the spec correctly.

@griesemer
Copy link
Contributor Author

Comment 3:

Here's another (simpler) example of diverging initialization order:
http://play.golang.org/p/aYAWMqmJZU
The f() calls are simply to illustrate the initialization order - they do not (should
not) affect the order in this case. All three front-ends produce different (!) results:
1) gc produces: cbda
2) gccgo produces: abcd
3) go/types produces: bcda
d depends on b and c so d comes after b, c. b and c are independent, so they appear in
source order, leading to the subsequence bcd for 2) and 3). For 1) the ordering of b and
c is as they appear "in the source" of the expression, while for 2) and 3) the ordering
is as b and c appear "in the source" of their declarations. Arguably, the interpretation
chosen by 2), 3) is more sensible (e.g. independent of different orderings of the same
variables in different expressions); and 1) is incorrect.
d appears before a in the source, leading to interpretation 3), but a appears before b
and c in the source, leading to interpretation 2) (and a is independent of b, c, d).
Both 2) and 3) seem valid interpretations of the spec.
See also issue #7137 (related, but not the same).

Status changed to Accepted.

@rsc
Copy link
Contributor

rsc commented Apr 3, 2014

Comment 4:

Status changed to Duplicate.

Merged into issue #7320.

@rsc rsc added this to the Go1.3 milestone Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
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

5 participants