diff --git a/src/managed.jl b/src/managed.jl index f36587e..6ff6f7c 100644 --- a/src/managed.jl +++ b/src/managed.jl @@ -9,6 +9,7 @@ using Cassette: Cassette using ..TypesModule: AllBound, Bound, BoundMut, Borrowed, BorrowedMut, is_moved using ..SemanticsModule: request_value, mark_moved!, unsafe_get_value using ..MacrosModule: @take +using ..PreferencesModule: is_borrow_checker_enabled # Create the Cassette context for ownership transfer Cassette.@context ManagedCtx @@ -53,6 +54,9 @@ passed to functions within the block will automatically have their ownership tra using the equivalent of `@take`. """ function managed(f) + # Get the module from the caller's context + caller_module = parentmodule(f) + is_borrow_checker_enabled(caller_module) || return f() ctx = Cassette.disablehooks(ManagedCtx()) return Cassette.@overdub(ctx, f()) end diff --git a/src/types.jl b/src/types.jl index 95dbf4c..a92c1a7 100644 --- a/src/types.jl +++ b/src/types.jl @@ -49,8 +49,6 @@ struct NoLifetime end Base.in(x, ::NoLifetime) = x - - struct Borrowed{T,O<:Union{Bound,BoundMut}} value::T owner::O diff --git a/test/FakeModule/src/FakeModule.jl b/test/FakeModule/src/FakeModule.jl index ae316b7..b0a85fe 100644 --- a/test/FakeModule/src/FakeModule.jl +++ b/test/FakeModule/src/FakeModule.jl @@ -14,7 +14,6 @@ function test() @test x[] == 1 # @take should just return the value directly @test (@take x)[] == 1 - # This error now goes undetected: @test x[] == 1 @@ -36,6 +35,20 @@ function test() @test z == 3 @test r == 2 # r should just be a copy end + + # Test managed() - it should just run functions as-is when disabled + function expects_raw_int(x::Int) + return x + 1 + end + + @bind w = 42 + # When enabled, managed() would automatically convert w to raw Int, + # but when disabled it should fail since w is passed as-is + result = managed() do + expects_raw_int(w) + end + @test result == 43 # Function runs normally since w is just a raw Int + @test w == 42 # w is not moved since managed() is disabled end end