Skip to content

Commit 116c2a3

Browse files
committed
More ambiguities found by Aqua
1 parent 703a19b commit 116c2a3

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/disambiguities.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,30 @@ for type in (Signed, Float64, Float32, Rational), op in (:flipsign, :copysign)
4949
return $(op)(x, ustrip(y))
5050
end
5151
end
52+
53+
function Base.:*(l::Complex{Bool}, r::AbstractRealQuantity)
54+
return new_quantity(typeof(r), l * ustrip(r), dimension(r))
55+
end
56+
function Base.:*(l::AbstractRealQuantity, r::Complex{Bool})
57+
return new_quantity(typeof(l), ustrip(l) * r, dimension(l))
58+
end
59+
60+
for op in (:(==), :isequal), base_type in (AbstractIrrational, AbstractFloat)
61+
@eval begin
62+
function Base.$(op)(l::AbstractRealQuantity, r::$base_type)
63+
return $(op)(ustrip(l), r) && iszero(dimension(l))
64+
end
65+
function Base.$(op)(l::$base_type, r::AbstractRealQuantity)
66+
return $(op)(l, ustrip(r)) && iszero(dimension(r))
67+
end
68+
end
69+
end
70+
71+
function Base.isless(l::AbstractRealQuantity, r::AbstractFloat)
72+
iszero(dimension(l)) || throw(DimensionError(l, r))
73+
return isless(ustrip(l), r)
74+
end
75+
function Base.isless(l::AbstractFloat, r::AbstractRealQuantity)
76+
iszero(dimension(r)) || throw(DimensionError(l, r))
77+
return isless(l, ustrip(r))
78+
end

src/utils.jl

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,18 @@ end
6868
# abstract number packages which may try to do the same thing.
6969
# (which would lead to ambiguities)
7070
const BASE_NUMERIC_TYPES = Union{
71-
Bool, Int8, UInt8, Int16, UInt16, Int32, UInt32,
71+
Int8, UInt8, Int16, UInt16, Int32, UInt32,
7272
Int64, UInt64, Int128, UInt128, Float16, Float32,
73-
Float64, BigFloat, BigInt, ComplexF16, ComplexF32,
73+
Float64, BigInt, ComplexF16, ComplexF32,
7474
ComplexF64, Complex{BigFloat}, Rational{Int8}, Rational{UInt8},
7575
Rational{Int16}, Rational{UInt16}, Rational{Int32}, Rational{UInt32},
7676
Rational{Int64}, Rational{UInt64}, Rational{Int128}, Rational{UInt128},
7777
Rational{BigInt},
7878
}
79+
# The following types require explicit promotion,
80+
# as putting them in a union type creates different ambiguities
81+
const AMBIGUOUS_NUMERIC_TYPES = (Bool, BigFloat)
82+
7983
for (type, _, _) in ABSTRACT_QUANTITY_TYPES
8084
@eval begin
8185
function Base.convert(::Type{Q}, x::BASE_NUMERIC_TYPES) where {T,D,Q<:$type{T,D}}
@@ -88,6 +92,19 @@ for (type, _, _) in ABSTRACT_QUANTITY_TYPES
8892
return with_type_parameters(promote_quantity(Q, T2), promote_type(T, T2), D)
8993
end
9094
end
95+
for numeric_type in AMBIGUOUS_NUMERIC_TYPES
96+
@eval begin
97+
function Base.convert(::Type{Q}, x::$numeric_type) where {T,D,Q<:$type{T,D}}
98+
return new_quantity(Q, convert(T, x), D())
99+
end
100+
function Base.promote_rule(::Type{Q}, ::Type{$numeric_type}) where {T,D,Q<:$type{T,D}}
101+
return with_type_parameters(promote_quantity(Q, $numeric_type), promote_type(T, $numeric_type), D)
102+
end
103+
function Base.promote_rule(::Type{$numeric_type}, ::Type{Q}) where {T,D,Q<:$type{T,D}}
104+
return with_type_parameters(promote_quantity(Q, $numeric_type), promote_type(T, $numeric_type), D)
105+
end
106+
end
107+
end
91108
end
92109

93110
"""

0 commit comments

Comments
 (0)