Skip to content

Commit

Permalink
Fix @deprecate with the new where syntax (#22034)
Browse files Browse the repository at this point in the history
* fix at-deprecate with where syntax

* tests for at-deprecate
  • Loading branch information
fredrikekre authored and KristofferC committed May 25, 2017
1 parent 4cbfd59 commit 215adb2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 13 deletions.
32 changes: 19 additions & 13 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,43 @@
# the name of the function, which is used to ensure that the deprecation warning
# is only printed the first time for each call place.

macro deprecate(old,new,ex=true)
macro deprecate(old, new, ex=true)
meta = Expr(:meta, :noinline)
@gensym oldmtname
if isa(old,Symbol)
oldname = Expr(:quote,old)
newname = Expr(:quote,new)
if isa(old, Symbol)
oldname = Expr(:quote, old)
newname = Expr(:quote, new)
Expr(:toplevel,
ex ? Expr(:export,esc(old)) : nothing,
ex ? Expr(:export, esc(old)) : nothing,
:(function $(esc(old))(args...)
$meta
depwarn(string($oldname," is deprecated, use ",$newname," instead."),
depwarn(string($oldname, " is deprecated, use ", $newname, " instead."),
$oldmtname)
$(esc(new))(args...)
end),
:(const $oldmtname = Core.Typeof($(esc(old))).name.mt.name))
elseif isa(old,Expr) && old.head == :call
elseif isa(old, Expr) && (old.head == :call || old.head == :where)
remove_linenums!(new)
oldcall = sprint(show_unquoted, old)
newcall = sprint(show_unquoted, new)
oldsym = if isa(old.args[1],Symbol)
old.args[1]::Symbol
elseif isa(old.args[1],Expr) && old.args[1].head == :curly
old.args[1].args[1]::Symbol
# if old.head is a :where, step down one level to the :call to avoid code duplication below
callexpr = old.head == :call ? old : old.args[1]
if callexpr.head == :call
if isa(callexpr.args[1], Symbol)
oldsym = callexpr.args[1]::Symbol
elseif isa(callexpr.args[1], Expr) && callexpr.args[1].head == :curly
oldsym = callexpr.args[1].args[1]::Symbol
else
error("invalid usage of @deprecate")
end
else
error("invalid usage of @deprecate")
end
Expr(:toplevel,
ex ? Expr(:export,esc(oldsym)) : nothing,
ex ? Expr(:export, esc(oldsym)) : nothing,
:($(esc(old)) = begin
$meta
depwarn(string($oldcall," is deprecated, use ",$newcall," instead."),
depwarn(string($oldcall, " is deprecated, use ", $newcall, " instead."),
$oldmtname)
$(esc(new))
end),
Expand Down
41 changes: 41 additions & 0 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -701,3 +701,44 @@ end
@test hton(0x102030405060708) == 0x807060504030201
@test ltoh(0x102030405060708) == 0x102030405060708
@test htol(0x102030405060708) == 0x102030405060708

module DeprecationTests # to test @deprecate
f() = true

# test the Symbol path of @deprecate
@deprecate f1 f
@deprecate f2 f false # test that f2 is not exported

# test the Expr path of @deprecate
@deprecate f3() f()
@deprecate f4() f() false # test that f4 is not exported
@deprecate f5(x::T) where T f()

# test deprecation of a constructor
struct A{T} end
@deprecate A{T}(x::S) where {T, S} f()
end # module

@testset "@deprecate" begin
using .DeprecationTests
# enable when issue #22043 is fixed
# @test @test_warn "f1 is deprecated, use f instead." f1()
# @test @test_nowarn f1()

# @test_throws UndefVarError f2() # not exported
# @test @test_warn "f2 is deprecated, use f instead." DeprecationTests.f2()
# @test @test_nowarn DeprecationTests.f2()

# @test @test_warn "f3() is deprecated, use f() instead." f3()
# @test @test_nowarn f3()

# @test_throws UndefVarError f4() # not exported
# @test @test_warn "f4() is deprecated, use f() instead." DeprecationTests.f4()
# @test @test_nowarn DeprecationTests.f4()

# @test @test_warn "f5(x::T) where T is deprecated, use f() instead." f5(1)
# @test @test_nowarn f5(1)

# @test @test_warn "A{T}(x::S) where {T, S} is deprecated, use f() instead." A{Int}(1.)
# @test @test_nowarn A{Int}(1.)
end

0 comments on commit 215adb2

Please sign in to comment.