Skip to content

Commit 93ca9dd

Browse files
authored
Try #316:
2 parents b8e448c + c67922d commit 93ca9dd

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

src/compiler.jl

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,7 @@ Builds the output expression.
470470
"""
471471
function build_output(modelinfo, linenumbernode)
472472
## Build the anonymous evaluator from the user-provided model definition.
473-
474-
# Remove the name.
475473
evaluatordef = deepcopy(modelinfo[:modeldef])
476-
delete!(evaluatordef, :name)
477474

478475
# Add the internal arguments to the user-specified arguments (positional + keywords).
479476
evaluatordef[:args] = vcat(
@@ -489,7 +486,13 @@ function build_output(modelinfo, linenumbernode)
489486
evaluatordef[:kwargs] = []
490487

491488
# Replace the user-provided function body with the version created by DynamicPPL.
492-
evaluatordef[:body] = modelinfo[:body]
489+
# We use `MacroTools.@q begin ... end` instead of regular `quote ... end` to ensure
490+
# that no new `LineNumberNode`s are added apart from the reference `linenumbernode`
491+
# to the call site
492+
evaluatordef[:body] = MacroTools.@q begin
493+
$(linenumbernode)
494+
$(modelinfo[:body])
495+
end
493496

494497
## Build the model function.
495498

@@ -498,24 +501,24 @@ function build_output(modelinfo, linenumbernode)
498501
defaults_namedtuple = modelinfo[:defaults_namedtuple]
499502

500503
# Update the function body of the user-specified model.
501-
# We use a name for the anonymous evaluator that does not conflict with other variables.
502-
modeldef = modelinfo[:modeldef]
503-
@gensym evaluator
504504
# We use `MacroTools.@q begin ... end` instead of regular `quote ... end` to ensure
505505
# that no new `LineNumberNode`s are added apart from the reference `linenumbernode`
506506
# to the call site
507+
modeldef = modelinfo[:modeldef]
507508
modeldef[:body] = MacroTools.@q begin
508509
$(linenumbernode)
509-
$evaluator = $(MacroTools.combinedef(evaluatordef))
510510
return $(DynamicPPL.Model)(
511511
$(QuoteNode(modeldef[:name])),
512-
$evaluator,
512+
$(modeldef[:name]),
513513
$allargs_namedtuple,
514514
$defaults_namedtuple,
515515
)
516516
end
517517

518-
return :($(Base).@__doc__ $(MacroTools.combinedef(modeldef)))
518+
return MacroTools.@q begin
519+
$(MacroTools.combinedef(evaluatordef))
520+
$(Base).@__doc__ $(MacroTools.combinedef(modeldef))
521+
end
519522
end
520523

521524
function warn_empty(body)

src/model.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ julia> @model function demo()
121121
x ~ Normal(m, 1)
122122
return (; m=m, x=x)
123123
end
124-
demo (generic function with 1 method)
124+
demo (generic function with 2 methods)
125125
126126
julia> model = demo();
127127
@@ -161,7 +161,7 @@ julia> @model function demo_mv(::Type{TV}=Float64) where {TV}
161161
m[2] ~ Normal()
162162
return m
163163
end
164-
demo_mv (generic function with 2 methods)
164+
demo_mv (generic function with 3 methods)
165165
166166
julia> model = demo_mv();
167167
@@ -192,13 +192,13 @@ the use of [`@submodel`](@ref).
192192
193193
```jldoctest condition
194194
julia> @model demo_inner() = m ~ Normal()
195-
demo_inner (generic function with 1 method)
195+
demo_inner (generic function with 2 methods)
196196
197197
julia> @model function demo_outer()
198198
m = @submodel demo_inner()
199199
return m
200200
end
201-
demo_outer (generic function with 1 method)
201+
demo_outer (generic function with 2 methods)
202202
203203
julia> model = demo_outer();
204204
@@ -218,7 +218,7 @@ julia> @model function demo_outer_prefix()
218218
m = @submodel inner demo_inner()
219219
return m
220220
end
221-
demo_outer_prefix (generic function with 1 method)
221+
demo_outer_prefix (generic function with 2 methods)
222222
223223
julia> # This doesn't work now!
224224
conditioned_model = demo_outer_prefix() | (m = 1.0, );
@@ -279,7 +279,7 @@ julia> @model function demo()
279279
x ~ Normal(m, 1)
280280
return (; m=m, x=x)
281281
end
282-
demo (generic function with 1 method)
282+
demo (generic function with 2 methods)
283283
284284
julia> conditioned_model = condition(demo(), m = 1.0, x = 10.0);
285285
@@ -333,7 +333,7 @@ julia> @model function demo()
333333
m ~ Normal()
334334
x ~ Normal(m, 1)
335335
end
336-
demo (generic function with 1 method)
336+
demo (generic function with 2 methods)
337337
338338
julia> m = demo();
339339
@@ -613,7 +613,7 @@ julia> @model function demo(xs)
613613
end
614614
return (m, )
615615
end
616-
demo (generic function with 1 method)
616+
demo (generic function with 2 methods)
617617
618618
julia> model = demo(randn(10));
619619

0 commit comments

Comments
 (0)