Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support more forms of at-nospecialize. #409

Merged
merged 2 commits into from
Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,16 @@ 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_kw1(@nospecialize(x=0)) = sin(1)
no_specialize_kw1(@nospecialize(x::Integer)) = sin(2)
@test no_specialize_kw1(1.0) == sin(1)
@test no_specialize_kw1(1) == sin(2)
@test no_specialize_kw1() == sin(2)
no_specialize_kw2(@nospecialize(x)) = sin(1)
no_specialize_kw2(@nospecialize(x::Integer=0)) = sin(2)
@test no_specialize_kw2(1.0) == sin(1)
@test no_specialize_kw2(1) == sin(2)
@test no_specialize_kw2() == sin(2)

# 0.7
@test read(IOBuffer("aaaa"), String) == "aaaa"
Expand Down