JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.41k stars 5.45k forks source link

Regression: failure of TypeVar in recursive struct with `NTuple` field #55189

Closed MilesCranmer closed 1 month ago

MilesCranmer commented 1 month ago

When using NTuple in a recursive type, there is a failure of TypeVars to apply:

julia> struct A{N}
           children::NTuple{N,A{N}}
       end

julia> fieldtypes(A{2})
(Tuple{A, A},)

However, this should be Tuple{A{2}, A{2}}.

Consider NTuple by itself:

julia> NTuple{2,Array{Float64,2}}
Tuple{Matrix{Float64}, Matrix{Float64}}

You can use this in structs if it is not recursive:

julia> struct A{N}
           children::NTuple{N,Array{Float64,N}}
       end

julia> fieldtypes(A{2})
(Tuple{Matrix{Float64}, Matrix{Float64}},)

However, if the type is recursive, the typevar fails to apply in the type parameters.

versioninfo():

Julia Version 1.10.4
Commit 48d4fd48430 (2024-06-04 10:41 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 8 × Apple M1 Pro
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
Threads: 6 default, 0 interactive, 3 GC (on 6 virtual cores)

Another potentially related failure for NTuple field types is #55076.


While this fails on 1.10.4, this works on 1.6.7.

MilesCranmer commented 1 month ago

This works on 1.6.7, so seems to be a regression:

julia> struct A{N}
           children::NTuple{N,A{N}}
       end

julia> fieldtypes(A{2})
(Tuple{A{2}, A{2}},)

On 1.7.3 it seems to infinitely hang.

Possibly also caused by https://github.com/JuliaLang/julia/pull/43306?