Skip to content

Commit

Permalink
Support more forms of at-nospecialize.
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt committed Nov 8, 2017
1 parent 5bd5e8c commit 775d9ee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
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
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 775d9ee

Please sign in to comment.