mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-05 04:27:44 +00:00
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:
@@ -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:
|
||||
|
||||
8
tests/vm/tnilclosurecall.nim
Normal file
8
tests/vm/tnilclosurecall.nim
Normal file
@@ -0,0 +1,8 @@
|
||||
discard """
|
||||
errormsg: "attempt to call nil closure"
|
||||
line: 8
|
||||
"""
|
||||
|
||||
static:
|
||||
let x: proc () = nil
|
||||
x()
|
||||
23
tests/vm/tnilclosurecallstacktrace.nim
Normal file
23
tests/vm/tnilclosurecallstacktrace.nim
Normal 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)
|
||||
Reference in New Issue
Block a user