Skip to content

Commit 42036af

Browse files
authored
Issue #185 (#187)
* Add test for naked call to produce * Add test for issue #185 * Add helpful error for naked produce call * Alternative check of is_produce_stmt * Fix issue 185 test * Fix test randomness * Bump patch version to 0.9.2
1 parent 1a71063 commit 42036af

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
@@ -3,7 +3,7 @@ uuid = "6f1fad26-d15e-5dc8-ae53-837a1d7b8c9f"
33
license = "MIT"
44
desc = "Tape based task copying in Turing"
55
repo = "https://github.com/TuringLang/Libtask.jl.git"
6-
version = "0.9.1"
6+
version = "0.9.2"
77

88
[deps]
99
MistyClosures = "dbe65cb8-6be2-42dd-bbc5-4196aaced4f4"

src/copyable_task.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ the current world age, will make a copy of an existing `MistyClosure`. If not,
7272
will derive it from scratch (derive the IR + compile it etc).
7373
"""
7474
function build_callable(sig::Type{<:Tuple})
75+
if sig <: Tuple{typeof(produce),Any}
76+
msg = """
77+
Can not construct a TapedTask for a 'naked' call to `produce`.
78+
Please wrap the call to `produce` in a function, and construct a
79+
TapedTask from that function."""
80+
throw(ArgumentError(msg))
81+
end
7582
key = CacheKey(Base.get_world_counter(), sig)
7683
if haskey(mc_cache, key)
7784
return fresh_copy(mc_cache[key])
@@ -367,8 +374,8 @@ get_value(x) = x
367374
expression, otherwise `false`.
368375
"""
369376
function is_produce_stmt(x)::Bool
370-
if Meta.isexpr(x, :invoke) && length(x.args) == 3
371-
return get_value(x.args[2]) === produce
377+
if Meta.isexpr(x, :invoke) && length(x.args) == 3 && x.args[1] isa Core.MethodInstance
378+
return x.args[1].specTypes <: Tuple{typeof(produce),Any}
372379
elseif Meta.isexpr(x, :call) && length(x.args) == 2
373380
return get_value(x.args[1]) === produce
374381
else

test/copyable_task.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@
138138
@test ex isa BoundsError
139139
end
140140
end
141+
142+
@testset "Naked produce" begin
143+
@test_throws "wrap the call to `produce` in a function" Libtask.consume(
144+
Libtask.TapedTask(nothing, Libtask.produce, 0)
145+
)
146+
end
141147
end
142148

143149
@testset "copying" begin
@@ -209,4 +215,9 @@
209215
end
210216
@test ex === nothing
211217
end
218+
219+
@testset "Issue #185" begin
220+
g() = produce(rand() > -1.0 ? 2 : 0.1)
221+
@test Libtask.consume(Libtask.TapedTask(nothing, g)) == 2
222+
end
212223
end

0 commit comments

Comments
 (0)