Skip to content

Commit dfbcc45

Browse files
authored
ensure all isequal methods to be inferred to return Bool (#49800)
This would help inference on `Core.Compiler.return_type(isequal, tt)` when `tt` is not well inferred (e.g. `tt` is inferred to `Tuple{Any,Any}`). (although #46810 may disable this `Core.Compiler.return_type` improvement for good reasons). Anyway, it is explicitly stated in the documentation that the `isequal` method should always return a value of `Bool`. So, not only does this annotation assist inference, it also serves to ensure the correctness of our code base, and therefore should be beneficial. We may need to take similar measures for `isless` and `isgreater` (in separate PRs).
1 parent 909c57f commit dfbcc45

File tree

5 files changed

+7
-7
lines changed

5 files changed

+7
-7
lines changed

base/complex.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,9 @@ bswap(z::Complex) = Complex(bswap(real(z)), bswap(imag(z)))
245245
==(z::Complex, x::Real) = isreal(z) && real(z) == x
246246
==(x::Real, z::Complex) = isreal(z) && real(z) == x
247247

248-
isequal(z::Complex, w::Complex) = isequal(real(z),real(w)) & isequal(imag(z),imag(w))
249-
isequal(z::Complex, w::Real) = isequal(real(z),w) & isequal(imag(z),zero(w))
250-
isequal(z::Real, w::Complex) = isequal(z,real(w)) & isequal(zero(z),imag(w))
248+
isequal(z::Complex, w::Complex) = isequal(real(z),real(w))::Bool & isequal(imag(z),imag(w))::Bool
249+
isequal(z::Complex, w::Real) = isequal(real(z),w)::Bool & isequal(imag(z),zero(w))::Bool
250+
isequal(z::Real, w::Complex) = isequal(z,real(w))::Bool & isequal(zero(z),imag(w))::Bool
251251

252252
in(x::Complex, r::AbstractRange{<:Real}) = isreal(x) && real(x) in r
253253

base/operators.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ also implement [`<`](@ref) to ensure consistency of comparisons.
7979
==
8080

8181
"""
82-
isequal(x, y)
82+
isequal(x, y) -> Bool
8383
8484
Similar to [`==`](@ref), except for the treatment of floating point numbers
8585
and of missing values. `isequal` treats all floating-point `NaN` values as equal

base/pair.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ indexed_iterate(p::Pair, i::Int, state=1) = (getfield(p, i), i + 1)
4444
hash(p::Pair, h::UInt) = hash(p.second, hash(p.first, h))
4545

4646
==(p::Pair, q::Pair) = (p.first==q.first) & (p.second==q.second)
47-
isequal(p::Pair, q::Pair) = isequal(p.first,q.first) & isequal(p.second,q.second)
47+
isequal(p::Pair, q::Pair) = isequal(p.first,q.first)::Bool & isequal(p.second,q.second)::Bool
4848

4949
isless(p::Pair, q::Pair) = ifelse(!isequal(p.first,q.first), isless(p.first,q.first),
5050
isless(p.second,q.second))

test/missing.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ end
8080
@test isapprox(missing, 1.0, atol=1e-6) === missing
8181
@test isapprox(1.0, missing, rtol=1e-6) === missing
8282

83-
@test !any(T -> T === Union{Missing,Bool}, Base.return_types(isequal, Tuple{Any,Any}))
83+
@test all(==(Bool), Base.return_types(isequal, Tuple{Any,Any}))
8484
end
8585

8686
@testset "arithmetic operators" begin

test/testhelpers/Furlongs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ for op in (:+, :-)
7474
end
7575
end
7676
for op in (:(==), :(!=), :<, :<=, :isless, :isequal)
77-
@eval $op(x::Furlong{p}, y::Furlong{p}) where {p} = $op(x.val, y.val)
77+
@eval $op(x::Furlong{p}, y::Furlong{p}) where {p} = $op(x.val, y.val)::Bool
7878
end
7979
for (f,op) in ((:_plus,:+),(:_minus,:-),(:_times,:*),(:_div,://))
8080
@eval function $f(v::T, ::Furlong{p}, ::Union{Furlong{q},Val{q}}) where {T,p,q}

0 commit comments

Comments
 (0)