-
-
Notifications
You must be signed in to change notification settings - Fork 606
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
153 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
Added `@standalone` for module constructors | ||
|
||
When two modules import each other and both have module constructors, | ||
druntime would throw an error because it can't determine which to run first. | ||
|
||
This could be circumvented by using `pragma(crt_constructor)` instead, but in C runtime constructors, druntime isn't initialized. | ||
Therefore the Garbage Collector can't be used in such constructors. | ||
|
||
`@standalone` is a new attribute that can be used to mark module constructors that run after druntime has been initialized, | ||
but do not depend on any other module constructors being run before it, so it will not cause a cyclic dependency error. | ||
It must be imported from `core.attribute`. | ||
|
||
The compiler doesn't verify that the module constructor truly doesn't depend on other variables being initialized, so it must be enforced manually. | ||
Because of this, they must be marked `@system` or `@trusted`. | ||
|
||
--- | ||
import core.attribute : standalone; | ||
|
||
immutable int* x; | ||
|
||
@standalone @system shared static this() | ||
{ | ||
x = new int(10); | ||
} | ||
|
||
void main() | ||
{ | ||
assert(*x == 10); | ||
} | ||
--- | ||
|
||
If possible, prefer to solve cyclic dependency errors by putting the offending module constructors into their own smaller modules instead of using `@standalone`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
TEST_OUTPUT: | ||
--- | ||
fail_compilation/standalone_modctor.d(11): Error: `@standalone` can only be used on shared static constructors | ||
fail_compilation/standalone_modctor.d(12): Error: a module constructor using `@standalone` must be `@system` or `@trusted` | ||
fail_compilation/standalone_modctor.d(13): Error: a module constructor using `@standalone` must be `@system` or `@trusted` | ||
--- | ||
*/ | ||
import core.attribute : standalone; | ||
|
||
@standalone static this() {} | ||
@standalone shared static this() {} | ||
@standalone shared static this() @safe {} | ||
@standalone shared static this() @trusted {} | ||
@standalone shared static this() @system {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module standalone_b; | ||
|
||
import standalone_modctor; | ||
import core.attribute : standalone; | ||
|
||
immutable int* y; | ||
|
||
@standalone @system shared static this() | ||
{ | ||
y = new int(2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// REQUIRED_ARGS: -Irunnable/imports | ||
// EXTRA_SOURCES: imports/standalone_b.d | ||
// PERMUTE_ARGS: -cov | ||
|
||
import standalone_b; | ||
import core.attribute : standalone; | ||
|
||
immutable int* x; | ||
|
||
@standalone @system shared static this() | ||
{ | ||
x = new int(1); | ||
} | ||
|
||
void main() | ||
{ | ||
assert(*x == 1); | ||
assert(*y == 2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters