Skip to content

Add support for at-nospecialize #384

@ararslan

Description

@ararslan

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions