Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #537 #538

Merged
merged 3 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DynamicPPL"
uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8"
version = "0.23.17"
version = "0.23.18"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand Down
17 changes: 9 additions & 8 deletions src/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -561,14 +561,15 @@ function replace_returns(e::Expr)
end

# If it's just a symbol, e.g. `f(x) = 1`, then we make it `f(x) = return 1`.
add_return_to_last_statment!(body) = Expr(:return, body)
function add_return_to_last_statment!(body::Expr)
add_return_to_last_statment(body) = Expr(:return, body)
function add_return_to_last_statment(body::Expr)
# If the last statement is a return-statement, we don't do anything.
# Otherwise we replace the last statement with a `return` statement.
if !Meta.isexpr(body.args[end], :return)
body.args[end] = Expr(:return, body.args[end])
end
return body
Meta.isexpr(body.args[end], :return) && return body
# We need to copy the arguments since we are modifying them.
new_args = copy(body.args)
torfjelde marked this conversation as resolved.
Show resolved Hide resolved
new_args[end] = Expr(:return, body.args[end])
return Expr(body.head, new_args...)
end

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

## Build the anonymous evaluator from the user-provided model definition.
evaluatordef = deepcopy(modeldef)
evaluatordef = copy(modeldef)

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

## Build the model function.
Expand Down
14 changes: 14 additions & 0 deletions test/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ struct MyCoolStruct{T}
a::T
end

module Issue537 end

@testset "compiler.jl" begin
@testset "model macro" begin
@model function testmodel_comp(x, y)
Expand Down Expand Up @@ -675,4 +677,16 @@ end
res = f_splat_test_2(1)()
@test res == (1, (), 1, Int, NamedTuple())
end

@testset "issue #537: model with logging" begin
# Make sure `Module` is valid to put in a model.
@model demo_with_module() = Issue537
model = demo_with_module()
@test model() === Issue537

# And one explicit test for logging so know that is working.
@model demo_with_logging() = @info "hi"
model = demo_with_logging()
@test model() == nothing
end
end