Skip to content

Commit

Permalink
Deprecate ==(::Any, ::Any) fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
nalimilan committed Oct 10, 2016
1 parent 82fbd52 commit fb0f333
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 24 deletions.
6 changes: 4 additions & 2 deletions base/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ outstream(r::BasicREPL) = r.terminal

function run_frontend(repl::BasicREPL, backend::REPLBackendRef)
d = REPLDisplay(repl)
dopushdisplay = !in(d,Base.Multimedia.displays)
# FIXME: in (and therefore isequal) should work here as everywhere else, but doesn't
dopushdisplay = !any(x->x===d,Base.Multimedia.displays)
dopushdisplay && pushdisplay(d)
repl_channel, response_channel = backend.repl_channel, backend.response_channel
hit_eof = false
Expand Down Expand Up @@ -921,7 +922,8 @@ end

function run_frontend(repl::LineEditREPL, backend)
d = REPLDisplay(repl)
dopushdisplay = repl.specialdisplay === nothing && !in(d,Base.Multimedia.displays)
# FIXME: in (and therefore isequal) should work here as everywhere else, but doesn't
dopushdisplay = repl.specialdisplay === nothing && !any(x->x===d,Base.Multimedia.displays)
dopushdisplay && pushdisplay(d)
if !isdefined(repl,:interface)
interface = repl.interface = setup_interface(repl)
Expand Down
50 changes: 35 additions & 15 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,30 @@ supertype(T::DataType) = T.super

## generic comparison ##

==(x, y) = x === y
function notcomparablewarning{S, T}(x::S, y::T)
@_noinline_meta
b = IOBuffer()
print(b, "comparison of values of type $S with values of type $T using == operator are deprecated: use isequal or === instead. Found in attempt to compare ")
iolim = IOContext(b, :limit=>true)
show(iolim, x)
print(b, " with ")
show(iolim, y)
print(b, ".")
Base.depwarn(takebuf_string(b), :(==))
end

function ==(x::ANY, y::ANY)
@_inline_meta
@boundscheck notcomparablewarning(x, y)
x === y
end

=={T}(x::T, y::T) = x === y
==(x::Union{Symbol, Expr, QuoteNode, GlobalRef},
y::Union{Symbol, Expr, QuoteNode, GlobalRef}) =
x === y
# FIXME: why does removing this make bootstrap fail?
==(x::Function, y::Function) = x === y

"""
isequal(x, y)
Expand All @@ -48,26 +71,23 @@ Scalar types generally do not need to implement `isequal` separate from `==`, un
represent floating-point numbers amenable to a more efficient implementation than that
provided as a generic fallback (based on `isnan`, `signbit`, and `==`).
"""
isequal(x, y) = x == y
function isequal(x, y)
@inbounds res = x == y
res
end

# FIXME: Required since @inbounds does seem to work during early bootstrap
isequal(::Void, ::Void) = true
isequal(::Void, ::Any) = false
isequal(::Any, ::Void) = false
isequal(x::Union{Method, TypeName}, y::Union{Method, TypeName}) = x === y
isequal(x::Union{Module, Symbol}, y::Union{Module, Symbol}) = x === y

# TODO: these can be deleted once the deprecations of ==(x::Char, y::Integer) and
# ==(x::Integer, y::Char) are gone and the above returns false anyway
isequal(x::Char, y::Integer) = false
isequal(x::Integer, y::Char) = false

## minimally-invasive changes to test == causing NotComparableError
# export NotComparableError
# =={T}(x::T, y::T) = x === y
# immutable NotComparableError <: Exception end
# const NotComparable = NotComparableError()
# ==(x::ANY, y::ANY) = NotComparable
# !(e::NotComparableError) = throw(e)
# isequal(x, y) = (x == y) === true

## alternative NotComparableError which captures context
# immutable NotComparableError; a; b; end
# ==(x::ANY, y::ANY) = NotComparableError(x, y)

isequal(x::AbstractFloat, y::AbstractFloat) = (isnan(x) & isnan(y)) | (signbit(x) == signbit(y)) & (x == y)
isequal(x::Real, y::AbstractFloat) = (isnan(x) & isnan(y)) | (signbit(x) == signbit(y)) & (x == y)
isequal(x::AbstractFloat, y::Real ) = (isnan(x) & isnan(y)) | (signbit(x) == signbit(y)) & (x == y)
Expand Down
2 changes: 1 addition & 1 deletion test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ end
@test !(Type{Tuple{Int,Int}} <: Tuple)
@test Tuple{Type{Int}} <: Tuple{DataType}

@test () != Type{Tuple{}}
@test !isequal((), Type{Tuple{}})

# issue #6561
@test issubtype(Array{Tuple}, Array{NTuple})
Expand Down
8 changes: 4 additions & 4 deletions test/dates/periods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using Base.Test
@test Dates.Year(1) > Dates.Year(0)
@test (Dates.Year(1) < Dates.Year(0)) == false
@test Dates.Year(1) == Dates.Year(1)
@test Dates.Year(1) != 1
@test !isequal(Dates.Year(1), 1)
@test Dates.Year(1) + Dates.Year(1) == Dates.Year(2)
@test Dates.Year(1) - Dates.Year(1) == zero(Dates.Year)
@test_throws MethodError Dates.Year(1) * Dates.Year(1) == Dates.Year(1)
Expand Down Expand Up @@ -172,7 +172,7 @@ y2 = Dates.Year(2)
@test y*10 % Dates.Year(5) == Dates.Year(0)
@test_throws MethodError (y > 3) == false
@test_throws MethodError (4 < y) == false
@test 1 != y
@test !isequal(1, y)
t = [y,y,y,y,y]
@test t .+ Dates.Year(2) == [Dates.Year(3),Dates.Year(3),Dates.Year(3),Dates.Year(3),Dates.Year(3)]

Expand Down Expand Up @@ -233,8 +233,8 @@ test = ((((((((dt + y) - m) + w) - d) + h) - mi) + s) - ms)
@test Dates.Year(-1) < Dates.Year(1)
@test !(Dates.Year(-1) > Dates.Year(1))
@test Dates.Year(1) == Dates.Year(1)
@test Dates.Year(1) != 1
@test 1 != Dates.Year(1)
@test !isequal(Dates.Year(1), 1)
@test !isequal(1, Dates.Year(1))
@test Dates.Month(-1) < Dates.Month(1)
@test !(Dates.Month(-1) > Dates.Month(1))
@test Dates.Month(1) == Dates.Month(1)
Expand Down
3 changes: 3 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@ end

# issue #12960
type T12960 end
# For x == 0 test in sparse matrix assignment
Base.:(==)(x::T12960, y::Integer) = false
Base.:(==)(x::Integer, y::T12960) = false
let
A = speye(3)
B = similar(A, T12960)
Expand Down
4 changes: 2 additions & 2 deletions test/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ end
@test true
@test false
@test 1 == 1
@test 2 == :foo
@test 2 == 100
@test 3 == 3
@testset "d" begin
@test 4 == 4
Expand All @@ -122,7 +122,7 @@ end
@testset "inner1" begin
@test 1 == 1
@test 2 == 2
@test 3 == :bar
@test 3 == 5.0
@test 4 == 4
@test_throws ErrorException 1+1
@test_throws ErrorException error()
Expand Down

0 comments on commit fb0f333

Please sign in to comment.