@@ -561,14 +561,15 @@ function replace_returns(e::Expr)
561
561
end
562
562
563
563
# If it's just a symbol, e.g. `f(x) = 1`, then we make it `f(x) = return 1`.
564
- add_return_to_last_statment! (body) = Expr (:return , body)
565
- function add_return_to_last_statment! (body:: Expr )
564
+ add_return_to_last_statment (body) = Expr (:return , body)
565
+ function add_return_to_last_statment (body:: Expr )
566
566
# If the last statement is a return-statement, we don't do anything.
567
567
# Otherwise we replace the last statement with a `return` statement.
568
- if ! Meta. isexpr (body. args[end ], :return )
569
- body. args[end ] = Expr (:return , body. args[end ])
570
- end
571
- return body
568
+ Meta. isexpr (body. args[end ], :return ) && return body
569
+ # We need to copy the arguments since we are modifying them.
570
+ new_args = copy (body. args)
571
+ new_args[end ] = Expr (:return , body. args[end ])
572
+ return Expr (body. head, new_args... )
572
573
end
573
574
574
575
const FloatOrArrayType = Type{<: Union{AbstractFloat,AbstractArray} }
@@ -602,7 +603,7 @@ function build_output(modeldef, linenumbernode)
602
603
kwargs = modeldef[:kwargs ]
603
604
604
605
# # Build the anonymous evaluator from the user-provided model definition.
605
- evaluatordef = deepcopy (modeldef)
606
+ evaluatordef = copy (modeldef)
606
607
607
608
# Add the internal arguments to the user-specified arguments (positional + keywords).
608
609
evaluatordef[:args ] = vcat (
@@ -624,7 +625,7 @@ function build_output(modeldef, linenumbernode)
624
625
# See the docstrings of `replace_returns` for more info.
625
626
evaluatordef[:body ] = MacroTools. @q begin
626
627
$ (linenumbernode)
627
- $ (replace_returns (add_return_to_last_statment! (modeldef[:body ])))
628
+ $ (replace_returns (add_return_to_last_statment (modeldef[:body ])))
628
629
end
629
630
630
631
# # Build the model function.
0 commit comments