Issue with using @show in generated functions

I had written a generated function of the following form and used @show to see the final code being generated.

@generated function deserialize!(buffer, ::TypeHolder{T}) where T <: NSE_composites
    ops = [:(retval.$f = deserialize!(buffer, TypeHolder{$t}())) for (f,t) in collect(zip(fieldnames(T), T.types))]
    pushfirst!(ops, :(retval = T())) 
    push!(ops, :(return retval)) 
    ex = Expr(:block, ops...)
    @show ex
    return ex
end

Now the above code worked perfectly and did not give me any issues. However later when it was called by a an async task, the code crashed with the following error

task switch not allowed from inside staged nor pure functions

After checking some issues on github issues, I figured that this is because of calls to @show. Removing @show does solve the error.

Could someone explain what happened here, and should we avoid println and @show in generated functions always?

There are certain forbidden things in generated functions. They are listed in the relevant docs section, fourth point in the list.

2 Likes