Make async stacktraces less verbose (#21091)

* Name iterators something human readable

Remove intermediate async procs from stacktraces

Clean async traceback message from reraises message

* Remove unused import/variable

* Fix failing tests

Don't add {.stackTrace: off.} to anonymous procs (They already don't appear in stacktrace)

* Fix failing tests in pragma category

Now check that the nim is a routine type first so we don't run into any assertion defects

* Hide stack trace pragma in docs and update doc tests

User doesn't need to know if something won't appear so this more becomes verbose noise

If this is a bad idea we can always add a `when defined(nimdoc)` switch so we don't add {.stackTrace: off.} to the Future[T] returning proc for docs
This commit is contained in:
Jake Leahy 2022-12-16 02:05:34 +11:00 • committed by GitHub
commit d88f46df38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 63 deletions

View file

@ -329,29 +329,21 @@ proc `$`*(stackTraceEntries: seq[StackTraceEntry]): string =
if leftLen > longestLeft:
longestLeft = leftLen
var indent = 2
# Format the entries.
for entry in entries:
let (filename, procname) = getFilenameProcname(entry)
if procname == "":
if entry.line == reraisedFromBegin:
result.add(spaces(indent) & "#[\n")
indent.inc(2)
elif entry.line == reraisedFromEnd:
indent.dec(2)
result.add(spaces(indent) & "]#\n")
continue
if procname == "" and entry.line == reraisedFromBegin:
break
let left = "$#($#)" % [filename, $entry.line]
result.add((spaces(indent) & "$#$# $#\n") % [
result.add((spaces(2) & "$# $#\n") % [
left,
spaces(longestLeft - left.len + 2),
procname
])
let hint = getHint(entry)
if hint.len > 0:
result.add(spaces(indent+2) & "## " & hint & "\n")
result.add(spaces(4) & "## " & hint & "\n")
proc injectStacktrace[T](future: Future[T]) =
when not defined(release):

View file

@ -20,9 +20,8 @@ proc newCallWithLineInfo(fromNode: NimNode; theProc: NimNode, args: varargs[NimN
template createCb(retFutureSym, iteratorNameSym,
strName, identName, futureVarCompletions: untyped) =
bind finished
var nameIterVar = iteratorNameSym
proc identName {.closure.} =
proc identName {.closure, stackTrace: off.} =
try:
if not nameIterVar.finished:
var next = nameIterVar()
@ -151,6 +150,10 @@ proc asyncSingleProc(prc: NimNode): NimNode =
result[0][0] = quote do: Future[void]
return result
if prc.kind in RoutineNodes and prc.name.kind != nnkEmpty:
# Only non anonymous functions need/can have stack trace disabled
prc.addPragma(nnkExprColonExpr.newTree(ident"stackTrace", ident"off"))
if prc.kind notin {nnkProcDef, nnkLambda, nnkMethodDef, nnkDo}:
error("Cannot transform this node kind into an async proc." &
" proc/method definition or lambda node expected.", prc)
@ -209,7 +212,7 @@ proc asyncSingleProc(prc: NimNode): NimNode =
# -> {.pop.}
# -> <proc_body>
# -> complete(retFuture, result)
var iteratorNameSym = genSym(nskIterator, $prcName & "Iter")
var iteratorNameSym = genSym(nskIterator, $prcName & " (Async)")
var procBody = prc.body.processBody(retFutureSym, futureVarIdents)
# don't do anything with forward bodies (empty)
if procBody.kind != nnkEmpty:
@ -251,9 +254,10 @@ proc asyncSingleProc(prc: NimNode): NimNode =
# friendlier stack traces:
var cbName = genSym(nskProc, prcName & NimAsyncContinueSuffix)
var procCb = getAst createCb(retFutureSym, iteratorNameSym,
newStrLitNode(prcName),
cbName,
createFutureVarCompletions(futureVarIdents, nil))
newStrLitNode(prcName),
cbName,
createFutureVarCompletions(futureVarIdents, nil)
)
outerProcBody.add procCb
# -> return retFuture