Skip to content
Merged
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
37 changes: 34 additions & 3 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,46 @@ function promote_operation_fallback(
::Type{S},
::Type{T},
) where {S,T}
return typeof(op(_instantiate_zero(S), _instantiate_oneunit(T)))
if isconcretetype(S) && isconcretetype(T)
return typeof(op(_instantiate_zero(S), _instantiate_oneunit(T)))
else
return promote_type(S, T)
Copy link
Member

Choose a reason for hiding this comment

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

Not covered by tests

end
end

function promote_operation_fallback(
op::typeof(/),
::Type{S},
::Type{T},
) where {S<:Integer,T<:Integer}
if isconcretetype(S) && isconcretetype(T)
return typeof(op(_instantiate_zero(S), _instantiate_oneunit(T)))
else
return promote_type(float(S), float(T))
end
end

function promote_operation_fallback(
op::F,
::Type{S},
::Type{T},
) where {F<:Function,S,T}
return typeof(op(_instantiate_zero(S), _instantiate_zero(T)))
if isconcretetype(S) && isconcretetype(T)
return typeof(op(_instantiate_zero(S), _instantiate_zero(T)))
else
return promote_type(S, T)
Copy link
Member

Choose a reason for hiding this comment

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

Not covered by tests

end
end

function promote_operation_fallback(
op::F,
args::Vararg{Type,N},
) where {F<:Function,N}
return typeof(op(_instantiate_zero.(args)...))
if all(isconcretetype, args)
return typeof(op(_instantiate_zero.(args)...))
else
return promote_type(args...)
end
end

promote_operation_fallback(::typeof(*), ::Type{T}) where {T} = T
Expand Down Expand Up @@ -103,6 +127,13 @@ function promote_operation_fallback(
)
end

function promote_operation(
::Union{typeof(real),typeof(imag)},
Copy link
Member

Choose a reason for hiding this comment

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

This should be promote_operation, it's not a fallback

::Type{Complex{T}},
) where {T}
return T
Copy link
Member

Choose a reason for hiding this comment

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

Not covered by tests

end

"""
promote_operation(op::Function, ArgsTypes::Type...)

Expand Down
12 changes: 12 additions & 0 deletions test/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ import MutableArithmetics as MA
)
@test_throws err MA.promote_operation(op, Int, Vector{Int})
end
for op in [+, -, *, /, div]
@test MA.promote_operation(op, Int, Number) == Number
@test MA.promote_operation(op, Number, Int) == Number
end
@test MA.promote_operation(/, Int, Integer) == Float64
@test MA.promote_operation(/, Integer, Integer) == Float64
@test MA.promote_operation(/, Integer, Int) == Float64
@test MA.promote_operation(gcd, Int, Integer) == Integer
@test MA.promote_operation(gcd, Integer, Integer) == Integer
@test MA.promote_operation(gcd, Integer, Int) == Integer
@test MA.promote_operation(&, Integer, Integer, Integer) == Integer
@test MA.promote_operation(&, Integer, Integer, Int) == Integer
end
@testset "add_to!! / add!!" begin
@test MA.mutability(Int, MA.add_to!!, Int, Int) isa MA.IsNotMutable
Expand Down
5 changes: 5 additions & 0 deletions test/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ Base.@irrational theodorus 1.73205080756887729353 sqrt(big(3))
MathConstants.catalan
@test MA._instantiate(typeof(theodorus)) == theodorus
end

for op in [real, imag]
@test MA.promote_operation(op, ComplexF64) == Float64
@test MA.promote_operation(op, Complex{Real}) == Real
end
end

@testset "Errors" begin
Expand Down
3 changes: 1 addition & 2 deletions test/rewrite_generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ function test_rewrite_nonconcrete_vector()
y = Vector{Union{Float64,String}}(x)
@test MA.@rewrite(x' * y, move_factors_into_sums = false) == x' * y
@test MA.@rewrite(x .+ y, move_factors_into_sums = false) == x .+ y
# Reproducing buggy behavior in MA.@rewrite.
@test_broken MA.@rewrite(x + y, move_factors_into_sums = false) == x + x
@test MA.@rewrite(x + y, move_factors_into_sums = false) == x + x
return
end

Expand Down
2 changes: 1 addition & 1 deletion test/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
include("dummy.jl")

# Allocating size for allocating a `BigInt`. Half size on 32-bit.
const BIGINT_ALLOC = @static if VERSION >= v"1.12"
const BIGINT_ALLOC = @static if VERSION >= v"1.12-beta1"
Sys.WORD_SIZE == 64 ? 72 : 36
elseif VERSION >= v"1.11"
Sys.WORD_SIZE == 64 ? 56 : 28
Expand Down
Loading