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 #511 #512

Merged
merged 7 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 6 additions & 9 deletions src/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -541,9 +541,7 @@
"""
replace_returns(e) = e
function replace_returns(e::Expr)
if isfuncdef(e)
return e
end
isfuncdef(e) && return e

if Meta.isexpr(e, :return)
# We capture the original return-value in `retval` and return
Expand All @@ -554,19 +552,18 @@
# and is not our intent).
@gensym retval
return quote
$retval = $(e.args...)
$retval = $(map(replace_returns, e.args)...)
return $retval, __varinfo__
end
end

return Expr(e.head, map(replace_returns, e.args)...)
end

# If it's just a symbol, e.g. `f(x) = 1`, then we make it `f(x) = return 1`.
make_returns_explicit!(body) = Expr(:return, body)
function make_returns_explicit!(body::Expr)
add_return_to_last_statment!(body) = Expr(:return, body)

Check warning on line 563 in src/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/compiler.jl#L563

Added line #L563 was not covered by tests
torfjelde marked this conversation as resolved.
Show resolved Hide resolved
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.
# Otherwise we replace the last statement with an altered `return`.
torfjelde marked this conversation as resolved.
Show resolved Hide resolved
if !Meta.isexpr(body.args[end], :return)
body.args[end] = Expr(:return, body.args[end])
end
Expand Down Expand Up @@ -626,7 +623,7 @@
# See the docstrings of `replace_returns` for more info.
evaluatordef[:body] = MacroTools.@q begin
$(linenumbernode)
$(replace_returns(make_returns_explicit!(modeldef[:body])))
$(replace_returns(add_return_to_last_statment!(modeldef[:body])))
end

## Build the model function.
Expand Down
27 changes: 27 additions & 0 deletions test/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,33 @@ end
# With assignment.
@model outer() = @submodel x = inner()
@test outer()() isa Real

# Edge-cases.
# `return` in the last statement.
# Ref: issue #511.
@model function demo_ret_in_last_stmt(x::Bool)
# Two different values not supporting `iterate`.
if x
return Val(1)
else
return Val(2)
end
end

model_true = demo_ret_in_last_stmt(true)
@test model_true() === Val(1)

model_false = demo_ret_in_last_stmt(false)
@test model_false() === Val(2)

# `return` with `return`
@model function demo_ret_with_ret()
return begin
return Val(1)
Val(2)
end
end
@test demo_ret_with_ret()() === Val(1)
end

@testset "issue #368: hasmissing dispatch" begin
Expand Down
Loading