Skip to content

Commit

Permalink
Hotfixes after #309 (#351)
Browse files Browse the repository at this point in the history
#309 was merged a bit too soon, for example `bors` was never run on the final version due to some issues. Before we make a release, we should make sure that it all works properly.
  • Loading branch information
torfjelde committed Dec 14, 2021
1 parent 1744ba7 commit b60bd84
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
matrix:
version:
# - '1.3' # minimum supported version
- '1.3' # minimum supported version
- '1' # current stable version
os:
- ubuntu-latest
Expand Down
15 changes: 11 additions & 4 deletions src/submodel_macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,23 @@ function submodel(prefix_expr, expr, ctx=esc(:__context__))
if prefix_left !== :prefix
error("$(prefix_left) is not a valid kwarg")
end

# The user expects `@submodel ...` to return the
# return-value of the `...`, hence we need to capture
# the return-value and handle it correctly.
@gensym retval

# `prefix=false` => don't prefix, i.e. do nothing to `ctx`.
# `prefix=true` => automatically determine prefix.
# `prefix=...` => use it.
args_assign = getargs_assignment(expr)
return if args_assign === nothing
ctx = prefix_submodel_context(prefix, ctx)
# In this case we only want to get the `__varinfo__`.
quote
$(esc(:__varinfo__)) = last(
$(DynamicPPL._evaluate!!)($(esc(expr)), $(esc(:__varinfo__)), $(ctx))
$retval, $(esc(:__varinfo__)) = $(DynamicPPL._evaluate!!)(
$(esc(expr)), $(esc(:__varinfo__)), $(ctx)
)
$retval
end
else
L, R = args_assign
Expand All @@ -235,9 +241,10 @@ function submodel(prefix_expr, expr, ctx=esc(:__context__))
)
end
quote
$(esc(L)), $(esc(:__varinfo__)) = $(DynamicPPL._evaluate!!)(
$retval, $(esc(:__varinfo__)) = $(DynamicPPL._evaluate!!)(
$(esc(R)), $(esc(:__varinfo__)), $(ctx)
)
$(esc(L)) = $retval
end
end
end
10 changes: 10 additions & 0 deletions test/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -570,5 +570,15 @@ end

@model demo() = x ~ Normal()
retval, svi = DynamicPPL.evaluate!!(demo(), SimpleVarInfo(), SamplingContext())

# Return-value when using `@submodel`
@model inner() = x ~ Normal()
# Without assignment.
@model outer() = @submodel inner()
@test outer()() isa Real

# With assignment.
@model outer() = @submodel x = inner()
@test outer()() isa Real
end
end

0 comments on commit b60bd84

Please sign in to comment.