Return CodeInfo instead of expression from a generated function?

Is it possible to return a CodeInfo object instead of expression from a generated function? I’ve seen it in a couple of packages recently, but I can’t find any docs for it and my naive implementation fails with segfault:

foo(x) = 2x + 1

@generated function inspect(f, args...)
    ci = code_lowered(f.instance, args)[1]
    # do something
    return ci
end

inspect(foo, 9.0)

gives

Internal error: encountered unexpected error in runtime:
BoundsError(a=Array{Core.Compiler.SlotInfo, (2,)}[
  Core.Compiler.SlotInfo(defs=Array{Int64, (0,)}[], uses=Array{Int64, (0,)}[], any_newvar=false),
  Core.Compiler.SlotInfo(defs=Array{Int64, (0,)}[], uses=Array{Int64, (0,)}[], any_newvar=false)], i=(Core.Compiler.UnitRange{Int64}(start=1, stop=3),))
rec_backtrace at /buildworker/worker/package_linux64/build/src/stackwalk.c:94
...
unknown function (ip: 0xffffffffffffffff)
Allocations: 4114249 (Pool: 4113306; Big: 943); GC: 8
Segmentation fault (core dumped)
1 Like