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

Add support for at-nospecialize #384

Closed
ararslan opened this issue Jul 22, 2017 · 0 comments · Fixed by #385
Closed

Add support for at-nospecialize #384

ararslan opened this issue Jul 22, 2017 · 0 comments · Fixed by #385

Comments

@ararslan
Copy link
Member

I started working on this but haven't quite nailed it down yet. Here's my start if anyone else wants to have a go:

diff --git a/src/Compat.jl b/src/Compat.jl
index c0c82d4..3007fab 100644
--- a/src/Compat.jl
+++ b/src/Compat.jl
@@ -662,6 +662,27 @@ module Sys
     end
 end

+if VERSION < v"0.7.0-DEV.969"
+    # If any type annotations are provided, we'll just ignore them for now
+    macro nospecialize(ex)
+        if isa(ex, Symbol)                                    # @nospecialize(x)
+            Expr(:(::), quot(ex), :ANY)
+        elseif ex.head == :(=) && isa(ex.args[1], Symbol)     # @nospecialize(x=1)
+            Expr(:(=), Expr(:(::), quot(ex.args[1]), :ANY), quot(ex.args[2]))
+        elseif ex.head == :(=) && ex.args[1].head == :(::)    # @nospecialize(x::T=1)
+            Expr(:(=), Expr(:(::), quot(ex.args[1].args[1]), :ANY), quot(ex.args[1].args[2]))
+        elseif ex.head == :(...) && isa(ex.args[1], Symbol)   # @nospecialize(x...)
+            Expr(:(...), Expr(:(::), quot(ex.args[1]), :ANY))
+        elseif ex.head == :(...) && ex.args[1].head == :(::)  # @nospecialize(x::T...)
+            Expr(:(...), Expr(:(::), quot(ex.args[1].args[1]), :ANY))
+        else
+            error("improper use of @nospecialize")
+        end
+    end
+else
+    import Base: @nospecialize
+end
+
 include("deprecated.jl")

 end # module Compat

It basically just takes the @nospecialize annotation for a function argument and turns it into an ::ANY annotation on the argument.

The quoting here isn't working quite right, since, for example, the returned expression from @nospecialize(x) is actually :x::Main.ANY as the above stands. And we can't use esc here instead, since that produces Main.x::Main.ANY, which is wrong in the context of specifying a function signature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant