Skip to content

Commit

Permalink
feat: disable managed too
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Jan 10, 2025
1 parent 6ae8a29 commit def2317
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/managed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ struct NoLifetime end

Base.in(x, ::NoLifetime) = x



struct Borrowed{T,O<:Union{Bound,BoundMut}}
value::T
owner::O
Expand Down
15 changes: 14 additions & 1 deletion test/FakeModule/src/FakeModule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

0 comments on commit def2317

Please sign in to comment.