From 775d9eedc2a5d6e5dcd54fa8248430385ae7f18f Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Tue, 7 Nov 2017 20:50:10 +0100 Subject: [PATCH] Support more forms of at-nospecialize. --- src/Compat.jl | 20 +++++++++++++++++--- test/runtests.jl | 5 +++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Compat.jl b/src/Compat.jl index 993d33f85..28e7e0e17 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -7,11 +7,25 @@ using Base.Meta @static if !isdefined(Base, Symbol("@nospecialize")) # 0.7 macro nospecialize(arg) - earg = esc(arg) if isa(arg, Symbol) - return :($earg::ANY) + # @nospecialize(arg) + return :($(esc(arg))::ANY) + elseif isa(arg, Expr) && arg.head == :(::) + # @nospecialize(arg::typ) + # unsupported: needs ::ANY which would change dispatch as determined by ::typ + elseif isa(arg, Expr) && arg.head == :(=) + # @nospecialize(arg=val) + arg, val = arg.args + if isa(arg, Expr) && arg.head == :(::) + # @nospecialize(arg::typ=val) + # unsupported (see above), but generate a kw arg + arg, typ = arg.args + return Expr(:kw, :($(esc(arg))::$(esc(typ))), esc(val)) + else + return Expr(:kw, :($(esc(arg))::ANY), esc(val)) + end end - return earg + return esc(arg) end export @nospecialize end diff --git a/test/runtests.jl b/test/runtests.jl index fb1943a22..0f3236fd4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -763,6 +763,11 @@ no_specialize(@nospecialize(x)) = sin(1) no_specialize(@nospecialize(x::Integer)) = sin(2) @test no_specialize(1.0) == sin(1) @test no_specialize(1) == sin(2) +no_specialize_kw(@nospecialize(x=0)) = sin(1) +no_specialize_kw(@nospecialize(x::Integer=0)) = sin(2) +@test no_specialize_kw(1.0) == sin(1) +@test no_specialize_kw(1) == sin(2) +@test no_specialize_kw() == sin(2) # 0.7 @test read(IOBuffer("aaaa"), String) == "aaaa"