Skip to content

Commit

Permalink
ntuple: ensure eltype is always Int
Browse files Browse the repository at this point in the history
Fixes #55790
  • Loading branch information
barucden committed Sep 27, 2024
1 parent 6e33dfb commit ee78487
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 3 additions & 2 deletions base/ntuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ julia> ntuple(i -> 2*i, 4)
n == 8 ? (f(1), f(2), f(3), f(4), f(5), f(6), f(7), f(8)) :
n == 9 ? (f(1), f(2), f(3), f(4), f(5), f(6), f(7), f(8), f(9)) :
n == 10 ? (f(1), f(2), f(3), f(4), f(5), f(6), f(7), f(8), f(9), f(10)) :
_ntuple(f, n)
_ntuple(f, Int(n))
return t
end

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, Int64}
end
end

struct A_15703{N}
Expand Down

0 comments on commit ee78487

Please sign in to comment.