Skip to content

Commit 448d31b

Browse files
committed
Fix anonymous argument with type parameter (#394)
Fixes #393. ~~Well, it fixes the error but evaluation does not work it seems.~~ cc: @torfjelde
1 parent f2eb635 commit 448d31b

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

Project.toml

+1-1
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.17.9"
3+
version = "0.17.10"
44

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

src/compiler.jl

+4-2
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ function build_model_info(input_expr)
245245
return modelinfo
246246
end
247247

248+
# Ensure that all arguments have a name, i.e., are of the form `name` or `name::T`
249+
addargnames!(modeldef[:args])
250+
248251
# Extract the positional and keyword arguments from the model definition.
249252
allargs = vcat(modeldef[:args], modeldef[:kwargs])
250253

@@ -262,8 +265,7 @@ function build_model_info(input_expr)
262265
# Extract the names of the arguments.
263266
allargs_syms = map(allargs_exprs) do arg
264267
MacroTools.@match arg begin
265-
(::Type{T_}) | (name_::Type{T_}) => T
266-
name_::T_ => name
268+
(name_::_) => name
267269
x_ => x
268270
end
269271
end

src/utils.jl

+42
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,48 @@ macro addlogprob!(ex)
7474
end
7575
end
7676

77+
"""
78+
addargnames!(args)
79+
80+
Adds names to unnamed arguments in `args`.
81+
82+
The names are generated with `gensym(:arg)` to avoid conflicts with other variable names.
83+
84+
# Examples
85+
86+
```jldoctest; filter = r"var\\"##arg#[0-9]+\\""
87+
julia> args = :(f(x::Int, y, ::Type{T}=Float64)).args[2:end]
88+
3-element Vector{Any}:
89+
:(x::Int)
90+
:y
91+
:($(Expr(:kw, :(::Type{T}), :Float64)))
92+
93+
julia> DynamicPPL.addargnames!(args)
94+
95+
julia> args
96+
3-element Vector{Any}:
97+
:(x::Int)
98+
:y
99+
:($(Expr(:kw, :(var"##arg#301"::Type{T}), :Float64)))
100+
```
101+
"""
102+
function addargnames!(args)
103+
if isempty(args)
104+
return nothing
105+
end
106+
107+
@inbounds for i in eachindex(args)
108+
arg = args[i]
109+
if MacroTools.@capture(arg, ::T_)
110+
args[i] = Expr(:(::), gensym(:arg), T)
111+
elseif MacroTools.@capture(arg, ::T_ = val_)
112+
args[i] = Expr(:kw, Expr(:(::), gensym(:arg), T), val)
113+
end
114+
end
115+
116+
return nothing
117+
end
118+
77119
"""
78120
getargs_dottilde(x)
79121

test/compiler.jl

+6
Original file line numberDiff line numberDiff line change
@@ -602,4 +602,10 @@ end
602602
@test !DynamicPPL.hasmissing(Matrix{Real})
603603
@test !DynamicPPL.hasmissing(Vector{Matrix{Float32}})
604604
end
605+
606+
@testset "issue #393: anonymous argument with type parameter" begin
607+
@model f_393(::Val{ispredict}=Val(false)) where {ispredict} = ispredict ? 0 : 1
608+
@test f_393()() == 1
609+
@test f_393(Val(true))() == 0
610+
end
605611
end

0 commit comments

Comments
 (0)