Skip to content

Commit ffac475

Browse files
authored
improve the inferrability of isequal(::Any, ::Any) (#42195)
Currently, the return type of `isequal(::Any, ::Any)` is inferred as `Union{Bool,Missing}` because it takes the possibilities of e.g. `==(::Any, ::Missing)` into account. But actually `isequal` already handles every `missing` case by dispatch: <https://github.com/JuliaLang/julia/blob/2f00c5f6e2211ed1588976dbbe7b022148716d95/base/missing.jl#L80-L82> , and we can improve the inferrability by type annotation.
1 parent d7028da commit ffac475

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

base/operators.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ julia> isequal(missing, missing)
137137
true
138138
```
139139
"""
140-
isequal(x, y) = x == y
140+
isequal(x, y) = (x == y)::Bool # all `missing` cases are handled in missing.jl
141141

142142
signequal(x, y) = signbit(x)::Bool == signbit(y)::Bool
143143
signless(x, y) = signbit(x)::Bool & !signbit(y)::Bool

test/operators.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ import Base.<
8181
@test isequal(minmax(TO23094(2), TO23094(1))[1], TO23094(1))
8282
@test isequal(minmax(TO23094(2), TO23094(1))[2], TO23094(2))
8383

84+
let m = Module()
85+
@eval m begin
86+
struct Foo end
87+
foo(xs) = isequal(xs[1], Foo())
88+
end
89+
@test !(@inferred m.foo(Any[42]))
90+
end
91+
8492
@test isless('a','b')
8593

8694
@testset "isgreater" begin

0 commit comments

Comments
 (0)