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

type normalization: Union{Tuple{}, Tuple{Int64, Vararg{Int64, N}} where {N}} into Tuple{Vararg{Int64}}? #56732

Open
nsajko opened this issue Dec 2, 2024 · 1 comment
Labels
feature Indicates new feature / enhancement requests types and dispatch Types, subtyping and method dispatch

Comments

@nsajko
Copy link
Contributor

nsajko commented Dec 2, 2024

PR #55123 introduced normalization of types like Union{Tuple{}, Tuple{Int64, Vararg{Int64}}} into Tuple{Vararg{Int64}}. The motivation was making subtyping more precise, helping inference, xref #54746.

This was great, however it seems incomplete: in the above Union, replacing Tuple{Int64, Vararg{Int64}} with the equal type Tuple{Int64, Vararg{Int64, N}} where {N} prevents this normalization from occuring, also resulting in some unexpected subtyping behavior:

julia> const A = Tuple{Int64, Vararg{Int64}}
Tuple{Int64, Vararg{Int64}}

julia> const B = Tuple{Int64, Vararg{Int64, N}} where {N}
Tuple{Int64, Vararg{Int64, N}} where N

julia> f(t) = Union{Tuple{}, t}
f (generic function with 1 method)

julia> A == B
true

julia> f(A) == f(B)
false

julia> A <: B
true

julia> f(A) <: f(B)
false

This results in bad inference in PR #56731 (buildkite CI link):

  Expression: Base.infer_return_type(sort, Tuple{Tuple{Vararg{Int}}}) == Tuple{Vararg{Int}}
   Evaluated: Union{Tuple{}, Tuple{Int64, Vararg{Int64, N}} where N} == Tuple{Vararg{Int64}}

I think one way of fixing it would be to normalize Tuple{Int64, Vararg{Int64, N}} where {N} into Tuple{Int64, Vararg{Int64}}. Does that sound doable? Keno explains this can't work.

@nsajko nsajko added feature Indicates new feature / enhancement requests types and dispatch Types, subtyping and method dispatch labels Dec 2, 2024
@Keno
Copy link
Member

Keno commented Dec 3, 2024

The two are equal as types, but they're not equivalent under apply_type, so we can't normalize on construction. We could potentially normalize inside of Unions, which would still address the case you care about

@nsajko nsajko changed the title type normalization: Tuple{Int64, Vararg{Int64, N}} where {N} into Tuple{Int64, Vararg{Int64}}? type normalization: Union{Tuple{}, Tuple{Int64, Vararg{Int64, N}} where {N}} into Tuple{Vararg{Int64}}? Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Indicates new feature / enhancement requests types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

No branches or pull requests

2 participants