Skip to content

Commit 4788480

Browse files
authored
Fix for #537 (#538)
* add_return_to_statement is no longer mutating, thus fixing #537 * bumped patch version * formatting
1 parent ffe9272 commit 4788480

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "DynamicPPL"
22
uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8"
3-
version = "0.23.17"
3+
version = "0.23.18"
44

55
[deps]
66
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"

src/compiler.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -561,14 +561,15 @@ function replace_returns(e::Expr)
561561
end
562562

563563
# 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)
566566
# If the last statement is a return-statement, we don't do anything.
567567
# 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...)
572573
end
573574

574575
const FloatOrArrayType = Type{<:Union{AbstractFloat,AbstractArray}}
@@ -602,7 +603,7 @@ function build_output(modeldef, linenumbernode)
602603
kwargs = modeldef[:kwargs]
603604

604605
## Build the anonymous evaluator from the user-provided model definition.
605-
evaluatordef = deepcopy(modeldef)
606+
evaluatordef = copy(modeldef)
606607

607608
# Add the internal arguments to the user-specified arguments (positional + keywords).
608609
evaluatordef[:args] = vcat(
@@ -624,7 +625,7 @@ function build_output(modeldef, linenumbernode)
624625
# See the docstrings of `replace_returns` for more info.
625626
evaluatordef[:body] = MacroTools.@q begin
626627
$(linenumbernode)
627-
$(replace_returns(add_return_to_last_statment!(modeldef[:body])))
628+
$(replace_returns(add_return_to_last_statment(modeldef[:body])))
628629
end
629630

630631
## Build the model function.

test/compiler.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ struct MyCoolStruct{T}
3333
a::T
3434
end
3535

36+
module Issue537 end
37+
3638
@testset "compiler.jl" begin
3739
@testset "model macro" begin
3840
@model function testmodel_comp(x, y)
@@ -675,4 +677,16 @@ end
675677
res = f_splat_test_2(1)()
676678
@test res == (1, (), 1, Int, NamedTuple())
677679
end
680+
681+
@testset "issue #537: model with logging" begin
682+
# Make sure `Module` is valid to put in a model.
683+
@model demo_with_module() = Issue537
684+
model = demo_with_module()
685+
@test model() === Issue537
686+
687+
# And one explicit test for logging so know that is working.
688+
@model demo_with_logging() = @info "hi"
689+
model = demo_with_logging()
690+
@test model() == nothing
691+
end
678692
end

0 commit comments

Comments
 (0)