Skip to content

Commit

Permalink
ntuple(f, -1) throws ArgumentError
Browse files Browse the repository at this point in the history
  • Loading branch information
alyst committed May 7, 2017
1 parent 78e835e commit 31975bf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Breaking changes

This section lists changes that do not have deprecation warnings.

* `ntuple(f, n::Integer)` throws `ArgumentError` if `n` is negative.
Previously an empty tuple was returned ([#21697]).

Library improvements
--------------------

Expand Down
8 changes: 6 additions & 2 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ julia> ntuple(i -> 2*i, 4)
```
"""
function ntuple(f::F, n::Integer) where F
t = n <= 0 ? () :
t = n == 0 ? () :
n == 1 ? (f(1),) :
n == 2 ? (f(1), f(2)) :
n == 3 ? (f(1), f(2), f(3)) :
Expand All @@ -120,7 +120,11 @@ function ntuple(f::F, n::Integer) where F
return t
end

_ntuple(f, n) = (@_noinline_meta; ([f(i) for i = 1:n]...))
function _ntuple(f, n)
@_noinline_meta
(n >= 0) || throw(ArgumentError(string("tuple length should be ≥0, got ", n)))
([f(i) for i = 1:n]...)
end

# inferrable ntuple
ntuple(f, ::Type{Val{0}}) = (@_inline_meta; ())
Expand Down
2 changes: 2 additions & 0 deletions test/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ for n = 0:20
@test t[i] == i
end
end
# issue #21697
@test_throws ArgumentError ntuple(identity, -1)

# issue #19719
@test_throws BoundsError (1,2,3)[falses(4)]
Expand Down

0 comments on commit 31975bf

Please sign in to comment.