Commit b636f192 authored by Rob Pike's avatar Rob Pike

spec: fix description of initialization

The analysis does not depend on the values of the items.
Fixes #4648.

R=golang-dev, gri, rsc
CC=golang-dev
https://golang.org/cl/7593050
parent 5ae4012b
...@@ -3542,7 +3542,7 @@ using an addressable value will automatically take the address of that value: <c ...@@ -3542,7 +3542,7 @@ using an addressable value will automatically take the address of that value: <c
f := t.Mv; f(7) // like t.Mv(7) f := t.Mv; f(7) // like t.Mv(7)
f := pt.Mp; f(7) // like pt.Mp(7) f := pt.Mp; f(7) // like pt.Mp(7)
f := pt.Mv; f(7) // like (*pt).Mv(7) f := pt.Mv; f(7) // like (*pt).Mv(7)
f := t.Mp; f(7) // like (&t).Mp(7) f := t.Mp; f(7) // like (&amp;t).Mp(7)
f := makeT().Mp // invalid: result of makeT() is not addressable f := makeT().Mp // invalid: result of makeT() is not addressable
</pre> </pre>
...@@ -5715,19 +5715,23 @@ in unspecified order. ...@@ -5715,19 +5715,23 @@ in unspecified order.
</p> </p>
<p> <p>
Within a package, package-level variables are initialized, Within a package, package-level variables are initialized,
and constant values are determined, in and constant values are determined, according to
data-dependent order: if the initializer of <code>A</code> order of reference: if the initializer of <code>A</code>
depends on the value of <code>B</code>, <code>A</code> depends on <code>B</code>, <code>A</code>
will be set after <code>B</code>. will be set after <code>B</code>.
It is an error if such dependencies form a cycle. Dependency analysis does not depend on the actual values
Dependency analysis is done lexically: <code>A</code> of the items being initialized, only on their appearance
in the source.
<code>A</code>
depends on <code>B</code> if the value of <code>A</code> depends on <code>B</code> if the value of <code>A</code>
contains a mention of <code>B</code>, contains a value contains a mention of <code>B</code>, contains a value
whose initializer whose initializer
mentions <code>B</code>, or mentions a function that mentions <code>B</code>, or mentions a function that
mentions <code>B</code>, recursively. mentions <code>B</code>, recursively.
It is an error if such dependencies form a cycle.
If two items are not interdependent, they will be initialized If two items are not interdependent, they will be initialized
in the order they appear in the source. in the order they appear in the source, possibly in multiple files,
as presented to the compiler.
Since the dependency analysis is done per package, it can produce Since the dependency analysis is done per package, it can produce
unspecified results if <code>A</code>'s initializer calls a function defined unspecified results if <code>A</code>'s initializer calls a function defined
in another package that refers to <code>B</code>. in another package that refers to <code>B</code>.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment