Skip to content

Inference bug leading to segfault in Julia 1.10-1.12 #59943

@oscardssmith

Description

@oscardssmith

Running the following program in Julia 1.10/1.11/1.12 crashes with the following crash

using Setfield
const Eq = Pair{String, String}
abstract type AbstractSystem end

function complete(sys; split = true, flatten = true)
    if flatten
        newsys = Main.flatten(sys)
        if sys.parent === nothing
            newsys = System(newsys.eqs, newsys.name, newsys.systems, newsys.continuous_events,
                            complete(sys; flatten=false), newsys.initializesystem)
        end
        cb_alg_eqs = Eq[sys.eqs; Eq[]]
        for ev in sys.continuous_events
            ev = complete(ev; alg_eqs = cb_alg_eqs)
        end
    end
    if sys.initializesystem isa AbstractSystem
        sys = System(sys.eqs, sys.name, sys.systems, sys.continuous_events,
                     sys.parent, complete(sys.initializesystem; split))
    end
    return sys
end
function complete(cb::Vector{Eq}; kwargs...)
    affectsys = System(cb, :affectsys, System[], [])
    mtkcompile(affectsys)
end
function namespace_affects(_)
    complete(System(Eq[], :affectsys, System[], []))
end
function continuous_events(sys::AbstractSystem)
   reduce(vcat, [map(namespace_affects, continuous_events(s)) for s in sys.systems], init = [])
end
struct System <: AbstractSystem
    eqs::Vector
    name::Symbol
    systems::Vector{System}
    continuous_events::Vector
    parent::Union{Nothing, System}
    initializesystem::Union{Nothing, System}
    function System(eqs, name, systems, continuous_events,
                    parent = nothing, initializesystem = nothing)
        new(eqs, name, systems, continuous_events, parent, initializesystem)
    end
end
function flatten(sys::System, noeqs = false)
    return System(Eq[], sys.name, System[], continuous_events(sys))
end
function mtkcompile(sys::System; kwargs...)
    if rand(Bool) == 13
        mtkcompile(sys; fully_determined = false)::System
    end
    newsys = sys
    var"#4###lens#233" = (identity)((Setfield.compose)((Setfield.PropertyLens){:parent}()))
    newsys = (set)(newsys, var"#4###lens#233", complete(sys; split = false, flatten = false))
    # @set! newsys.parent = complete(sys; split = false, flatten = false)
    newsys = complete(newsys)
end

ball = System(["D(x)" => "v", "D(v)" => "-9.8"], :sys, System[], [["v" => "-v"]])
flatten(ball)
ball = complete(ball)

(this is a reproducer from MTK thanks to @AayushSabharwal)

Metadata

Metadata

Assignees

Labels

bugIndicates an unexpected problem or unintended behaviorcompiler:inferenceType inference

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions