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
This commit is contained in:
bptato
2025-09-11 09:22:47 +02:00
committed by GitHub
parent f90951cc61
commit d73f478bdc
4 changed files with 49 additions and 1 deletions

View File

@@ -725,7 +725,7 @@ template isLocalSym(sym: PSym): bool =
sym.typ.kind == tyTypeDesc or
sfCompileTime in sym.flags) or
sym.kind in {skProc, skFunc, skIterator} and
sfGlobal notin sym.flags
sfGlobal notin sym.flags and sym.typ.callConv == ccClosure
proc usesLocalVar(n: PNode): bool =
case n.kind

View File

@@ -0,0 +1,14 @@
discard """
errormsg: "cannot assign local to global variable"
line: 11
"""
proc main() =
var x = "hi"
proc p() =
echo x
let a {.global.} = p
p()
main()

View File

@@ -0,0 +1,17 @@
discard """
errormsg: "cannot assign local to global variable"
line: 14
"""
type X = object
p: proc() {.closure.}
proc main() =
var x = "hi"
proc p() =
echo x
let a {.global.} = X(p: p)
a.p()
main()

View File

@@ -0,0 +1,17 @@
discard """
output: "hi\nhi"
"""
type X = object
p: proc() {.nimcall.}
proc main() =
proc p() =
echo "hi"
let a {.global.} = p
let b {.global.} = X(p: p)
a()
b.p()
main()