Skip to content

Better purity checks for @generated functions #55332

Open
@MilesCranmer

Description

@MilesCranmer

According to @aviatesk in #55147, the following code:

g() = [i for i in 1:0]
@generated function foo()
    if g() isa Vector{Int}
        return :(1)
    else
        return :(2)
    end
end

violates assumptions in @generated due to the implicit use of Core.Compiler.return_type in the list comprehension and thus might lead to Illegal instruction errors and segfaults.

However, Julia seems happy to compile it, and it seems to work without error most of the time.

If this is accurate, it is a major footgun, and I think Julia needs to be able to detect such behavior before compiling. This is similar to how Julia can detect the use of closures in generated functions and refuse to compile them.

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