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

Function definition causes StackOverflowError #41948

Open
Keno opened this issue Aug 21, 2021 · 4 comments
Open

Function definition causes StackOverflowError #41948

Keno opened this issue Aug 21, 2021 · 4 comments
Labels
domain:types and dispatch Types, subtyping and method dispatch kind:bug Indicates an unexpected problem or unintended behavior

Comments

@Keno
Copy link
Member

Keno commented Aug 21, 2021

I knew I was committing type crimes when I wrote this code, but feels like it should at least not stack overflow?

julia> struct Default{T}
           val::T
       end

julia> Base.convert(::Type{Default{S}}, d::Default{T}) where {S, T} = Default(convert(S, d.val))

julia> function Base.convert(TT::Type{Union{S, Default{S}}}, d::Default{T}) where {S, T}
           Default(convert(S, d.val))
       end

julia> function Base.convert(TT::Type{Union{S, Default{S}}}, b) where {S, T}
           convert(S, b)
       end

julia> Base.convert(TT::Type{Union{S, Default{S}}}, b::Union{S, Default{S}}) where {S} = b
ERROR: StackOverflowError:
Stacktrace:
 [1] top-level scope
   @ REPL[8]:1
@JeffBezanson JeffBezanson added domain:types and dispatch Types, subtyping and method dispatch kind:bug Indicates an unexpected problem or unintended behavior labels Aug 24, 2021
@User-764Q
Copy link

Hi,

I tried to recreate this today on Julia 1.6.2 and got no errors so maybe it has been fixed?

image

@ianatol
Copy link
Member

ianatol commented Nov 3, 2021

Successfully recreated on master and 1.6.2, will look into it

@ianatol
Copy link
Member

ianatol commented Nov 3, 2021

You only need 2 of the definitions to cause the error. Also, trying to define it again after getting the stack overflow is successful.

julia> struct Default{T}
           val::T
       end

julia> function Base.convert(TT::Type{Union{S, Default{S}}}, d::Default{T}) where {S, T}
                  Default(convert(S, d.val))
              end

julia> Base.convert(TT::Type{Union{S, Default{S}}}, b::Union{S, Default{S}}) where {S} = b
ERROR: StackOverflowError:
Stacktrace:
 [1] top-level scope
   @ REPL[3]:1

julia> Base.convert(TT::Type{Union{S, Default{S}}}, b::Union{S, Default{S}}) where {S} = b

julia> 

@Keno
Copy link
Member Author

Keno commented Nov 20, 2021

Another case:

struct Unassigned; end
struct Matching{U>:Unassigned, V<:AbstractVector{<:Union{U, Int}}} <: AbstractVector{Union{U, Int}}
    match::V
    inv_match::Union{Nothing, V}
end
Matching{U}(v::V) where {U, V<:AbstractVector{<:Union{U, Int}}} = Matching{U, V}(v, nothing)
Matching{U}(v::V, iv::Union{V, Nothing}) where {U, V<:AbstractVector{<:Union{U, Int}}} = Matching{U, V}(v, iv)
Matching(v::V) where {U>:Unassigned, V<:AbstractVector{<:Union{U, Int}}} =
    Matching{U, V}(v, nothing)
Matching(m::Int) = Matching{Unassigned}(Union{Int, Unassigned}[unassigned for _ = 1:m], nothing)
Matching{V}(m::Matching{U}) where {U,V} = Matching{V}(convert(V, m.match),
    m.inv_match === nothing ? nothing : convert(V, m.inv_match))

gives

julia> Matching{V}(m::Matching{U}) where {U,V} = Matching{V}(convert(V, m.match),
           m.inv_match === nothing ? nothing : convert(V, m.inv_match))
ERROR: StackOverflowError:
Stacktrace:
 [1] top-level scope
   @ REPL[7]:1

Not sure if it's the same issue or not, but I guess we'll find out once one or the other is fixed:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:types and dispatch Types, subtyping and method dispatch kind:bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

4 participants