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

Order-dependent error in recursive data type constructor #42297

Closed
tkf opened this issue Sep 17, 2021 · 0 comments · Fixed by #44797
Closed

Order-dependent error in recursive data type constructor #42297

tkf opened this issue Sep 17, 2021 · 0 comments · Fixed by #44797
Labels
regression Regression in behavior compared to a previous version types and dispatch Types, subtyping and method dispatch

Comments

@tkf
Copy link
Member

tkf commented Sep 17, 2021

The following definition of a recursive data type does not work in Julia 1.3 and later.

julia> mutable struct Node{T,V}
           value::V
           next::Union{Node{T,T},Node{T,Val{T}},Nothing}

           Node{T}(value) where {T} = new{T,typeof(value)}(value, nothing)
       end

julia> Node{Int}(1)
Node{Int64, Int64}(1, nothing)

julia> Node{Int}(Val{Int}())
ERROR: MethodError: Cannot `convert` an object of type Val{Int64} to an object of type Int64
Closest candidates are:
  convert(::Type{T}, ::Ptr) where T<:Integer at pointer.jl:23
  convert(::Type{T}, ::T) where T<:Number at number.jl:6
  convert(::Type{T}, ::Number) where T<:Number at number.jl:7
  ...
Stacktrace:
 [1] (Node{Int64, V} where V)(value::Val{Int64})
   @ Main ./REPL[1]:5

If I reboot julia and then evaluate Node{Int}(Val{Int}()) first, it does not throw but Node{Int}(1) now throws:

julia> Node{Int}(Val{Int}())
Node{Int64, Val{Int64}}(Val{Int64}(), nothing)

julia> Node{Int}(1)
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Val{Int64}

Julia 1.0.5 and 1.2.0 can handle construction in both orders. But in 1.3.1, 1.7.0-beta, and 1.8-DEV, I see the above order-dependent behavior.

@tkf tkf added the types and dispatch Types, subtyping and method dispatch label Sep 19, 2021
@vtjnash vtjnash added the regression Regression in behavior compared to a previous version label Mar 29, 2022
vtjnash added a commit that referenced this issue Mar 29, 2022
We were accidentally passing the start of the list instead of the end of
the list, resulting in some values passing through uninitialized.

Fix #42297 regression
vtjnash added a commit that referenced this issue Apr 7, 2022
We were accidentally passing the start of the list instead of the end of
the list, resulting in some values passing through uninitialized.

Fix #42297 regression
KristofferC pushed a commit that referenced this issue Apr 19, 2022
We were accidentally passing the start of the list instead of the end of
the list, resulting in some values passing through uninitialized.

Fix #42297 regression

(cherry picked from commit 5f2abf6)
KristofferC pushed a commit that referenced this issue Apr 19, 2022
We were accidentally passing the start of the list instead of the end of
the list, resulting in some values passing through uninitialized.

Fix #42297 regression

(cherry picked from commit 5f2abf6)
KristofferC pushed a commit that referenced this issue May 16, 2022
We were accidentally passing the start of the list instead of the end of
the list, resulting in some values passing through uninitialized.

Fix #42297 regression

(cherry picked from commit 5f2abf6)
KristofferC pushed a commit that referenced this issue Dec 21, 2022
We were accidentally passing the start of the list instead of the end of
the list, resulting in some values passing through uninitialized.

Fix #42297 regression

(cherry picked from commit 5f2abf6)
staticfloat pushed a commit that referenced this issue Dec 23, 2022
We were accidentally passing the start of the list instead of the end of
the list, resulting in some values passing through uninitialized.

Fix #42297 regression

(cherry picked from commit 5f2abf6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
regression Regression in behavior compared to a previous version types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants