From a9d4dcd4485b554a440fab2a1017f7c817459d47 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Fri, 13 Jun 2025 18:10:01 +0100 Subject: [PATCH 1/7] Add test for naked call to produce --- test/copyable_task.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/copyable_task.jl b/test/copyable_task.jl index f6562993..59c44e42 100644 --- a/test/copyable_task.jl +++ b/test/copyable_task.jl @@ -138,6 +138,12 @@ @test ex isa BoundsError end end + + @testset "Naked produce" begin + @test_throws "wrap the call to `produce` in a function" Libtask.consume( + Libtask.TapedTask(nothing, Libtask.produce, 0) + ) + end end @testset "copying" begin From 850523d31391d546e1d4edf6131ac7a26308dba6 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Fri, 13 Jun 2025 18:13:43 +0100 Subject: [PATCH 2/7] Add test for issue #185 --- test/copyable_task.jl | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/copyable_task.jl b/test/copyable_task.jl index 59c44e42..54214e7f 100644 --- a/test/copyable_task.jl +++ b/test/copyable_task.jl @@ -215,4 +215,16 @@ end @test ex === nothing end + + @testset "Issue #185" begin + function g(x) + if x > 0 + x = 2 + else + x = 0.1 + end + return produce(x) + end + @test Libtask.consume(Libtask.TapedTask(nothing, g, 1.0)) == 2 + end end From 89b4d8083b732d2b9684cf2c9b96fbdee71d2b36 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Fri, 13 Jun 2025 18:16:12 +0100 Subject: [PATCH 3/7] Add helpful error for naked produce call --- src/copyable_task.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/copyable_task.jl b/src/copyable_task.jl index 2334bb33..02148d9f 100644 --- a/src/copyable_task.jl +++ b/src/copyable_task.jl @@ -72,6 +72,13 @@ the current world age, will make a copy of an existing `MistyClosure`. If not, will derive it from scratch (derive the IR + compile it etc). """ function build_callable(sig::Type{<:Tuple}) + if sig <: Tuple{typeof(produce),Any} + msg = """ + Can not construct a TapedTask for a 'naked' call to `produce`. + Please wrap the call to `produce` in a function, and construct a + TapedTask from that function.""" + throw(ArgumentError(msg)) + end key = CacheKey(Base.get_world_counter(), sig) if haskey(mc_cache, key) return fresh_copy(mc_cache[key]) From 4be43694b04731697601207efdde88ffaba10bb2 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Fri, 13 Jun 2025 18:17:42 +0100 Subject: [PATCH 4/7] Alternative check of is_produce_stmt --- src/copyable_task.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/copyable_task.jl b/src/copyable_task.jl index 02148d9f..2210c5e1 100644 --- a/src/copyable_task.jl +++ b/src/copyable_task.jl @@ -374,8 +374,8 @@ get_value(x) = x expression, otherwise `false`. """ function is_produce_stmt(x)::Bool - if Meta.isexpr(x, :invoke) && length(x.args) == 3 - return get_value(x.args[2]) === produce + if Meta.isexpr(x, :invoke) && length(x.args) == 3 && x.args[1] isa Core.MethodInstance + return x.args[1].specTypes <: Tuple{typeof(produce),Any} elseif Meta.isexpr(x, :call) && length(x.args) == 2 return get_value(x.args[1]) === produce else From f1355f0bde377502f99746efba61b9a713ab9110 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Mon, 16 Jun 2025 15:55:57 +0100 Subject: [PATCH 5/7] Fix issue 185 test --- test/copyable_task.jl | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/test/copyable_task.jl b/test/copyable_task.jl index 54214e7f..4f6431b7 100644 --- a/test/copyable_task.jl +++ b/test/copyable_task.jl @@ -217,14 +217,7 @@ end @testset "Issue #185" begin - function g(x) - if x > 0 - x = 2 - else - x = 0.1 - end - return produce(x) - end - @test Libtask.consume(Libtask.TapedTask(nothing, g, 1.0)) == 2 + g() = produce(randn() > 0 ? 2 : 0.1) + @test Libtask.consume(Libtask.TapedTask(nothing, g)) == 2 end end From 9d74bbbe6af087c74a04db90925fca51d066347a Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Mon, 16 Jun 2025 16:06:29 +0100 Subject: [PATCH 6/7] Fix test randomness --- test/copyable_task.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/copyable_task.jl b/test/copyable_task.jl index 4f6431b7..6837e881 100644 --- a/test/copyable_task.jl +++ b/test/copyable_task.jl @@ -217,7 +217,7 @@ end @testset "Issue #185" begin - g() = produce(randn() > 0 ? 2 : 0.1) + g() = produce(rand() > -1.0 ? 2 : 0.1) @test Libtask.consume(Libtask.TapedTask(nothing, g)) == 2 end end From d1ce33c1af435d9d4bb9838c2ab3099cc3b9eb37 Mon Sep 17 00:00:00 2001 From: Markus Hauru Date: Mon, 16 Jun 2025 17:05:27 +0100 Subject: [PATCH 7/7] Bump patch version to 0.9.2 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 7920b392..3a54c667 100644 --- a/Project.toml +++ b/Project.toml @@ -3,7 +3,7 @@ uuid = "6f1fad26-d15e-5dc8-ae53-837a1d7b8c9f" license = "MIT" desc = "Tape based task copying in Turing" repo = "https://github.com/TuringLang/Libtask.jl.git" -version = "0.9.1" +version = "0.9.2" [deps] MistyClosures = "dbe65cb8-6be2-42dd-bbc5-4196aaced4f4"