Skip to content

Commit d73f478

Browse files
authored
Allow assignment of nested non-closure procs to globals (#25154)
For memory-safety, this only seems problematic in case of closures, so I just special cased that. Fixes #25131
1 parent f90951c commit d73f478

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

compiler/semstmts.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ template isLocalSym(sym: PSym): bool =
725725
sym.typ.kind == tyTypeDesc or
726726
sfCompileTime in sym.flags) or
727727
sym.kind in {skProc, skFunc, skIterator} and
728-
sfGlobal notin sym.flags
728+
sfGlobal notin sym.flags and sym.typ.callConv == ccClosure
729729

730730
proc usesLocalVar(n: PNode): bool =
731731
case n.kind

tests/global/tglobalclosure.nim

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
discard """
2+
errormsg: "cannot assign local to global variable"
3+
line: 11
4+
"""
5+
6+
proc main() =
7+
var x = "hi"
8+
proc p() =
9+
echo x
10+
11+
let a {.global.} = p
12+
p()
13+
14+
main()

tests/global/tglobalclosure2.nim

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
discard """
2+
errormsg: "cannot assign local to global variable"
3+
line: 14
4+
"""
5+
6+
type X = object
7+
p: proc() {.closure.}
8+
9+
proc main() =
10+
var x = "hi"
11+
proc p() =
12+
echo x
13+
14+
let a {.global.} = X(p: p)
15+
a.p()
16+
17+
main()

tests/global/tglobalproc.nim

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
discard """
2+
output: "hi\nhi"
3+
"""
4+
5+
type X = object
6+
p: proc() {.nimcall.}
7+
8+
proc main() =
9+
proc p() =
10+
echo "hi"
11+
12+
let a {.global.} = p
13+
let b {.global.} = X(p: p)
14+
a()
15+
b.p()
16+
17+
main()

0 commit comments

Comments
 (0)