With the optional generator syntax from #23168, there is currently no way of discovering errors that occur while executing the optional generator. In the case of a @generated function (ie. generated_only), the error will at least be noticed at run-time, but with optional generators we'll just ignore any error: https://github.com/JuliaLang/julia/blob/bca94e4bd5f491661249c0ba0d83f3abdbbbcb43/base/compiler/utilities.jl#L89-L96
Just to be clear:
julia> function foo()
if @generated
error("I am invisible")
else
42
end
end
foo (generic function with 1 method)
julia> foo()
42
whereas
julia> @generated function bar()
error("I am not invisible")
end
bar (generic function with 1 method)
julia> bar()
ERROR: I am not invisible
Stacktrace:
[1] error at ./error.jl:33 [inlined]
[2] #s2#3(::Any) at ./REPL[5]:2
[3] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any,N} where N) at ./boot.jl:466
[4] top-level scope
I've long had a local change that prints the error along with a backtrace, since with CUDAnative we don't even get to see the error of a @generated function (because of allocs, calls to the runtime, etc). Now that there's a similar situation with regular code, I wonder if we need to do something more user friendly.
What it should probably do is turn in an Expr(:error), which gets thrown at runtime.
Most helpful comment
What it should probably do is turn in an
Expr(:error), which gets thrown at runtime.