Skip to content

A peer-to-peer stack for code collaboration

Fixes #191 in which catch did not correctly handle the scope of the handler

Luke Palmerecc097e4650d
diff --git a/rad/prelude.rad b/rad/prelude.rad
index f43cf14..70894d8 100644
--- a/rad/prelude.rad
+++ b/rad/prelude.rad
@@ -6,6 +6,7 @@
(load! "rad/prelude/dict.rad")
(load! "rad/prelude/ref.rad")
(load! "rad/prelude/lens.rad")
+(load! "rad/prelude/exception.rad")
(load! "rad/chain.rad")
(load! "rad/state-machine.rad")
@@ -18,6 +19,7 @@
"rad/prelude/dict.rad"
"rad/prelude/ref.rad"
"rad/prelude/lens.rad"
+ "rad/prelude/exception.rad"
"rad/state-machine.rad"
))
diff --git a/rad/prelude/exception.rad b/rad/prelude/exception.rad
new file mode 100644
index 0000000..f721782
--- /dev/null
+++ b/rad/prelude/exception.rad
@@ -0,0 +1,5 @@
+(should-be "catch handles scope correctly"
+ ;; Issue #191, catch used to accidentally use the lexical environment of
+ ;; the `throw` that generated the exception.
+ ((fn [body handler] (catch 'any (body) handler)) (fn [] (throw 'foo "foo")) (fn [e] #t))
+ #t)
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hs
index 359760f..8bc1bed 100644
--- a/src/Radicle/Internal/Core.hs
+++ b/src/Radicle/Internal/Core.hs
@@ -468,12 +468,13 @@ specialForms = Map.fromList $ first Ident <$>
, ("catch", \case
[l, form, handler] -> do
mlabel <- baseEval l
+ handlerclo <- baseEval handler
case mlabel of
-- TODO reify stack
Atom label -> baseEval form `catchError` \(LangError _stack e) -> do
(thrownLabel, thrownValue) <- errorDataToValue e
if thrownLabel == label || label == Ident "any"
- then handler $$ [thrownValue]
+ then handlerclo $$ [thrownValue]
else baseEval form
_ -> throwErrorHere $ TypeError "catch: first argument must be atom"
xs -> throwErrorHere $ WrongNumberOfArgs "catch" 3 (length xs))