Skip to content

Commit

Permalink
look up constructors by type name in depwarn. fixes JuliaLang#21972
Browse files Browse the repository at this point in the history
add deprecation_exec.jl for testing deprecations with other `--depwarn` settings
  • Loading branch information
JeffBezanson committed Jul 17, 2017
1 parent 72ecb44 commit b1c196e
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 53 deletions.
9 changes: 9 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ function firstcaller(bt::Array{Ptr{Void},1}, funcsyms)
end
found && @goto found
found = lkup.func in funcsyms
# look for constructor type name
if !found && !isnull(lkup.linfo)
li = get(lkup.linfo)
ft = ccall(:jl_first_argument_datatype, Any, (Any,), li.def.sig)
if isa(ft,DataType) && ft.name === Type.body.name
ft = unwrap_unionall(ft.parameters[1])
found = (isa(ft,DataType) && ft.name.name in funcsyms)
end
end
end
end
return StackTraces.UNKNOWN
Expand Down
69 changes: 69 additions & 0 deletions test/deprecation_exec.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Base.Test

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()

# test that @deprecate_moved can be overridden by an import
Base.@deprecate_moved foo1234 "Foo"
Base.@deprecate_moved bar "Bar" false
end # module
module Foo1234
export foo1234
foo1234(x) = x+1
end

# issue #21972
struct T21972
@noinline function T21972()
Base.depwarn("something", :T21972)
new()
end
end

@testset "@deprecate" begin
using .DeprecationTests
using .Foo1234
@test foo1234(3) == 4
@test_throws ErrorException DeprecationTests.bar(3)

# 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.)

# issue #21972
@noinline function f21972()
T21972()
end
@test_warn "deprecated" f21972()
@test_nowarn f21972()
end
63 changes: 10 additions & 53 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -754,61 +754,18 @@ end
@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()

# test that @deprecate_moved can be overridden by an import
Base.@deprecate_moved foo1234 "Foo"
Base.@deprecate_moved bar "Bar" false
end # module
module Foo1234
export foo1234
foo1234(x) = x+1
end

@testset "@deprecate" begin
using .DeprecationTests
using .Foo1234
@test foo1234(3) == 4
@test_throws ErrorException DeprecationTests.bar(3)

# 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

@testset "inline bug #18735" begin
@noinline f(n) = n ? error() : Int
g() = Union{f(true)}
@test_throws ErrorException g()
end

include("testenv.jl")

let flags = Cmd(filter(a->!contains(a, "depwarn"), collect(test_exeflags)))
local cmd = `$test_exename $flags deprecation_exec.jl`

if !success(pipeline(cmd; stdout=STDOUT, stderr=STDERR))
error("Deprecation test failed, cmd : $cmd")
end
end

0 comments on commit b1c196e

Please sign in to comment.