Skip to content

Commit fbd9cb2

Browse files
authored
Merge pull request #409 from JuliaLang/tb/nospecialize
Support more forms of at-nospecialize.
2 parents 5bd5e8c + c0edbd7 commit fbd9cb2

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/Compat.jl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,25 @@ using Base.Meta
77
@static if !isdefined(Base, Symbol("@nospecialize"))
88
# 0.7
99
macro nospecialize(arg)
10-
earg = esc(arg)
1110
if isa(arg, Symbol)
12-
return :($earg::ANY)
11+
# @nospecialize(arg)
12+
return :($(esc(arg))::ANY)
13+
elseif isa(arg, Expr) && arg.head == :(::)
14+
# @nospecialize(arg::typ)
15+
# unsupported: needs ::ANY which would change dispatch as determined by ::typ
16+
elseif isa(arg, Expr) && arg.head == :(=)
17+
# @nospecialize(arg=val)
18+
arg, val = arg.args
19+
if isa(arg, Expr) && arg.head == :(::)
20+
# @nospecialize(arg::typ=val)
21+
# unsupported (see above), but generate a kw arg
22+
arg, typ = arg.args
23+
return Expr(:kw, :($(esc(arg))::$(esc(typ))), esc(val))
24+
else
25+
return Expr(:kw, :($(esc(arg))::ANY), esc(val))
26+
end
1327
end
14-
return earg
28+
return esc(arg)
1529
end
1630
export @nospecialize
1731
end

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,16 @@ no_specialize(@nospecialize(x)) = sin(1)
763763
no_specialize(@nospecialize(x::Integer)) = sin(2)
764764
@test no_specialize(1.0) == sin(1)
765765
@test no_specialize(1) == sin(2)
766+
no_specialize_kw1(@nospecialize(x=0)) = sin(1)
767+
no_specialize_kw1(@nospecialize(x::Integer)) = sin(2)
768+
@test no_specialize_kw1(1.0) == sin(1)
769+
@test no_specialize_kw1(1) == sin(2)
770+
@test no_specialize_kw1() == sin(2)
771+
no_specialize_kw2(@nospecialize(x)) = sin(1)
772+
no_specialize_kw2(@nospecialize(x::Integer=0)) = sin(2)
773+
@test no_specialize_kw2(1.0) == sin(1)
774+
@test no_specialize_kw2(1) == sin(2)
775+
@test no_specialize_kw2() == sin(2)
766776

767777
# 0.7
768778
@test read(IOBuffer("aaaa"), String) == "aaaa"

0 commit comments

Comments
 (0)