nim-lang / compilerdev

This repository contains a collection of documents about how to change/refactor the Nim compiler in order to make it faster, easier to maintain and have fewer bugs by a superior architecture and design. However, no every idea here will work out.
10 stars 3 forks source link

`nkComesFrom` seems deadcode (but used a lot); ok to remove? #11

Open timotheecour opened 4 years ago

timotheecour commented 4 years ago

@Araq while working on something else I noticed that nkComesFrom is deadcode (never set) even though it's used quite a bit, so removing it would declutter a bit.

proc wrapInComesFrom*(info: TLineInfo; sym: PSym; res: PNode): PNode =
  when true:
    result = res
    result.info = info
    if result.kind in {nkStmtList, nkStmtListExpr} and result.len > 0:
      result.lastSon.info = info
    when false:
      # this hack is required to
      var x = result
      while x.kind == nkStmtListExpr: x = x.lastSon
      if x.kind in nkCallKinds:
        for i in 1..<x.len:
          if x[i].kind in nkCallKinds:
            x[i].info = info
  else:
    result = newNodeI(nkStmtListExpr, info)
    var d = newNodeI(nkComesFrom, info)
    d.add newSymNode(sym, info)
    result.add d
    result.add res
    result.typ = res.typ

eg, this code is also dead because of that:

    if it.kind == nkComesFrom:
      if hasNimFrame and frameName == nil:
        inc p.labels
        frameName = "FR" & rope(p.labels) & "_"
        let theMacro = it[0].sym
        add p.s(cpsStmts), initFrameNoDebug(p, frameName,
           makeCString theMacro.name.s,
           quotedFilename(p.config, theMacro.info), it.info.line.int)

as well as initFrameNoDebug + deinitFrameNoDebug + some other code

Araq commented 4 years ago

The idea of ComesFrom is that template expansions do not lose one debug stack frame. As you can see, the implementation was never finished but I think the idea is a good one so I'd rather keep the dead code.

timotheecour commented 4 years ago

just to clarify: so the goal was indeed what i suspected, to treat template/macro calls as if they were function calls, ie:

template bar() = doAssert false proc fn=bar() fn()

with nim --stacktracetemplates would produce something like:

stacktrace:
main.nim(1) in bar
main.nim(2) in fn

?

if so, yes, it's a good idea (but i was thinking of a different implementation to achieve that)

if not, please clarify maybe with an example

Araq commented 4 years ago

Yes, exactly.