Skip to content

Commit eb6e7c5

Browse files
authored
Merge pull request #263 from ReactiveBayes/261-it-is-not-possible-to-instantiate-an-empty-array-in-the-model-macro
Fix walk into created by guard to not fail on empty expressions
2 parents a630f63 + ca861a4 commit eb6e7c5

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/model_macro.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ struct walk_until_occurrence{E}
1919
end
2020

2121
not_enter_indexed_walk = guarded_walk((x) -> (x isa Expr && x.head == :ref) || (x isa Expr && x.head == :call && x.args[1] == :new))
22+
not_created_by = guarded_walk((x) -> (x isa Expr && !isempty(x.args) && x.args[1] == :created_by))
2223

2324
function (w::walk_until_occurrence{E})(f, x) where {E <: Tuple}
2425
return walk(x, z -> any(pattern -> @capture(x, $(pattern)), w.patterns) ? z : w(f, z), f)
@@ -265,7 +266,7 @@ function convert_to_kwargs_expression(e::Expr)
265266
end
266267

267268
# This is necessary to ensure that we don't change the `created_by` option as well.
268-
what_walk(::typeof(convert_to_kwargs_expression)) = guarded_walk((x) -> (x isa Expr && x.args[1] == :created_by))
269+
what_walk(::typeof(convert_to_kwargs_expression)) = not_created_by
269270

270271
"""
271272
convert_to_anonymous(e::Expr, created_by)
@@ -357,7 +358,7 @@ function add_get_or_create_expression(e::Expr)
357358
return e
358359
end
359360

360-
what_walk(::typeof(add_get_or_create_expression)) = guarded_walk((x) -> (x isa Expr && x.args[1] == :created_by))
361+
what_walk(::typeof(add_get_or_create_expression)) = not_created_by
361362

362363
"""
363364
generate_get_or_create(s::Symbol, lhs::Symbol, index::Nothing)
@@ -626,7 +627,7 @@ function convert_tilde_expression(e::Expr)
626627
end
627628
end
628629

629-
what_walk(::typeof(convert_tilde_expression)) = guarded_walk((x) -> (x isa Expr && x.args[1] == :created_by))
630+
what_walk(::typeof(convert_tilde_expression)) = not_created_by
630631

631632
"""
632633
options_vector_to_named_tuple(options::AbstractArray)

test/graph_construction_tests.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,3 +2003,18 @@ end
20032003
@test length(collect(filter(as_variable(:y), model))) == 1
20042004
@test length(collect(filter(as_variable(:x), model))) == 1
20052005
end
2006+
2007+
@testitem "Create empty array" begin
2008+
using Distributions
2009+
using GraphPPL
2010+
import GraphPPL: @model, create_model, datalabel, NodeCreationOptions, neighbors
2011+
2012+
@model function empty_array_model()
2013+
x = []
2014+
end
2015+
2016+
model = create_model(empty_array_model()) do model, ctx
2017+
return (;)
2018+
end
2019+
@test model isa GraphPPL.Model
2020+
end

0 commit comments

Comments
 (0)