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

fix compiler crash with .global initializers #1472

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions compiler/mir/mirgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2388,16 +2388,18 @@ proc generateAssignment*(graph: ModuleGraph, env: var MirEnv,
## `builder`'s currently selected buffer.
assert n.kind == nkIdentDefs and n.len == 3
var c = initCtx(graph, config, nil, move env)
# treat the code as top-level code so that no 'def' is generated for
# assignments to globals
c.scopeDepth = 1

template swapState() =
swap(c.sp.map, source)
swap(c.builder, builder)

swapState()
genLocInit(c, n[0], n[2])
# treat the code as top-level code so that no 'def' is generated for
# assignments to globals
c.scope(true):
# a scope is required, otherwise locals/temporaries cannot be registered
# for destruction
genLocInit(c, n[0], n[2])
swapState()
env = move c.env # move back

Expand Down
20 changes: 20 additions & 0 deletions tests/global/twith_local_requiring_destruction.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
discard """
description: '''
Ensure that not explicitly scoped locals/temporaries in `.global`
intializer expressions are destroyed.
'''
targets: c js vm
output: "destroy: 1"
"""

type Object = object
val: int

proc `=destroy`(x: var Object) =
echo "destroy: ", x.val

proc test() =
# the local (`x`) has no explicit scope
var g {.global.} = (var x = Object(val: 1); x.val)

test()
Loading