Skip to content

Commit aa13dcf

Browse files
authored
Fix for #511 (#512)
* fix for #511 * revert to previous behavior but with recursive application of replace_returns in return statements * use Val in the tests for return-values rather than something like missing and nothing * Update src/compiler.jl * Update src/compiler.jl * bumped patch version
1 parent 5dd7c53 commit aa13dcf

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
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.9"
3+
version = "0.23.10"
44

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

src/compiler.jl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -541,9 +541,7 @@ definitions. This is checked using [`isfuncdef`](@ref).
541541
"""
542542
replace_returns(e) = e
543543
function replace_returns(e::Expr)
544-
if isfuncdef(e)
545-
return e
546-
end
544+
isfuncdef(e) && return e
547545

548546
if Meta.isexpr(e, :return)
549547
# We capture the original return-value in `retval` and return
@@ -554,7 +552,7 @@ function replace_returns(e::Expr)
554552
# and is not our intent).
555553
@gensym retval
556554
return quote
557-
$retval = $(e.args...)
555+
$retval = $(map(replace_returns, e.args)...)
558556
return $retval, __varinfo__
559557
end
560558
end
@@ -563,8 +561,8 @@ function replace_returns(e::Expr)
563561
end
564562

565563
# If it's just a symbol, e.g. `f(x) = 1`, then we make it `f(x) = return 1`.
566-
make_returns_explicit!(body) = Expr(:return, body)
567-
function make_returns_explicit!(body::Expr)
564+
add_return_to_last_statment!(body) = Expr(:return, body)
565+
function add_return_to_last_statment!(body::Expr)
568566
# If the last statement is a return-statement, we don't do anything.
569567
# Otherwise we replace the last statement with a `return` statement.
570568
if !Meta.isexpr(body.args[end], :return)
@@ -626,7 +624,7 @@ function build_output(modeldef, linenumbernode)
626624
# See the docstrings of `replace_returns` for more info.
627625
evaluatordef[:body] = MacroTools.@q begin
628626
$(linenumbernode)
629-
$(replace_returns(make_returns_explicit!(modeldef[:body])))
627+
$(replace_returns(add_return_to_last_statment!(modeldef[:body])))
630628
end
631629

632630
## Build the model function.

test/compiler.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,33 @@ end
599599
# With assignment.
600600
@model outer() = @submodel x = inner()
601601
@test outer()() isa Real
602+
603+
# Edge-cases.
604+
# `return` in the last statement.
605+
# Ref: issue #511.
606+
@model function demo_ret_in_last_stmt(x::Bool)
607+
# Two different values not supporting `iterate`.
608+
if x
609+
return Val(1)
610+
else
611+
return Val(2)
612+
end
613+
end
614+
615+
model_true = demo_ret_in_last_stmt(true)
616+
@test model_true() === Val(1)
617+
618+
model_false = demo_ret_in_last_stmt(false)
619+
@test model_false() === Val(2)
620+
621+
# `return` with `return`
622+
@model function demo_ret_with_ret()
623+
return begin
624+
return Val(1)
625+
Val(2)
626+
end
627+
end
628+
@test demo_ret_with_ret()() === Val(1)
602629
end
603630

604631
@testset "issue #368: hasmissing dispatch" begin

0 commit comments

Comments
 (0)