Skip to content
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

fix #23107, ensure Array constructor always makes new arrays #23490

Merged
merged 1 commit into from
Aug 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,12 @@ convert(::Type{Array{T,n}}, x::AbstractArray{S,n}) where {T,n,S} = copy!(Array{T

promote_rule(a::Type{Array{T,n}}, b::Type{Array{S,n}}) where {T,n,S} = el_same(promote_type(T,S), a, b)

# constructors should make copies
Copy link
Member

@nalimilan nalimilan Aug 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this documented somewhere? This is something I wondered when writing CategoricalArrays contructors. Since one-argument constructors fall back to convert, by default they do not make copies. I agree it would make sense for convertnot to make a copy and for constructors to make a copy, but this should be stated clearly in the manual. Then we should review other types in Base (and in our packages) to check that this rule is followed consistently (not only for arrays).

EDIT: Sorry, I had missed the discussion at #23107. I agree with the conclusion there, so this is just a request to document this behavior instead of just silently doing it. :-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll re-do the manual entries on convert and construct as part of #15120. In progress in #23273.


if module_name(@__MODULE__) === :Base # avoid method overwrite
(::Type{T})(x::T) where {T<:Array} = copy(x)
end

## copying iterators to containers

"""
Expand Down
4 changes: 4 additions & 0 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,10 @@ reinterpret(B::BitArray, dims::NTuple{N,Int}) where {N} = reshape(B, dims)

BitArray(A::AbstractArray{<:Any,N}) where {N} = convert(BitArray{N}, A)

if module_name(@__MODULE__) === :Base # avoid method overwrite
(::Type{T})(x::T) where {T<:BitArray} = copy(x)
end

"""
BitArray(itr)

Expand Down
7 changes: 7 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ using TestHelpers.OAs
@test ndims(a) == 5
@test a[2,1,2,2,1] == b[14]
@test a[2,2,2,2,2] == b[end]

# issue #23107
a = [1,2,3]
@test typeof(a)(a) !== a
@test Array(a) !== a
@test Array{eltype(a)}(a) !== a
@test Vector(a) !== a
end
@testset "reshaping SubArrays" begin
a = collect(reshape(1:5, 1, 5))
Expand Down
5 changes: 5 additions & 0 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ timesofar("utils")
@test Array(one(BitMatrix(2,2))) == eye(2,2)
@test_throws DimensionMismatch one(BitMatrix(2,3))
end

# constructors should copy
a = trues(3)
@test BitArray(a) !== a
@test BitArray{1}(a) !== a
end

timesofar("constructors")
Expand Down