janet icon indicating copy to clipboard operation
janet copied to clipboard

false branch of `if-let` doesn't report the correct call stack information.

Open amano-kenji opened this issue 1 year ago • 1 comments

(defn recurse
  [branch]
  (if-let [ha branch]
    (do
      (ev/sleep 1)
      (debug/stacktrace (fiber/current) "print stack for true branch" "")
      (recurse branch))
    (do
      (ev/sleep 1)
      (debug/stacktrace (fiber/current) "print stack for false branch" "")
      (recurse branch))))

(defn main
  [& args]
  (def arg
    (case (get args 1 "true")
      "true" true
      "false" false
      nil))
  (recurse arg))

With this, debug/stacktrace in the false branch doesn't report its own line in the call stack, but the line where if-let resides.

debug/stacktrace in the true branch reports its own line in the call stack.

amano-kenji avatar Feb 17 '24 02:02 amano-kenji

For future readers, see this comment for example invocations along with output and some explanation.

sogaiu avatar Feb 17 '24 03:02 sogaiu

So the issue here is there is an explicit macro expansion here inside the if-let macro that doesn't preserve the source location information. We can fix this issue by making slight modifications to macex1 which does the explicit macro expansion. This should also fix source mapping information for any macros that explicitly expand sub forms manually, instead of the usual case of letting the be.

bakpakin avatar Feb 17 '24 19:02 bakpakin

I tried 7a2868c with the sample code.

Both invocations produced the expected output here:

$ janet recurse.janet true
alive: print stack for true branch
  in debug/stacktrace [src/core/debug.c] on line 388
  in recurse [recurse.janet] (tailcall) on line 6, column 7
...
$ janet recurse.janet false
alive: print stack for false branch
  in debug/stacktrace [src/core/debug.c] on line 388
  in recurse [recurse.janet] (tailcall) on line 10, column 7
...

@amano-kenji Does it work for you?

sogaiu avatar Feb 18 '24 02:02 sogaiu

I tested the latest commit, and the issue seems fixed.

amano-kenji avatar Feb 18 '24 02:02 amano-kenji