Skip to content

Commit a19569d

Browse files
authored
ntuple: ensure eltype is always Int (#55901)
Fixes #55790
1 parent 9e92a9d commit a19569d

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

base/ntuple.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ julia> ntuple(i -> 2*i, 4)
1414
(2, 4, 6, 8)
1515
```
1616
"""
17-
@inline function ntuple(f::F, n::Integer) where F
17+
@inline function ntuple(f::F, n::Int) where F
1818
# marked inline since this benefits from constant propagation of `n`
1919
t = n == 0 ? () :
2020
n == 1 ? (f(1),) :
@@ -30,8 +30,10 @@ julia> ntuple(i -> 2*i, 4)
3030
_ntuple(f, n)
3131
return t
3232
end
33+
ntuple(f::F, n::Integer) where F = ntuple(f, convert(Int, n)::Int)
3334

34-
function _ntuple(f::F, n) where F
35+
# `n` should always be an Int (#55790)
36+
function _ntuple(f::F, n::Int) where F
3537
@noinline
3638
(n >= 0) || throw(ArgumentError(LazyString("tuple length should be ≥ 0, got ", n)))
3739
([f(i) for i = 1:n]...,)

test/tuple.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,11 @@ end
534534
end
535535

536536
@test Base.infer_return_type(ntuple, Tuple{typeof(identity), Val}) == Tuple{Vararg{Int}}
537+
538+
# issue #55790
539+
for n in 1:32
540+
@test typeof(ntuple(identity, UInt64(n))) == NTuple{n, Int}
541+
end
537542
end
538543

539544
struct A_15703{N}

0 commit comments

Comments
 (0)