Generated functions and reflection

Why is it the case that reflection is not permitted from within generated functions? (I know that e.g. Zygote does it, but I get the impression that’s frowned upon). I had a search through old posts but can’t find a clear answer. If I’ve missed a well-known post, I would appreciate a pointer towards it!

My confusion stems from the fact that I cannot see why is not safe to call e.g. Base.code_ircode_by_type(sig; world, interp, optimize_until) from a generated function (using the version of a generated function where you get access to the world age). I guess that I can’t see why the returned IRCode ought not to be a pure function of these arguments. Or does constructing a NativeInterpreter involve making use of something global and stateful?

Hi, maybe this one can help you: Constraints for `@generated` function - #2 by cscherrer

Also this one: Eval of Julia code · The Julia Language

“a generated function gets expanded at a time when the types of the arguments are known”, code_ircode_by_type calls typeinf_ircode (julia/base/compiler/typeinfer.jl at master · JuliaLang/julia · GitHub ) so I guess the types of the arguments are not known at this time and the function is not expanded. Someone with a better understanding than me can probably confirm.