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 1 commit
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
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And you are defeating the whole point of the test. The test is added specifically to prevent making changes like this one.

no_specialize_kw(@nospecialize(x=0)) = sin(1)
no_specialize_kw(@nospecialize(x::Integer=0)) = sin(2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a method overwrite here...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I've split them up.

@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