Skip to content

Stack overflow when calling a function named f outside the RGF #146

Closed

Description

@penelopeysm
using RuntimeGeneratedFunctions
RuntimeGeneratedFunctions.init(@__MODULE__)

f(x) = x + 1

rgf = @RuntimeGeneratedFunction(:(function(x); f(x); end))

rgf(3)

errors dramatically with

julia> rgf(3)  # StackOverflowError: f resolves to the RGF itself, not Main.f
Warning: detected a stack overflow; program state may be corrupted, so further execution might be unreliable.
ERROR: StackOverflowError:
Stacktrace:
      [1] generated_callfunc(f::RuntimeGeneratedFunction{…}, __args::Int64)
        @ Main ./none:0
      [2] (::RuntimeGeneratedFunction{…})(args::Int64)
        @ RuntimeGeneratedFunctions ~/jl/RuntimeGeneratedFunctions.jl/src/RuntimeGeneratedFunctions.jl:230
      [3] macro expansion
        @ ./REPL[4]:1 [inlined]
      [4] macro expansion
        @ ~/jl/RuntimeGeneratedFunctions.jl/src/RuntimeGeneratedFunctions.jl:243 [inlined]
      [5] macro expansion
        @ ./none:0 [inlined]
--- the above 5 lines are repeated 39990 more times ---
 [199956] generated_callfunc(f::RuntimeGeneratedFunction{…}, __args::Int64)
        @ Main ./none:0
Some type information was truncated. Use `show(err)` to see complete types.

If the arity of the RGF and the externally defined f is different, then it segfaults instead of overflowing because of #100.

I think this is because inside the RGF body, f refers to the RGF itself:

@inline @generated function $RuntimeGeneratedFunctions.generated_callfunc(
f::$RuntimeGeneratedFunctions.RuntimeGeneratedFunction{
argnames,
cache_tag,
$_tagname,
id,
},
__args...
) where {

Activity

  1. penelopeysm commented on Aug 21, 2026

    @penelopeysm
    ContributorAuthor

    Different error if you use __args because of the same root cause:

    julia> __args(x) = x + 1
    __args (generic function with 1 method)
    
    julia> rgf = @RuntimeGeneratedFunction(:(function(x); __args(x); end))
    RuntimeGeneratedFunction(#=in Main=#, #=using Main=#, :((x,)->begin
              #= REPL[4]:1 =#
              #= REPL[4]:1 =#
              __args(x)
          end))
    
    julia> rgf(3)
    ERROR: MethodError: objects of type Tuple{Int64} are not callable
    The object of type `Tuple{Int64}` exists, but no method is defined for this combination of argument types when trying to treat it as a callable object.
    Stacktrace:
     [1] macro expansion
       @ ./REPL[4]:1 [inlined]
     [2] macro expansion
       @ ~/jl/RuntimeGeneratedFunctions.jl/src/RuntimeGeneratedFunctions.jl:243 [inlined]
     [3] macro expansion
       @ ./none:0 [inlined]
     [4] generated_callfunc(f::RuntimeGeneratedFunction{…}, __args::Int64)
       @ Main ./none:0
     [5] (::RuntimeGeneratedFunction{…})(args::Int64)
       @ RuntimeGeneratedFunctions ~/jl/RuntimeGeneratedFunctions.jl/src/RuntimeGeneratedFunctions.jl:230
     [6] top-level scope
       @ REPL[5]:1
     [7] top-level scope
       @ REPL:1
    Some type information was truncated. Use `show(err)` to see complete types.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions