janet-lang / janet

A dynamic language and bytecode vm
https://janet-lang.org
MIT License
3.38k stars 217 forks source link

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

Closed amano-kenji closed 4 months ago

amano-kenji commented 4 months ago
(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.

sogaiu commented 4 months ago

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

bakpakin commented 4 months ago

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.

sogaiu commented 4 months ago

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?

amano-kenji commented 4 months ago

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