Skip to content

Problem with generic copyto! #45125

@bkamins

Description

@bkamins

The issue is:

julia> x = view(["A", "B"], 1:2)
2-element view(::Vector{String}, 1:2) with eltype String:
 "A"
 "B"

julia> y = Vector{String}(undef, 2)
2-element Vector{String}:
 #undef
 #undef

julia> copyto!(x, y)
ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] getindex
   @ .\array.jl:861 [inlined]
 [2] copyto_unaliased!(deststyle::IndexLinear, dest::SubArray{String, 1, Vector{String}, Tuple{UnitRange{Int64}}, true}, srcstyle::IndexLinear, src::Vector{String})
   @ Base .\abstractarray.jl:1018
 [3] copyto!(dest::SubArray{String, 1, Vector{String}, Tuple{UnitRange{Int64}}, true}, src::Vector{String})
   @ Base .\abstractarray.jl:998
 [4] top-level scope
   @ REPL[19]:1

The reason is

dest[i + Δi] = src[i]
(and similar operation below this one) assuming that all elements of source array are defined.

If I understand the intention correctly this is a bug in copyto! as e.g.

julia> x = ["A", "b"]
2-element Vector{String}:
 "A"
 "b"

julia> y = Vector{String}(undef, 2)
2-element Vector{String}:
 #undef
 #undef

julia> copyto!(x, y)
2-element Vector{String}:
 #undef
 #undef

julia> x
2-element Vector{String}:
 #undef
 #undef

works.

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrays[a, r, r, a, y, s]

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions