Skip to content

Commit

Permalink
Fix aliasing bug in copy!(x, x) for x::AbstractSet and x::AbstractDict,
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellipse0934 authored and LilithHafner committed Mar 8, 2022
1 parent 37f3070 commit f5274e4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
5 changes: 4 additions & 1 deletion base/abstractdict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ empty(a::AbstractDict) = empty(a, keytype(a), valtype(a))
empty(a::AbstractDict, ::Type{V}) where {V} = empty(a, keytype(a), V) # Note: this is the form which makes sense for `Vector`.

copy(a::AbstractDict) = merge!(empty(a), a)
copy!(dst::AbstractDict, src::AbstractDict) = merge!(empty!(dst), src)
function copy!(dst::AbstractDict, src::AbstractDict)
dst === src && return dst
merge!(empty!(dst), src)
end

"""
merge!(d::AbstractDict, others::AbstractDict...)
Expand Down
5 changes: 4 additions & 1 deletion base/abstractset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
eltype(::Type{<:AbstractSet{T}}) where {T} = @isdefined(T) ? T : Any
sizehint!(s::AbstractSet, n) = nothing

copy!(dst::AbstractSet, src::AbstractSet) = union!(empty!(dst), src)
function copy!(dst::AbstractSet, src::AbstractSet)
dst === src && return dst
union!(empty!(dst), src)
end

## set operations (union, intersection, symmetric difference)

Expand Down
2 changes: 2 additions & 0 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,8 @@ end
@test s === copy!(s, Base.ImmutableDict(a[])) == Dict(a[])
end
end
s2 = copy(s)
@test copy!(s, s) == s2
end

@testset "map!(f, values(dict))" begin
Expand Down
3 changes: 3 additions & 0 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ end
@test s === copy!(s, BitSet(a)) == S(a)
end
end
s = Set([1, 2])
s2 = copy(s)
@test copy!(s, s) == s2
end

@testset "sizehint, empty" begin
Expand Down

0 comments on commit f5274e4

Please sign in to comment.