From ef2a8da42c986d3c1c078479f42d5a568a1c3647 Mon Sep 17 00:00:00 2001
From: Robert Griesemer
+Within a package, package-level variables are initialized in
+declaration order but after any of the variables
+they depend on.
+
+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.
+Initialization proceeds by repeatedly initializing the next package-level
+variable that is earliest in declaration order and ready for initialization,
+until there are no variables ready for initialization.
+
-Within a package, package-level variables are initialized according
-to their dependencies: if a variable
+The declaration order of variables declared in multiple files is determined
+by the order in which the files are presented to the compiler: Variables
+declared in the first file are declared before any of the variables declared
+in the second file, and so on.
Dependency analysis does not rely on the actual values of the
variables, only on lexical references to them in the source,
-analyzed transitively. For instance, a variable Package initialization
+
+x
depends on
-a variable y
, x
will be initialized after
-y
.
+If any variables are still uninitialized when this
+process ends, those variables are part of one or more initialization cycles,
+and the program is not valid.
+x
's
-initialization expression
-may refer to a function whose body refers to variable y
;
-if so, x
depends on y
.
+analyzed transitively. For instance, if a variable x
's
+initialization expression refers to a function whose body refers to
+variable y
then x
depends on y
.
Specifically:
y
.
Dependency analysis is performed per package; only references referring
to variables, functions, and methods declared in the current package
are considered.
-It is an error if variable dependencies form a cycle
-(but dependency cycles containing no variables are permitted).
-If two variables are independent of each other,
-they are initialized in the order they are declared
-in the source, possibly in multiple files, as presented to the compiler.
@@ -6000,8 +6017,6 @@ func f() int {
the initialization order is d
, b
, c
, a
.
-Since b
and c
are independent of each other, they are
-initialized in declaration order (b
before c
).
@@ -6044,6 +6059,12 @@ the init
functions: it will not invoke the next one
until the previous one has returned.
+To ensure reproducible initialization behavior, build systems are encouraged +to present multiple files belonging to the same package in lexical file name +order to a compiler. +
+-- cgit v1.2.1