Skip to content

copy!(::AbstractSet, ::AbstractSet) behaves incorrectly under aliasing (also AbstractDict) #41268

Closed
@yurivish

Description

@yurivish

Mutating copy on abstract sets is implemented as union!(empty!(dst), src):

copy!(dst::AbstractSet, src::AbstractSet) = union!(empty!(dst), src)

This fails when copying a Set to itself — arguments are evaluated from left to right and the set is emptied and returned.

julia> s = Set([1, 2, 3])
Set{Int64} with 3 elements:
  2
  3
  1
julia> copy!(s, s)
Set{Int64}()

The same behavior exists for Dicts:

copy!(dst::AbstractDict, src::AbstractDict) = merge!(empty!(dst), src)

julia> d = Dict(1 => 2, 3 => 4)
Dict{Int64, Int64} with 2 entries:
  3 => 4
  1 => 2

julia> copy!(d, d)
Dict{Int64, Int64}()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions