Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ResumableFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ include("safe_logging.jl")
include("types.jl")
include("transforms.jl")
include("utils.jl")
include("scoping_backends.jl")
include("macro.jl")

end
4 changes: 1 addition & 3 deletions src/macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ macro resumable(ex::Expr...)
_name = func_def[:name]
end

scope = ScopeTracker(0, __module__, [Dict(i =>i for i in vcat(args, kwargs, [_name], params...))])
func_def[:body] = scoping(copy(func_def[:body]), scope)
func_def[:body] = postwalk(x->transform_remove_local(x), func_def[:body])
func_def[:body] = apply_scope_fixes(func_def[:body], args, kwargs, _name, params, __module__)
@debug func_def[:body] |> striplines

inferfn, slots = get_slots(copy(func_def), arg_dict, __module__)
Expand Down
56 changes: 56 additions & 0 deletions src/scoping_backends.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
Selects which scoping backend to use for `@resumable` macro expansion.

Currently supported values:
- `legacy` (default): use the existing `ScopeTracker` + `scoping(...)` pass.
- `julialowering`: reserved for the experimental JuliaLowering-based path.

The selector is intentionally environment-based for now so the package can keep
current behavior by default while allowing controlled experiments on dedicated
branches.
"""
function scoping_backend()
backend = lowercase(get(ENV, "RESUMABLEFUNCTIONS_SCOPE_BACKEND", "legacy"))
backend in ("legacy", "julialowering") || throw(ArgumentError(
"Unsupported RESUMABLEFUNCTIONS_SCOPE_BACKEND=$(repr(backend)). Expected `legacy` or `julialowering`."
))
return Symbol(backend)
end

function apply_scope_fixes(func_body, args, kwargs, name, params, mod::Module)
backend = scoping_backend()
backend === :legacy && return apply_scope_fixes_legacy(func_body, args, kwargs, name, params, mod)
backend === :julialowering && return apply_scope_fixes_julialowering(func_body, args, kwargs, name, params, mod)
error("Unreachable scoping backend: $backend")
end

function apply_scope_fixes_legacy(func_body, args, kwargs, name, params, mod::Module)
scope = ScopeTracker(0, mod, [Dict(i => i for i in vcat(args, kwargs, [name], params...))])
func_body = scoping(copy(func_body), scope)
func_body = postwalk(x->transform_remove_local(x), func_body)
return func_body
end

function julialowering_probe()
package_path = Base.find_package("JuliaLowering")
available = !isnothing(package_path)
return (
available = available,
package_path = package_path,
status = available ? :installed_unimplemented : :missing_dependency,
)
end

function julialowering_error_message()
probe = julialowering_probe()
pkg_hint = probe.available ?
"JuliaLowering.jl is installed, but the integration path is not implemented on this branch yet. " :
"JuliaLowering.jl is not installed in this environment. "

return pkg_hint *
"Use `RESUMABLEFUNCTIONS_SCOPE_BACKEND=legacy` (default) or continue the experimental JuliaLowering integration work."
end

function apply_scope_fixes_julialowering(func_body, args, kwargs, name, params, mod::Module)
throw(ArgumentError(julialowering_error_message()))
end
57 changes: 57 additions & 0 deletions test/test_main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,63 @@ end
@test collect((test_quotenode((a = 3, b = 4)))) == [5^2]
end

@testset "test_scoping_backend_selection" begin
withenv("RESUMABLEFUNCTIONS_SCOPE_BACKEND" => "legacy") do
@eval begin
@resumable function test_scope_backend_legacy()
x = 41
@yield x + 1
end
end
result = Base.invokelatest(() -> collect(Base.invokelatest(test_scope_backend_legacy)))
@test result == [42]
end

probe = ResumableFunctions.julialowering_probe()
@test haskey(pairs(probe), :available)
@test haskey(pairs(probe), :package_path)
@test haskey(pairs(probe), :status)
@test probe.available == !isnothing(probe.package_path)
@test probe.status in (:missing_dependency, :installed_unimplemented)

msg = ResumableFunctions.julialowering_error_message()
@test occursin("JuliaLowering", msg)
@test occursin("legacy", msg)

withenv("RESUMABLEFUNCTIONS_SCOPE_BACKEND" => "unknown_backend") do
err = try
@eval begin
@resumable function test_scope_backend_invalid()
@yield 1
end
end
nothing
catch ex
ex
end
@test err isa LoadError
@test err.error isa ArgumentError
@test occursin("RESUMABLEFUNCTIONS_SCOPE_BACKEND", sprint(showerror, err.error))
end

withenv("RESUMABLEFUNCTIONS_SCOPE_BACKEND" => "julialowering") do
err = try
@eval begin
@resumable function test_scope_backend_julialowering()
@yield 1
end
end
nothing
catch ex
ex
end
@test err isa LoadError
@test err.error isa ArgumentError
@test occursin("JuliaLowering", sprint(showerror, err.error))
@test occursin("legacy", sprint(showerror, err.error))
end
end

@testset "test_named_tuple" begin
@resumable function test_named_tuple(u, v)
r = @NamedTuple{a::Int, b::Int}[]
Expand Down
Loading