Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect inference in MTK #43082

Closed
Keno opened this issue Nov 15, 2021 · 5 comments · Fixed by #46350
Closed

Incorrect inference in MTK #43082

Keno opened this issue Nov 15, 2021 · 5 comments · Fixed by #46350
Assignees
Labels
bug Indicates an unexpected problem or unintended behavior compiler:inference Type inference types and dispatch Types, subtyping and method dispatch

Comments

@Keno
Copy link
Member

Keno commented Nov 15, 2021

Using Julia and MTK master:

using ModelingToolkit
using ModelingToolkit.BipartiteGraphs
graph = ModelingToolkit.BipartiteGraph(0, 0, Val(false))
f() = DiCMOBiGraph{true}(graph)
julia> @code_typed f()
CodeInfo(
1 ─ %1 = Main.graph::Any
│        (DiCMOBiGraph{true, I} where I)(%1)::Union{}
└──      unreachable
) => Union{}

julia> f()
Unreachable reached at 0x7f4edee39050

The inference of the constructor is incorrect. The function returns, causing the subsequent crash. I suspect an intersection issue of some sort as usual, but I will investigate further.

@Keno Keno added bug Indicates an unexpected problem or unintended behavior compiler:inference Type inference types and dispatch Types, subtyping and method dispatch labels Nov 15, 2021
@Keno
Copy link
Member Author

Keno commented Nov 15, 2021

julia> TA = (@which DiCMOBiGraph{true}(graph)).sig
Tuple{Type{DiCMOBiGraph{Transposed, I} where I}, BipartiteGraph} where Transposed

julia> TB = Tuple{Type{DiCMOBiGraph{true}}, Any}
Tuple{Type{DiCMOBiGraph{true, I} where I}, Any}

julia> TC = Tuple{Type{DiCMOBiGraph{true}}, BipartiteGraph}
Tuple{Type{DiCMOBiGraph{true, I} where I}, BipartiteGraph}

julia> TC <: TA
true

julia> TC <: TB
true

julia> typeintersect(TA, TB)
Union{}

@Keno
Copy link
Member Author

Keno commented Nov 15, 2021

Non-MTK reduction:

mutable struct Foo{I<:Integer,B<:Union{Vector{Vector{I}},I}}
    ne::Int
    badjlist::B # `badjlist[dst] => srcs` or `ndsts`
end
Foo() = Foo(0, 0)
foo = Foo()

struct Bar{Transposed, I, G<:Foo{I}}; end
Bar{Transposed}(g::Foo) where {Transposed} = Bar{Transposed, typeof(g).parameters[1], typeof(g)}()
f() = Bar{true}(foo)
f()

@Keno
Copy link
Member Author

Keno commented Nov 15, 2021

Also happens on 1.7. Haven't seen this in the wild yet with regular MTK usage, but that of course depends on inference to figure out the argument to this constructor.

@JeffBezanson
Copy link
Member

1.0: works
1.1: stack overflow in intersection, aborting inference
1.2: this version of the bug introduced

@JeffBezanson
Copy link
Member

Slightly smaller:

julia> struct X{A, I, B<:Union{Ref{I},I}}; end

julia> typeintersect(Tuple{X{T  }, Int} where T, Tuple{X{Int}, Any})
Union{}

Keno added a commit to Keno/ModelingToolkit.jl that referenced this issue Nov 16, 2021
Avoids a base julia type intersection issue: JuliaLang/julia#43082.
The overparameterization is not particularly required - Julia is fast
at union splitting and this access is not particularly hot anyway.
Keno added a commit to Keno/ModelingToolkit.jl that referenced this issue Nov 16, 2021
Avoids a base julia type intersection issue: JuliaLang/julia#43082.
The overparameterization is not particularly required - Julia is fast
at union splitting and this access is not particularly hot anyway.
YingboMa pushed a commit to SciML/ModelingToolkit.jl that referenced this issue Nov 17, 2021
* Don't overparametrize BipartiteGraph

Avoids a base julia type intersection issue: JuliaLang/julia#43082.
The overparameterization is not particularly required - Julia is fast
at union splitting and this access is not particularly hot anyway.
N5N3 added a commit to N5N3/julia that referenced this issue Aug 15, 2022
N5N3 added a commit to N5N3/julia that referenced this issue Aug 15, 2022
N5N3 added a commit that referenced this issue Aug 18, 2022
… tries a new Union decision (#46350)

* `intersect_all` should always `restore_env`. let `merge_env` track valid `env` change.

* Add test.

Co-authored-by: Jeff Bezanson <[email protected]>
KristofferC pushed a commit that referenced this issue Aug 26, 2022
… tries a new Union decision (#46350)

* `intersect_all` should always `restore_env`. let `merge_env` track valid `env` change.

* Add test.

Co-authored-by: Jeff Bezanson <[email protected]>
(cherry picked from commit 9aabb4c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior compiler:inference Type inference types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants