mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-04 12:07:51 +00:00
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:
@@ -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
|
||||
|
||||
14
tests/global/tglobalclosure.nim
Normal file
14
tests/global/tglobalclosure.nim
Normal 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()
|
||||
17
tests/global/tglobalclosure2.nim
Normal file
17
tests/global/tglobalclosure2.nim
Normal 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()
|
||||
17
tests/global/tglobalproc.nim
Normal file
17
tests/global/tglobalproc.nim
Normal 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()
|
||||
Reference in New Issue
Block a user