Closed EvanKirshenbaum closed 8 months ago
This issue was referenced by the following commit before migration:
On second thought, it is probably safest to put it in MacroValue.apply()
, since it's only user-defined functions ("macros") that can have return
statements in them. Currently, this is defined as
class MacroValue(CallableValue):
def apply(self, args:Sequence[Any])->Delayed[Any]:
bindings = dict(zip(self.param_names, args))
local_env = self.static_env.new_child(bindings)
return self.body.evaluate(local_env)
It should (and appears to be) sufficient to change this to
class MacroValue(CallableValue):
def apply(self, args:Sequence[Any])->Delayed[Any]:
bindings = dict(zip(self.param_names, args))
local_env = self.static_env.new_child(bindings)
def unwrap_return(val: MaybeError[Any]) -> MaybeError[Any]:
return val.value if isinstance(val, MacroReturn) else val
return self.body.evaluate(local_env).transformed(unwrap_return)
Having done that, I can simply remove the catch_return()
logic from visitFunction_expr()
.
In
DMFCompiler.visitRet()
, the return value is processed asThe value is evaluated and on the final check, if it isn't an error (as checked by
error_check()
), it is wrapped in aMacroReturn
object, which is an error and so will propagate, causing the rest of the body to be ignored.This error is caught and unwrapped in
visitFunction_expr()
:This all works fine with
f(a,b,c)
syntax, but we don't have the same check with injuections, so if a function is injected asa : f
, wheref
returns a value, theMacroReturn
"error" will continue to be propagated, rather than being transformed into a value. This has three consequences:a : f : g
, where the result should be passed tog
, but won't be.The straightforward way to patch this is to put the
catch_return
logic withinvisitInjection_expr()
. The more robust way is to figure out how to put the logic into (probably)use_callable()
to ensure that anyMacroReturn
value is always unwrapped. (visitInjection_expr()
callsuse_function()
, which in turn callsuse_callable()
.)Migrated from internal repository. Originally created by @EvanKirshenbaum on Jun 09, 2023 at 1:44 PM PDT. Closed on Jun 09, 2023 at 2:57 PM PDT.