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

ntuple: ensure eltype is always Int #55901

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions base/ntuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ julia> ntuple(i -> 2*i, 4)
(2, 4, 6, 8)
```
"""
@inline function ntuple(f::F, n::Integer) where F
@inline function ntuple(f::F, n::Int) where F
# marked inline since this benefits from constant propagation of `n`
t = n == 0 ? () :
n == 1 ? (f(1),) :
Expand All @@ -30,8 +30,10 @@ julia> ntuple(i -> 2*i, 4)
_ntuple(f, n)
return t
end
ntuple(f::F, n::Integer) where F = ntuple(f, convert(Int, n)::Int)

function _ntuple(f::F, n) where F
# `n` should always be an Int (#55790)
function _ntuple(f::F, n::Int) where F
@noinline
(n >= 0) || throw(ArgumentError(LazyString("tuple length should be ≥ 0, got ", n)))
([f(i) for i = 1:n]...,)
Expand Down
5 changes: 5 additions & 0 deletions test/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,11 @@ end
end

@test Base.infer_return_type(ntuple, Tuple{typeof(identity), Val}) == Tuple{Vararg{Int}}

# issue #55790
for n in 1:32
@test typeof(ntuple(identity, UInt64(n))) == NTuple{n, Int}
end
end

struct A_15703{N}
Expand Down