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

Infinite recursion in wrapped arrays #145

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "FiniteDifferences"
uuid = "26cc04aa-876d-5657-8c51-4c34ba976000"
version = "0.12.1"
version = "0.12.2"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
2 changes: 1 addition & 1 deletion src/to_vec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function to_vec(x::AbstractVector)
end

function to_vec(x::AbstractArray)
x_vec, from_vec = to_vec(vec(x))
x_vec, from_vec = to_vec(collect(vec(x)))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
x_vec, from_vec = to_vec(collect(vec(x)))
# `collect` to avoid recursion in wrapped arrays
x_vec, from_vec = to_vec(collect(vec(x)))


function Array_from_vec(x_vec)
return oftype(x, reshape(from_vec(x_vec), size(x)))
Expand Down
18 changes: 16 additions & 2 deletions test/to_vec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ struct FillVector <: AbstractVector{Float64}
len::Int
end

Base.size(x::FillVector) = (x.len,)
Copy link
Member Author

Choose a reason for hiding this comment

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

This has just been moved

Base.getindex(x::FillVector, n::Int) = x.x

# For testing Composite{ThreeFields}
struct ThreeFields
a
Expand All @@ -32,8 +35,14 @@ struct Nested
y::Singleton
end

Base.size(x::FillVector) = (x.len,)
Base.getindex(x::FillVector, n::Int) = x.x
# for testing wrapped arrays
struct WrappedArray{T, V<:AbstractMatrix{T}} <: AbstractMatrix{T}
a::V
end

Base.size(w::WrappedArray) = size(w.a)
Base.getindex(w::WrappedArray, i, j) = getindex(w.a, i, j)
Base.convert(::Type{WrappedArray{T, Array{T, N}}}, a::Array{T, N}) where {T, N} = WrappedArray(a)

function test_to_vec(x::T; check_inferred = true) where {T}
check_inferred && @inferred to_vec(x)
Expand Down Expand Up @@ -179,4 +188,9 @@ end
nested = Nested(ThreeFields(1.0, 2.0, "Three"), Singleton())
test_to_vec(nested; check_inferred=false) # map
end

@testset "WrappedArray" begin
wa = WrappedArray(rand(2, 3))
test_to_vec(wa)
end
end