Merge pull request #4963 from jangko/macro_error_improvement
Macro.error improvement fixes #4915
This commit is contained in:
commit
cacbf8b32c
5 changed files with 28 additions and 9 deletions
|
|
@ -76,12 +76,13 @@ proc stackTraceAux(c: PCtx; x: PStackFrame; pc: int; recursionLimit=100) =
|
||||||
msgWriteln(s)
|
msgWriteln(s)
|
||||||
|
|
||||||
proc stackTrace(c: PCtx, tos: PStackFrame, pc: int,
|
proc stackTrace(c: PCtx, tos: PStackFrame, pc: int,
|
||||||
msg: TMsgKind, arg = "") =
|
msg: TMsgKind, arg = "", n: PNode = nil) =
|
||||||
msgWriteln("stack trace: (most recent call last)")
|
msgWriteln("stack trace: (most recent call last)")
|
||||||
stackTraceAux(c, tos, pc)
|
stackTraceAux(c, tos, pc)
|
||||||
# XXX test if we want 'globalError' for every mode
|
# XXX test if we want 'globalError' for every mode
|
||||||
if c.mode == emRepl: globalError(c.debug[pc], msg, arg)
|
let lineInfo = if n == nil: c.debug[pc] else: n.info
|
||||||
else: localError(c.debug[pc], msg, arg)
|
if c.mode == emRepl: globalError(lineInfo, msg, arg)
|
||||||
|
else: localError(lineInfo, msg, arg)
|
||||||
|
|
||||||
proc bailOut(c: PCtx; tos: PStackFrame) =
|
proc bailOut(c: PCtx; tos: PStackFrame) =
|
||||||
stackTrace(c, tos, c.exceptionInstr, errUnhandledExceptionX,
|
stackTrace(c, tos, c.exceptionInstr, errUnhandledExceptionX,
|
||||||
|
|
@ -1245,7 +1246,10 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
|
||||||
regs[rc].node.strVal, regs[rd].node.strVal,
|
regs[rc].node.strVal, regs[rd].node.strVal,
|
||||||
c.debug[pc])
|
c.debug[pc])
|
||||||
of opcNError:
|
of opcNError:
|
||||||
stackTrace(c, tos, pc, errUser, regs[ra].node.strVal)
|
decodeB(rkNode)
|
||||||
|
let a = regs[ra].node
|
||||||
|
let b = regs[rb].node
|
||||||
|
stackTrace(c, tos, pc, errUser, a.strVal, if b.kind == nkNilLit: nil else: b)
|
||||||
of opcNWarning:
|
of opcNWarning:
|
||||||
message(c.debug[pc], warnUser, regs[ra].node.strVal)
|
message(c.debug[pc], warnUser, regs[ra].node.strVal)
|
||||||
of opcNHint:
|
of opcNHint:
|
||||||
|
|
|
||||||
|
|
@ -1068,7 +1068,7 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest; m: TMagic) =
|
||||||
else:
|
else:
|
||||||
# setter
|
# setter
|
||||||
unused(n, dest)
|
unused(n, dest)
|
||||||
genUnaryStmt(c, n, opcNError)
|
genBinaryStmt(c, n, opcNError)
|
||||||
of mNCallSite:
|
of mNCallSite:
|
||||||
if dest < 0: dest = c.getTemp(n.typ)
|
if dest < 0: dest = c.getTemp(n.typ)
|
||||||
c.gABC(n, opcCallSite, dest)
|
c.gABC(n, opcCallSite, dest)
|
||||||
|
|
|
||||||
|
|
@ -235,7 +235,7 @@ proc getImpl*(s: NimSym): NimNode {.magic: "GetImpl", noSideEffect.} =
|
||||||
## const.
|
## const.
|
||||||
discard
|
discard
|
||||||
|
|
||||||
proc error*(msg: string) {.magic: "NError", benign.}
|
proc error*(msg: string, n: NimNode = nil) {.magic: "NError", benign.}
|
||||||
## writes an error message at compile time
|
## writes an error message at compile time
|
||||||
|
|
||||||
proc warning*(msg: string) {.magic: "NWarning", benign.}
|
proc warning*(msg: string) {.magic: "NWarning", benign.}
|
||||||
|
|
@ -377,19 +377,19 @@ proc expectKind*(n: NimNode, k: NimNodeKind) {.compileTime.} =
|
||||||
## checks that `n` is of kind `k`. If this is not the case,
|
## checks that `n` is of kind `k`. If this is not the case,
|
||||||
## compilation aborts with an error message. This is useful for writing
|
## compilation aborts with an error message. This is useful for writing
|
||||||
## macros that check the AST that is passed to them.
|
## macros that check the AST that is passed to them.
|
||||||
if n.kind != k: error("Expected a node of kind " & $k & ", got " & $n.kind)
|
if n.kind != k: error("Expected a node of kind " & $k & ", got " & $n.kind, n)
|
||||||
|
|
||||||
proc expectMinLen*(n: NimNode, min: int) {.compileTime.} =
|
proc expectMinLen*(n: NimNode, min: int) {.compileTime.} =
|
||||||
## checks that `n` has at least `min` children. If this is not the case,
|
## checks that `n` has at least `min` children. If this is not the case,
|
||||||
## compilation aborts with an error message. This is useful for writing
|
## compilation aborts with an error message. This is useful for writing
|
||||||
## macros that check its number of arguments.
|
## macros that check its number of arguments.
|
||||||
if n.len < min: error("macro expects a node with " & $min & " children")
|
if n.len < min: error("macro expects a node with " & $min & " children", n)
|
||||||
|
|
||||||
proc expectLen*(n: NimNode, len: int) {.compileTime.} =
|
proc expectLen*(n: NimNode, len: int) {.compileTime.} =
|
||||||
## checks that `n` has exactly `len` children. If this is not the case,
|
## checks that `n` has exactly `len` children. If this is not the case,
|
||||||
## compilation aborts with an error message. This is useful for writing
|
## compilation aborts with an error message. This is useful for writing
|
||||||
## macros that check its number of arguments.
|
## macros that check its number of arguments.
|
||||||
if n.len != len: error("macro expects a node with " & $len & " children")
|
if n.len != len: error("macro expects a node with " & $len & " children", n)
|
||||||
|
|
||||||
proc newTree*(kind: NimNodeKind,
|
proc newTree*(kind: NimNodeKind,
|
||||||
children: varargs[NimNode]): NimNode {.compileTime.} =
|
children: varargs[NimNode]): NimNode {.compileTime.} =
|
||||||
|
|
|
||||||
13
tests/clearmsg/tmacroerrorproc.nim
Normal file
13
tests/clearmsg/tmacroerrorproc.nim
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
discard """
|
||||||
|
file: "tmacroerrorproc.nim"
|
||||||
|
line: 13
|
||||||
|
errormsg: "Expected a node of kind nnkCharLit, got nnkCommand"
|
||||||
|
"""
|
||||||
|
# issue #4915
|
||||||
|
import macros
|
||||||
|
|
||||||
|
macro mixer(n: typed): untyped =
|
||||||
|
expectKind(n, nnkCharLit)
|
||||||
|
|
||||||
|
mixer:
|
||||||
|
echo "owh"
|
||||||
|
|
@ -31,6 +31,8 @@ Changes affecting backwards compatibility
|
||||||
Library Additions
|
Library Additions
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- Added new parameter to ``error`` proc of ``macro`` module to provide better
|
||||||
|
error message
|
||||||
|
|
||||||
Tool Additions
|
Tool Additions
|
||||||
--------------
|
--------------
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue