Skip to content

Commit 77533f5

Browse files
committed
More helpful error message
1 parent 8550c1c commit 77533f5

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

src/compiler.jl

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,8 @@ function _error_msg()
1111
return "This macro is only for use in the `@model` macro and not for external use."
1212
end
1313

14-
15-
16-
# Check if the right-hand side is a distribution.
17-
function assert_dist(dist; msg)
18-
isa(dist, Distribution) || throw(ArgumentError(msg))
19-
end
20-
function assert_dist(dist::AbstractVector; msg)
21-
all(d -> isa(d, Distribution), dist) || throw(ArgumentError(msg))
22-
end
23-
24-
function wrong_dist_errormsg(l)
25-
return "Right-hand side of a ~ must be subtype of Distribution or a vector of " *
26-
"Distributions on line $(l)."
27-
end
14+
const DISTMSG = "Right-hand side of a ~ must be subtype of Distribution or a vector of " *
15+
"Distributions."
2816

2917
"""
3018
isassumption(model, expr)
@@ -260,14 +248,14 @@ function replace_tilde!(model_info)
260248
dotargs = getargs_dottilde(x)
261249
if dotargs !== nothing
262250
L, R = dotargs
263-
return generate_dot_tilde(L, R, model_info)
251+
return Base.remove_linenums!(generate_dot_tilde(L, R, model_info))
264252
end
265253

266254
# Check tilde.
267255
args = getargs_tilde(x)
268256
if args !== nothing
269257
L, R = args
270-
return generate_tilde(L, R, model_info)
258+
return Base.remove_linenums!(generate_tilde(L, R, model_info))
271259
end
272260

273261
return x
@@ -296,7 +284,8 @@ function generate_tilde(left, right, model_info)
296284

297285
@gensym tmpright
298286
top = [:($tmpright = $right),
299-
:($(DynamicPPL.assert_dist)($tmpright, msg = $(wrong_dist_errormsg(@__LINE__))))]
287+
:($tmpright isa Union{$Distribution,AbstractVector{<:$Distribution}}
288+
|| throw(ArgumentError($DISTMSG)))]
300289

301290
if left isa Symbol || left isa Expr
302291
@gensym out vn inds
@@ -358,7 +347,8 @@ function generate_dot_tilde(left, right, model_info)
358347

359348
@gensym tmpright
360349
top = [:($tmpright = $right),
361-
:($(DynamicPPL.assert_dist)($tmpright, msg = $(wrong_dist_errormsg(@__LINE__))))]
350+
:($tmpright isa Union{$Distribution,AbstractVector{<:$Distribution}}
351+
|| throw(ArgumentError($DISTMSG)))]
362352

363353
if left isa Symbol || left isa Expr
364354
@gensym out vn inds

0 commit comments

Comments
 (0)