-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Throw an error when collecting from iterators with inconsistent length #29458
Open
nalimilan
wants to merge
3
commits into
master
Choose a base branch
from
nl/length
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -274,7 +274,12 @@ function copyto!(dest::Array{T}, doffs::Integer, src::Array{T}, soffs::Integer, | |
unsafe_copyto!(dest, doffs, src, soffs, n) | ||
end | ||
|
||
copyto!(dest::Array{T}, src::Array{T}) where {T} = copyto!(dest, 1, src, 1, length(src)) | ||
function _copyto_impl!(dest::Array{T}, src::Array{T}, allowshorter::Bool) where {T} | ||
if !allowshorter && length(src) < length(dest) | ||
throw(ArgumentError("source has fewer elements than destination")) | ||
end | ||
copyto!(dest, 1, src, 1, length(src)) | ||
end | ||
|
||
# N.B: The generic definition in multidimensional.jl covers, this, this is just here | ||
# for bootstrapping purposes. | ||
|
@@ -517,8 +522,10 @@ julia> collect(Float64, 1:2:5) | |
""" | ||
collect(::Type{T}, itr) where {T} = _collect(T, itr, IteratorSize(itr)) | ||
|
||
_collect(::Type{T}, itr, isz::HasLength) where {T} = copyto!(Vector{T}(undef, Int(length(itr)::Integer)), itr) | ||
_collect(::Type{T}, itr, isz::HasShape) where {T} = copyto!(similar(Array{T}, axes(itr)), itr) | ||
_collect(::Type{T}, itr, isz::HasLength) where {T} = | ||
_copyto_impl!(Vector{T}(undef, Int(length(itr)::Integer)), itr, false) | ||
_collect(::Type{T}, itr, isz::HasShape) where {T} = | ||
_copyto_impl!(similar(Array{T}, axes(itr)), itr, false) | ||
function _collect(::Type{T}, itr, isz::SizeUnknown) where T | ||
a = Vector{T}() | ||
for x in itr | ||
|
@@ -561,7 +568,7 @@ collect(A::AbstractArray) = _collect_indices(axes(A), A) | |
collect_similar(cont, itr) = _collect(cont, itr, IteratorEltype(itr), IteratorSize(itr)) | ||
|
||
_collect(cont, itr, ::HasEltype, isz::Union{HasLength,HasShape}) = | ||
copyto!(_similar_for(cont, eltype(itr), itr, isz), itr) | ||
_copyto_impl!(_similar_for(cont, eltype(itr), itr, isz), itr, false) | ||
|
||
function _collect(cont, itr, ::HasEltype, isz::SizeUnknown) | ||
a = _similar_for(cont, eltype(itr), itr, isz) | ||
|
@@ -618,6 +625,9 @@ function collect(itr::Generator) | |
else | ||
y = iterate(itr) | ||
if y === nothing | ||
if isa(isz, Union{HasLength, HasShape}) && length(itr) != 0 | ||
throw(ArgumentError("iterator returned fewer elements than its declared length")) | ||
end | ||
return _array_for(et, itr.iter, isz) | ||
end | ||
v1, st = y | ||
|
@@ -667,6 +677,11 @@ function collect_to!(dest::AbstractArray{T}, itr, offs, st) where T | |
return collect_to!(new, itr, i+1, st) | ||
end | ||
end | ||
lastidx = lastindex(dest) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should actually be |
||
i-1 < lastidx && | ||
throw(ArgumentError("iterator returned fewer elements than its declared length")) | ||
i-1 > lastidx && | ||
throw(ArgumentError("iterator returned more elements than its declared length")) | ||
return dest | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check and the one below is actually not needed for this PR since we already allocate an array of the correct length, and the
AbstractArray
interface relies on the fact that the length is correctly declared. But I added these for consistency, so that theallowshorter
argument (which needs to be accepted) isn't a no-op. I can drop it and add a comment explaining that if you want.