Skip to content

Commit cb78b63

Browse files
fredrikekretkelman
authored andcommitted
Fix @deprecate with the new where syntax (#22034)
* fix at-deprecate with where syntax * tests for at-deprecate (cherry picked from commit 215adb2)
1 parent 7612502 commit cb78b63

File tree

2 files changed

+60
-13
lines changed

2 files changed

+60
-13
lines changed

base/deprecated.jl

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,43 @@
1818
# the name of the function, which is used to ensure that the deprecation warning
1919
# is only printed the first time for each call place.
2020

21-
macro deprecate(old,new,ex=true)
21+
macro deprecate(old, new, ex=true)
2222
meta = Expr(:meta, :noinline)
2323
@gensym oldmtname
24-
if isa(old,Symbol)
25-
oldname = Expr(:quote,old)
26-
newname = Expr(:quote,new)
24+
if isa(old, Symbol)
25+
oldname = Expr(:quote, old)
26+
newname = Expr(:quote, new)
2727
Expr(:toplevel,
28-
ex ? Expr(:export,esc(old)) : nothing,
28+
ex ? Expr(:export, esc(old)) : nothing,
2929
:(function $(esc(old))(args...)
3030
$meta
31-
depwarn(string($oldname," is deprecated, use ",$newname," instead."),
31+
depwarn(string($oldname, " is deprecated, use ", $newname, " instead."),
3232
$oldmtname)
3333
$(esc(new))(args...)
3434
end),
3535
:(const $oldmtname = Core.Typeof($(esc(old))).name.mt.name))
36-
elseif isa(old,Expr) && old.head == :call
36+
elseif isa(old, Expr) && (old.head == :call || old.head == :where)
3737
remove_linenums!(new)
3838
oldcall = sprint(show_unquoted, old)
3939
newcall = sprint(show_unquoted, new)
40-
oldsym = if isa(old.args[1],Symbol)
41-
old.args[1]::Symbol
42-
elseif isa(old.args[1],Expr) && old.args[1].head == :curly
43-
old.args[1].args[1]::Symbol
40+
# if old.head is a :where, step down one level to the :call to avoid code duplication below
41+
callexpr = old.head == :call ? old : old.args[1]
42+
if callexpr.head == :call
43+
if isa(callexpr.args[1], Symbol)
44+
oldsym = callexpr.args[1]::Symbol
45+
elseif isa(callexpr.args[1], Expr) && callexpr.args[1].head == :curly
46+
oldsym = callexpr.args[1].args[1]::Symbol
47+
else
48+
error("invalid usage of @deprecate")
49+
end
4450
else
4551
error("invalid usage of @deprecate")
4652
end
4753
Expr(:toplevel,
48-
ex ? Expr(:export,esc(oldsym)) : nothing,
54+
ex ? Expr(:export, esc(oldsym)) : nothing,
4955
:($(esc(old)) = begin
5056
$meta
51-
depwarn(string($oldcall," is deprecated, use ",$newcall," instead."),
57+
depwarn(string($oldcall, " is deprecated, use ", $newcall, " instead."),
5258
$oldmtname)
5359
$(esc(new))
5460
end),

test/misc.jl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,3 +701,44 @@ end
701701
@test hton(0x102030405060708) == 0x807060504030201
702702
@test ltoh(0x102030405060708) == 0x102030405060708
703703
@test htol(0x102030405060708) == 0x102030405060708
704+
705+
module DeprecationTests # to test @deprecate
706+
f() = true
707+
708+
# test the Symbol path of @deprecate
709+
@deprecate f1 f
710+
@deprecate f2 f false # test that f2 is not exported
711+
712+
# test the Expr path of @deprecate
713+
@deprecate f3() f()
714+
@deprecate f4() f() false # test that f4 is not exported
715+
@deprecate f5(x::T) where T f()
716+
717+
# test deprecation of a constructor
718+
struct A{T} end
719+
@deprecate A{T}(x::S) where {T, S} f()
720+
end # module
721+
722+
@testset "@deprecate" begin
723+
using .DeprecationTests
724+
# enable when issue #22043 is fixed
725+
# @test @test_warn "f1 is deprecated, use f instead." f1()
726+
# @test @test_nowarn f1()
727+
728+
# @test_throws UndefVarError f2() # not exported
729+
# @test @test_warn "f2 is deprecated, use f instead." DeprecationTests.f2()
730+
# @test @test_nowarn DeprecationTests.f2()
731+
732+
# @test @test_warn "f3() is deprecated, use f() instead." f3()
733+
# @test @test_nowarn f3()
734+
735+
# @test_throws UndefVarError f4() # not exported
736+
# @test @test_warn "f4() is deprecated, use f() instead." DeprecationTests.f4()
737+
# @test @test_nowarn DeprecationTests.f4()
738+
739+
# @test @test_warn "f5(x::T) where T is deprecated, use f() instead." f5(1)
740+
# @test @test_nowarn f5(1)
741+
742+
# @test @test_warn "A{T}(x::S) where {T, S} is deprecated, use f() instead." A{Int}(1.)
743+
# @test @test_nowarn A{Int}(1.)
744+
end

0 commit comments

Comments
 (0)