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

@model macro breaks on conditional branches with return values #511

Closed
bgroenks96 opened this issue Aug 4, 2023 · 2 comments
Closed

@model macro breaks on conditional branches with return values #511

bgroenks96 opened this issue Aug 4, 2023 · 2 comments

Comments

@bgroenks96
Copy link
Collaborator

As discussed with @torfjelde on Slack, here is a minimal example:

@model function demo()
    x ~ Normal(0,1)
    if true
       return x+1
    else
       return x-1
    end
end

@model function superdemo()
    @submodel x = demo()
end

which results in:

ERROR: BoundsError: attempt to access Float64 at index [2]
Stacktrace:
  [1] indexed_iterate(I::Float64, i::Int64, state::Nothing)
    @ Base ./tuple.jl:97
  [2] macro expansion
    @ ~/.julia/packages/DynamicPPL/oJMmE/src/submodel_macro.jl:228 [inlined]
  [3] macro expansion
    @ ~/.julia/packages/DynamicPPL/oJMmE/src/compiler.jl:487 [inlined]
...

In other cases, depending on what is returned, the error may be different.

@torfjelde torfjelde transferred this issue from TuringLang/Turing.jl Aug 4, 2023
@torfjelde
Copy link
Member

torfjelde commented Aug 4, 2023

Just to it's clear, this does not require usage of @submodel:

julia> @model function demo()
           if true
               return nothing
           else
               return 0
           end
       end
demo (generic function with 2 methods)

julia> demo()()
ERROR: MethodError: no method matching iterate(::Nothing)

Closest candidates are:
  iterate(::Union{LinRange, StepRangeLen})
   @ Base range.jl:880
  iterate(::Union{LinRange, StepRangeLen}, ::Integer)
   @ Base range.jl:880
  iterate(::T) where T<:Union{Base.KeySet{<:Any, <:Dict}, Base.ValueIterator{<:Dict}}
   @ Base dict.jl:698
  ...

Stacktrace:
 [1] first(itr::Nothing)
   @ Base ./abstractarray.jl:465
 [2] (::Model{typeof(demo), (), (), (), Tuple{}, Tuple{}, DefaultContext})()
   @ DynamicPPL /drive-2/Projects/public/DynamicPPL.jl/src/model.jl:860
 [3] top-level scope
   @ REPL[20]:1

torfjelde added a commit that referenced this issue Aug 4, 2023
@torfjelde torfjelde mentioned this issue Aug 4, 2023
github-merge-queue bot pushed a commit that referenced this issue Aug 4, 2023
* 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
@torfjelde
Copy link
Member

This should be fixed in #512 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants