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 do block with @code_... macros #35283

Merged
merged 3 commits into from
Apr 1, 2020
Merged

Conversation

tkf
Copy link
Member

@tkf tkf commented Mar 28, 2020

This PR makes invocations like @code_typed map(1:1) do x; x; end work.

@tkf tkf changed the title Support do block works with @code_... macros Support do block with @code_... macros Mar 28, 2020
@Liozou
Copy link
Member

Liozou commented Mar 28, 2020

Nice addition! But it seems to me you are not taking into account the possible presence of keyword arguments:

julia> open("VERSION"; read=true) do f; f end
IOStream(<file VERSION>)

julia> @code_typed open("VERSION"; read=true) do f; f end
ERROR: syntax: invalid syntax ; read = true
Stacktrace:
 [1] top-level scope at REPL[15]:1

@Liozou
Copy link
Member

Liozou commented Mar 28, 2020

I think one option is to lower the do-block by hand near the entry point of the function, so modifying the gen_call_with_extracted_types this way instead of yours:

diff --git a/stdlib/InteractiveUtils/src/macros.jl b/stdlib/InteractiveUtils/src/macros.jl
index e6dd8cb7d1..91b6521bfa 100644
--- a/stdlib/InteractiveUtils/src/macros.jl
+++ b/stdlib/InteractiveUtils/src/macros.jl
@@ -8,6 +8,14 @@ separate_kwargs(args...; kwargs...) = (args, kwargs.data)
 
 function gen_call_with_extracted_types(__module__, fcn, ex0, kws=Expr[])
     if isa(ex0, Expr)
+        if ex0.head === :do && Meta.isexpr(get(ex0.args, 1, nothing), :call)
+            if length(ex0.args) != 2
+                return Expr(:call, :error, "ill-formed do call")
+            end
+            i = findlast(a->(Meta.isexpr(a, :kw) || Meta.isexpr(a, :parameters)), ex0.args[1].args)
+            insert!(ex0.args[1].args, (isnothing(i) ? 2 : i+1), ex0.args[2])
+            ex0 = ex0.args[1]
+        end
         if any(a->(Meta.isexpr(a, :kw) || Meta.isexpr(a, :parameters)), ex0.args)
             return quote
                 local arg1 = $(esc(ex0.args[1]))

Another option would probably be to check for keywords at the point where you originally added the do-block handling.

@tkf
Copy link
Member Author

tkf commented Mar 28, 2020

@Liozou Thanks a lot for looking into this! I switched to your patch.

@Liozou
Copy link
Member

Liozou commented Mar 29, 2020

Glad I could help! :)

@JeffBezanson JeffBezanson merged commit 652f3fb into JuliaLang:master Apr 1, 2020
oxinabox pushed a commit to oxinabox/julia that referenced this pull request Apr 8, 2020
ravibitsgoa pushed a commit to ravibitsgoa/julia that referenced this pull request Apr 9, 2020
staticfloat pushed a commit that referenced this pull request Apr 21, 2020
@tkf tkf deleted the do-code branch May 3, 2020 23:21
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 this pull request may close these issues.

3 participants