-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cgen: make
.global.
s work more according to spec
For the C target, globals defined inside procedures that are resource- like (i.e. have a user-defined or lifted destructor) are now initialized in a module's pre-init procedure, instead of each time control-flow reaches the definition. This mirrors what already happened for non-resource-like globals. In addition, globals defined in `inline` procedures no longer cause linking errors because of duplicated definitions in the generated C code. The globals are extracted before translating the AST to MIR code, making the workaround in `mirgen` (that didn't work anyway) obsolete. In order to work towards unifying the architecture of the back-ends, `vmbackend` now also makes use of the pre-extraction, no longer requiring extra logic in the code-generator. - the destructor injection pass is not run for the initializer expression of globals defined inside procedures - `.global.`s on the JS target still don't work according to specification
- Loading branch information
Showing
10 changed files
with
188 additions
and
43 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
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
29 changes: 29 additions & 0 deletions
29
tests/lang_objects/destructor/tdestructor_in_initializer.nim
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,29 @@ | ||
discard """ | ||
description: ''' | ||
Regression test to make sure that locals inside the initializer of a global | ||
defined inside a procedure's body are properly destroyed | ||
''' | ||
targets: "c" | ||
knownIssue: "the `injectdestructors` pass is not for the initializer" | ||
""" | ||
|
||
var wasDestroyed: bool | ||
# don't initialize the global; doing so might override the value assgined | ||
# during pre-initialization | ||
|
||
# initialization of ``.global.``s currently happens *before* the code part | ||
# of the module is executed, so if the destructor was called, ``wasDestroyed`` | ||
# has to be true here | ||
doAssert wasDestroyed | ||
|
||
type Resource = object | ||
|
||
proc `=destroy`(x: var Resource) = | ||
wasDestroyed = true | ||
|
||
proc prc() = | ||
var v {.global.} = block: | ||
var r = Resource() # `r` needs to be destroyed at the end of the block | ||
1 | ||
|
||
prc() # use the procedure so that the global is part of the alive code |