Skip to content

Commit

Permalink
Merge pull request #409 from JuliaLang/tb/nospecialize
Browse files Browse the repository at this point in the history
Support more forms of at-nospecialize.
  • Loading branch information
maleadt authored Nov 9, 2017
2 parents 5bd5e8c + c0edbd7 commit fbd9cb2
Show file tree
Hide file tree
Showing 2 changed files with 27 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
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

0 comments on commit fbd9cb2

Please sign in to comment.