Skip to content

Function definition order matters with generated functions #20326

@omus

Description

@omus

Any function that is called within a generated function needs to be defined before the generated function definition otherwise we encounter a MethodError from the world being too new:

julia> f1() = 1
f1 (generic function with 1 method)

julia> @generated function g1()
           v = f1()
           quote
               return $v
           end
       end
g1 (generic function with 1 method)

julia> g1()
1

vs.

julia> @generated function g2()
           v = f2()
           quote
               return $v
           end
       end
g2 (generic function with 1 method)

julia> f2() = 1
f2 (generic function with 1 method)

julia> g2()
ERROR: MethodError: no method matching f2()
The applicable method may be too new: running in world age 21396, while current world is 21397.
Closest candidates are:
  f2() at REPL[5]:1 (method too new to be called from this world context.)
Stacktrace:
 [1] g2(...) at ./REPL[4]:2

Introduced with #17057

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions