From fba1fdabe4f8869da8058b767702d93b9db45747 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sat, 13 May 2023 09:44:30 -0500 Subject: [PATCH] Update short-fn to fix #1123 Symbols are renamed on expansion to avoid the issue. --- CHANGELOG.md | 2 ++ src/boot/boot.janet | 15 +++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0451db51..5d0bf7ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ All notable changes to this project will be documented in this file. ## Unreleased - ??? +- Various bug fixes +- Make nested short-fn's behave a bit more predictably (it is still not recommended to nest short-fns). - Add `os/strftime` for date formatting. - Fix `ev/select` on threaded channels sometimes live-locking. - Support the `NO_COLOR` environment variable to turn off VT100 color codes in repl (and in scripts). diff --git a/src/boot/boot.janet b/src/boot/boot.janet index c120abb0..0ccda788 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -2209,6 +2209,7 @@ (defn saw-special-arg [num] (set max-param-seen (max max-param-seen num))) + (def prefix (gensym)) (defn on-binding [x] (if (string/has-prefix? '$ x) @@ -2216,22 +2217,24 @@ (= '$ x) (do (saw-special-arg 0) - '$0) + (symbol prefix '$0)) (= '$& x) (do (set vararg true) - x) + (symbol prefix x)) :else (do (def num (scan-number (string/slice x 1))) (if (nat? num) - (saw-special-arg num)) - x)) + (do + (saw-special-arg num) + (symbol prefix x)) + x))) x)) (def expanded (macex arg on-binding)) (def name-splice (if name [name] [])) - (def fn-args (seq [i :range [0 (+ 1 max-param-seen)]] (symbol '$ i))) - ~(fn ,;name-splice [,;fn-args ,;(if vararg ['& '$&] [])] ,expanded)) + (def fn-args (seq [i :range [0 (+ 1 max-param-seen)]] (symbol prefix '$ i))) + ~(fn ,;name-splice [,;fn-args ,;(if vararg ['& (symbol prefix '$&)] [])] ,expanded)) ### ###