Skip to content

New restrictions on generated functions in 1.12? #57701

@KristofferC

Description

@KristofferC

Consider the following generated function:

@generated function constructorof(::Type{T}) where T
    getfield(parentmodule(T), nameof(T))
end

This should (at least from what I can see) be an ok generated function considering the current restrictions the docs places on them. It only calls functions defined before the function and it only reads global constants. However, it currently gives a warning

julia> struct Foo end

julia> constructorof(Foo)
WARNING: Detected access to binding `Main.Foo` in a world prior to its definition world.
  Julia 1.12 has introduced more strict world age semantics for global bindings.
  !!! This code may malfunction under Revise.
  !!! This code will error in future versions of Julia.
Hint: Add an appropriate `invokelatest` around the access to this binding.
Foo

So it seems that there is an additional restriction to generated functions now in that it is only allowed to read a global constant defined before the generated function. If that is the case, the documentation should be updated.

A lot of code in e.g. ConstructionBase uses this pattern:

https://github.com/JuliaObjects/ConstructionBase.jl/blob/72bb19dad11695fd94de66c738db534f89b23878/src/ConstructionBase.jl#L29-L31

https://github.com/JuliaObjects/ConstructionBase.jl/blob/72bb19dad11695fd94de66c738db534f89b23878/src/functions.jl#L11

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions