Skip to content

Commit c95ccfa

Browse files
Better error message for anonymous functions (#263)
* Better error message for anonymous functions * Bump version * Fix format Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 5f61d88 commit c95ccfa

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "DynamicPPL"
22
uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8"
3-
version = "0.11.3"
3+
version = "0.11.4"
44

55
[deps]
66
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"

src/compiler.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ function build_model_info(input_expr)
109109
# Break up the model definition and extract its name, arguments, and function body
110110
modeldef = MacroTools.splitdef(input_expr)
111111

112+
# Check that the function has a name
113+
# https://github.com/TuringLang/DynamicPPL.jl/issues/260
114+
haskey(modeldef, :name) ||
115+
throw(ArgumentError("anonymous functions without name are not supported"))
116+
112117
# Print a warning if function body of the model is empty
113118
warn_empty(modeldef[:body])
114119

test/compiler.jl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,12 +441,25 @@ end
441441
return [10.0, 10.0] ~ MvNormal(m, 0.5 * ones(2))
442442
end) isa Function
443443

444-
@model function array_literal_model()
444+
@model function array_literal_model2()
445445
# `assume` and literal `observe`
446446
m ~ MvNormal(2, 1.0)
447447
return [10.0, 10.0] ~ MvNormal(m, 0.5 * ones(2))
448448
end
449449

450-
@test array_literal_model()() == [10.0, 10.0]
450+
@test array_literal_model2()() == [10.0, 10.0]
451+
end
452+
453+
# https://github.com/TuringLang/DynamicPPL.jl/issues/260
454+
@testset "anonymous function" begin
455+
error = ArgumentError("anonymous functions without name are not supported")
456+
@test_throws LoadError(@__FILE__, (@__LINE__) + 1, error) @macroexpand begin
457+
@model function (x)
458+
return x ~ Normal()
459+
end
460+
end
461+
@test_throws LoadError(@__FILE__, (@__LINE__) + 1, error) @macroexpand begin
462+
model = @model(x -> (x ~ Normal()))
463+
end
451464
end
452465
end

0 commit comments

Comments
 (0)