Skip to content

Allow more circular type definitions #31828

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

Closed
wants to merge 1 commit into from
Closed

Conversation

martinholters
Copy link
Member

Checking jl_types_equal against a potential wrapper when trying to normalize type parameters can result in an infinite recursion for circular type definitions. E.g.:

julia> struct T22624{A,B,C}; v::Vector{T22624{Int64,A}}; end # ok and tested for

julia> struct T22624b{A,B,C}; v::Vector{T22624b{Integer,A}}; end
ERROR: StackOverflowError:

Notably, the former only works due to an invalid fast-path, making it also recurse infinitely in #25796 and #31807 (without appropriate countermeasures). The culprit is that jl_types_equal needs to rename_unionall here, which tries to re-instantiate the same type (with another typevar), which comes to same test again. The obvious counter-measure is to avoid jl_types_equal and instead use a heuristic that may miss opportunities for normalization, but will not lead to recursion. As a starter, I'm using obviously_egal here, which fixes above issue, but may be too restrictive, e.g.

julia> Ref{Array{T,N} where {N,T}}
Ref{Array} # master
Ref{Array{T,N} where T where N} # using obviously_egal

Tests still pass, but we probably miss some optimization opportunities this way. Though I have no idea how much impact this actually has, so it might even be ok like this.

Discussion of this started in #25796, but this issue is somewhat independent, so for the sake of more focused discussions, I'm opening this draft PR.

Checking `jl_type_equal` against a potential wrapper when trying to 
normalize type parameters can result in an infinite recursion for 
circular type definitions.
@fingolfin
Copy link
Member

The example in the PR description has been working since at least Julia 1.2. So I think it is OK to close this PR now

@fingolfin fingolfin closed this Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants