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

inference: restrict type of type parameter from Vararg #51449

Merged
merged 1 commit into from
Sep 26, 2023

Conversation

aviatesk
Copy link
Member

@aviatesk aviatesk commented Sep 25, 2023

Inference has been able to restrict type of Vararg type parameter N to Int for cases like func(..., ::Vararg{T,N}) where {T,N}, but this refinement was not available for signatures like func(::Tuple{Vararg{T,N}}) where {T,N}.

This commit allows the later case to be inferred as well. Now the following kind of case will be inferred, e.g.:

julia> function sub2ind_gen_fallback(dims::NTuple{N,Int}, I) where N # N is knonw to be ::Int
           ind = I[N] - 1
           for i = (N - 1):-1:1
               ind = I[i] - 1 + dims[i]*ind
           end
           return ind + 1
       end;

julia> only(Base.return_types(sub2ind_gen_fallback, (NTuple,Tuple{Vararg{Int}})))
Int64

Inference has been able to restrict type of `Vararg` type parameter `N`
to `Int` for cases like `func(..., ::Vararg{T,N}) where {T,N}`,
but this refinement was not available for signatures like
`func(::Tuple{Vararg{T,N}}) where {T,N}`.

This commit allows the later case to be inferred as well. Now the
following kind of case will be inferred, e.g.:
```julia
julia> function sub2ind_gen_fallback(dims::NTuple{N,Int}, I) where N
           ind = I[N] - 1
           for i = (N - 1):-1:1
               ind = I[i] - 1 + dims[i]*ind
           end
           return ind + 1
       end;

julia> only(Base.return_types(sub2ind_gen_fallback, (NTuple,Tuple{Int,Int})))
Int64
```
@aviatesk
Copy link
Member Author

@nanosoldier runbenchmarks("inference", vs=":master")

@aviatesk aviatesk requested a review from vtjnash September 26, 2023 04:35
@nanosoldier
Copy link
Collaborator

Your benchmark job has completed - no performance regressions were detected. A full report can be found here.

@vtjnash vtjnash merged commit 8de80bd into master Sep 26, 2023
@vtjnash vtjnash deleted the avi/vatuple-constraint branch September 26, 2023 13:55
@simeonschaub
Copy link
Member

fixes #37316?

@aviatesk
Copy link
Member Author

Yeah, I didn't know there has
been an issue for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:inference Type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants