From fb0f333ddd961b09bc30d8d2e028c260cf9a48ff Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Sun, 9 Oct 2016 16:29:59 +0200 Subject: [PATCH] Deprecate ==(::Any, ::Any) fallback --- base/REPL.jl | 6 ++++-- base/operators.jl | 50 ++++++++++++++++++++++++++++++------------- test/core.jl | 2 +- test/dates/periods.jl | 8 +++---- test/show.jl | 3 +++ test/test.jl | 4 ++-- 6 files changed, 49 insertions(+), 24 deletions(-) diff --git a/base/REPL.jl b/base/REPL.jl index 5b618505f84ef..4500e3995502a 100644 --- a/base/REPL.jl +++ b/base/REPL.jl @@ -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 @@ -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) diff --git a/base/operators.jl b/base/operators.jl index 9ce58c1f98cf3..8f372d9f16351 100644 --- a/base/operators.jl +++ b/base/operators.jl @@ -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) @@ -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) diff --git a/test/core.jl b/test/core.jl index a62b04bf9b6c9..8ea4c563d119b 100644 --- a/test/core.jl +++ b/test/core.jl @@ -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}) diff --git a/test/dates/periods.jl b/test/dates/periods.jl index e27bef7efd98f..83b459ccd7963 100644 --- a/test/dates/periods.jl +++ b/test/dates/periods.jl @@ -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) @@ -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)] @@ -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) diff --git a/test/show.jl b/test/show.jl index ee16911f07854..db33f57818e50 100644 --- a/test/show.jl +++ b/test/show.jl @@ -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) diff --git a/test/test.jl b/test/test.jl index 4368b7f49e76a..420924b5fb236 100644 --- a/test/test.jl +++ b/test/test.jl @@ -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 @@ -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()