Skip to content

Commit

Permalink
make ntuple(f, Val{-1}) throw ArgumentError
Browse files Browse the repository at this point in the history
otherwise Julia would stuck in an endless type inference loop
  • Loading branch information
alyst committed May 5, 2017
1 parent d7a5b5a commit c6f458e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
1 change: 1 addition & 0 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ ntuple(f, ::Type{Val{15}}) = (@_inline_meta; (f(1), f(2), f(3), f(4), f(5), f(6)

function ntuple(f::F, ::Type{Val{N}}) where {F,N}
Core.typeassert(N, Int)
(N >= 0) || throw(ArgumentError(string("tuple length should be ≥0, got ", N)))
_ntuple((), f, Val{N})
end

Expand Down
2 changes: 2 additions & 0 deletions test/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ end
@test @inferred(ntuple(abs2, Val{4})) == (1, 4, 9, 16)
@test @inferred(ntuple(abs2, Val{5})) == (1, 4, 9, 16, 25)
@test @inferred(ntuple(abs2, Val{6})) == (1, 4, 9, 16, 25, 36)
# issue #21697
@test_throws ArgumentError ntuple(abs2, Val{-1})

# issue #12854
@test_throws TypeError ntuple(identity, Val{1:2})
Expand Down

0 comments on commit c6f458e

Please sign in to comment.