Skip to content

Problem Using @isdefined Inside a @resumable Function #119

Description

@MarkNahabedian

I'm having a problem using @isdefined inside a @resumable
function (https://github.com/JuliaDynamics/ResumableFunctions.jl).

function foo(x)
    local y
    @isdefined y
end

works, but

@resumable function foo(x)
    local y
    @isdefined y
end

gets the error

ERROR: LoadError: MethodError: no method matching var"@isdefined"(::LineNumberNode, ::Module, ::Expr)

Closest candidates are:
  var"@isdefined"(::LineNumberNode, ::Module, !Matched::Symbol)
   @ Base essentials.jl:177

When I try to macroexpand the same definition I get the same
error.

@macroexpand @Resumable function foo(x)
local y
y
end


The problem appears to be because `@resumable` replaces local
variables in the function body with struct property references:

using MacroTools
prewalk(rmlines,
@macroexpand @Resumable function foo(x)
local y
y
end
)
quote
mutable struct var"##foo_FSMI#307"{var"##y#308" <: Any, var"##x#309" <: Any} <: ResumableFunctions.FiniteStateMachineIterator{Any}
_state::UInt8
y::var"##y#308"
x::var"##x#309"
function var"##foo_FSMI#307"{var"##y#308", var"##x#309"}(; ) where {var"##y#308" <: Any, var"##x#309" <: Any}
fsmi = new()
fsmi._state = 0x00
fsmi
end
function var"##foo_FSMI#307"(; )
var"##foo_FSMI#307"{Any, Any}()
end
end
function (_fsmi::var"##foo_FSMI#307")(_arg::Any = nothing; )
_fsmi._state === 0x00 && $(Expr(:symbolicgoto, :_STATE_0))
error("@Resumable function has stopped!")
$(Expr(:symboliclabel, :_STATE_0))
_fsmi._state = 0xff
_arg isa Exception && throw(_arg)
local _fsmi.y
_fsmi.y
end
begin
$(Expr(:meta, :doc))
function foo(x::Any; )
fsmi = ResumableFunctions.typed_fsmi(var"##foo_FSMI#307", var"##306", x)
fsmi.x = x
fsmi
end
end
end


I don't see an obvioous fix for this, but feel it should be noted as
the error message doesn't clearly point to the problem.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions