Skip to content

Commit

Permalink
add a fallback copy method for isbits values
Browse files Browse the repository at this point in the history
  • Loading branch information
nsajko committed Apr 3, 2024
1 parent a69aa30 commit fcdd5e3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions base/promotion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -652,3 +652,8 @@ min(x::T, y::T) where {T<:Real} = ifelse(y < x, y, x)
minmax(x::T, y::T) where {T<:Real} = y < x ? (y, x) : (x, y)

flipsign(x::T, y::T) where {T<:Signed} = no_op_err("flipsign", T)

function copy(x::T) where {T}
isbits(x) || throw(MethodError(copy, (x,)))
x
end
16 changes: 16 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8132,3 +8132,19 @@ macro macroception()
end

@test (@macroception()) === 1

struct NewIsBitsType
a::Int
b::Float64
end

mutable struct NewMutableType end

@testset "`isbits`` fallback for `copy`" begin
let x = NewMutableType()
@test_throws MethodError copy(x)
end
@testset "x: $x" for x (nothing, missing, NewIsBitsType(3, 0.3))
@test x === copy(x)
end
end

0 comments on commit fcdd5e3

Please sign in to comment.