proper error for calling nil closure in VM (#24059)

fixes #24057

Instead of crashing the compiler, the VM now gives a stacktrace if a nil
closure is attempted to be called.
This commit is contained in:
metagn
2024-09-04 10:13:04 +03:00
committed by GitHub
parent c948ab9b85
commit f69809bb17
3 changed files with 35 additions and 0 deletions

View File

@@ -1378,7 +1378,11 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
let rb = instr.regB
let rc = instr.regC
let bb = regs[rb].node
if bb.kind == nkNilLit:
stackTrace(c, tos, pc, "attempt to call nil closure")
let isClosure = bb.kind == nkTupleConstr
if isClosure and bb[0].kind == nkNilLit:
stackTrace(c, tos, pc, "attempt to call nil closure")
let prc = if not isClosure: bb.sym else: bb[0].sym
if prc.offset < -1:
# it's a callback:

View File

@@ -0,0 +1,8 @@
discard """
errormsg: "attempt to call nil closure"
line: 8
"""
static:
let x: proc () = nil
x()

View File

@@ -0,0 +1,23 @@
discard """
action: reject
nimout: '''
stack trace: (most recent call last)
tnilclosurecallstacktrace.nim(23, 6) tnilclosurecallstacktrace
tnilclosurecallstacktrace.nim(20, 6) baz
tnilclosurecallstacktrace.nim(17, 6) bar
tnilclosurecallstacktrace.nim(14, 4) foo
tnilclosurecallstacktrace.nim(14, 4) Error: attempt to call nil closure
'''
"""
proc foo(x: proc ()) =
x()
proc bar(x: proc ()) =
foo(x)
proc baz(x: proc ()) =
bar(x)
static:
baz(nil)