From f1ec0cc48b43b972710274be437f3d2ad71e40fc Mon Sep 17 00:00:00 2001 From: Ian Henry Date: Fri, 24 Oct 2025 21:25:44 -0700 Subject: [PATCH 1/2] fix try macro hygiene Allow re-using the same symbol for the fiber and error. This allows you to write code like (try (print "hi") ([_ _] (print "oh no"))), fixing a regression introduced in #1605. --- src/boot/boot.janet | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/boot/boot.janet b/src/boot/boot.janet index c929cb0e5..cc8f3fa0b 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -348,12 +348,15 @@ [body catch] (assert (and (not (empty? catch)) (indexed? (catch 0))) "the first element of `catch` must be a tuple or array") (let [[err fib] (catch 0) - r (or err (gensym)) - f (or fib (gensym))] + r (gensym) + f (gensym)] ~(let [,f (,fiber/new (fn :try [] ,body) :ie) ,r (,resume ,f)] (if (,= (,fiber/status ,f) :error) - (do ,;(tuple/slice catch 1)) + (do + ,(if err ~(def ,err ,r)) + ,(if fib ~(def ,fib ,f)) + ,;(tuple/slice catch 1)) ,r)))) (defmacro with-syms From 0c34033b72d506789e8d197b861538ba677f9ef5 Mon Sep 17 00:00:00 2001 From: Ian Henry Date: Fri, 24 Oct 2025 21:54:05 -0700 Subject: [PATCH 2/2] add some tests for the (try) macro --- test/suite-boot.janet | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/suite-boot.janet b/test/suite-boot.janet index 7532a9725..e94ea16a2 100644 --- a/test/suite-boot.janet +++ b/test/suite-boot.janet @@ -1023,4 +1023,11 @@ (assert (deep-not= @{:key1 "value1" [@"key2"] @"value2"} @{:key1 "value1" [@"key2"] @"value2"}) "deep= mutable keys") +# different try overloads +(assert (= (try (error :error) ([] :caught)) :caught)) +(assert (= (try (error :error) ([e] e)) :error)) +(assert (= (try (error :error) ([e fib] [e (fiber? fib)])) [:error true])) +# regression test for #1659 +(assert (= (try (error :error) ([_ _] :caught)) :caught)) + (end-suite)