From 26c59815dcb1030bbf779c6ff3c1a2e308ab7177 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Fri, 27 Mar 2020 20:56:02 -0700 Subject: [PATCH 1/3] Support `do` block works with `@code_...` macros --- stdlib/InteractiveUtils/src/macros.jl | 5 +++++ stdlib/InteractiveUtils/test/runtests.jl | 3 +++ 2 files changed, 8 insertions(+) diff --git a/stdlib/InteractiveUtils/src/macros.jl b/stdlib/InteractiveUtils/src/macros.jl index e6dd8cb7d17da..c1e37a6cf7e96 100644 --- a/stdlib/InteractiveUtils/src/macros.jl +++ b/stdlib/InteractiveUtils/src/macros.jl @@ -51,6 +51,11 @@ function gen_call_with_extracted_types(__module__, fcn, ex0, kws=Expr[]) return Expr(:call, fcn, f, Expr(:call, typesof, map(esc, ex0.args)...)) end + elseif ex0.head === :do && Meta.isexpr(get(ex0.args, 1, nothing), :call) + f = ex0.args[1].args[1] + args = [[ex0.args[2]]; ex0.args[1].args[2:end]] + return Expr(:call, fcn, esc(f), Expr(:call, typesof, map(esc, args)...), + kws...) else for (head, f) in (:ref => Base.getindex, :hcat => Base.hcat, :(.) => Base.getproperty, :vect => Base.vect, Symbol("'") => Base.adjoint, :typed_hcat => Base.typed_hcat, :string => string) if ex0.head === head diff --git a/stdlib/InteractiveUtils/test/runtests.jl b/stdlib/InteractiveUtils/test/runtests.jl index b5d2abc39a1b7..41c4deea87c55 100644 --- a/stdlib/InteractiveUtils/test/runtests.jl +++ b/stdlib/InteractiveUtils/test/runtests.jl @@ -90,6 +90,9 @@ Base.getproperty(t::T1234321, ::Symbol) = "foo" Base.setproperty!(t::T1234321, ::Symbol, ::Symbol) = "foo" @test (@code_typed T1234321(1).f = :foo).second == String +# Make sure `do` block works with `@code_...` macros +@test (@code_typed map(1:1) do x; x; end).second == Vector{Int} + module ImportIntrinsics15819 # Make sure changing the lookup path of an intrinsic doesn't break # the heuristic for type instability warning. From f909ced3a67506f482bdd67e40d36caec637cb93 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Sat, 28 Mar 2020 16:47:15 -0700 Subject: [PATCH 2/3] Revert "Support `do` block works with `@code_...` macros" This reverts commit 26c59815dcb1030bbf779c6ff3c1a2e308ab7177. --- stdlib/InteractiveUtils/src/macros.jl | 5 ----- 1 file changed, 5 deletions(-) diff --git a/stdlib/InteractiveUtils/src/macros.jl b/stdlib/InteractiveUtils/src/macros.jl index c1e37a6cf7e96..e6dd8cb7d17da 100644 --- a/stdlib/InteractiveUtils/src/macros.jl +++ b/stdlib/InteractiveUtils/src/macros.jl @@ -51,11 +51,6 @@ function gen_call_with_extracted_types(__module__, fcn, ex0, kws=Expr[]) return Expr(:call, fcn, f, Expr(:call, typesof, map(esc, ex0.args)...)) end - elseif ex0.head === :do && Meta.isexpr(get(ex0.args, 1, nothing), :call) - f = ex0.args[1].args[1] - args = [[ex0.args[2]]; ex0.args[1].args[2:end]] - return Expr(:call, fcn, esc(f), Expr(:call, typesof, map(esc, args)...), - kws...) else for (head, f) in (:ref => Base.getindex, :hcat => Base.hcat, :(.) => Base.getproperty, :vect => Base.vect, Symbol("'") => Base.adjoint, :typed_hcat => Base.typed_hcat, :string => string) if ex0.head === head From 21f7d2faa7c9548ead16e1227677ba1e118f2517 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Sat, 28 Mar 2020 16:53:03 -0700 Subject: [PATCH 3/3] Support `do` block with keyword arguments Co-authored-by: Liozou --- stdlib/InteractiveUtils/src/macros.jl | 9 +++++++++ stdlib/InteractiveUtils/test/runtests.jl | 1 + 2 files changed, 10 insertions(+) diff --git a/stdlib/InteractiveUtils/src/macros.jl b/stdlib/InteractiveUtils/src/macros.jl index e6dd8cb7d17da..d3695cc437834 100644 --- a/stdlib/InteractiveUtils/src/macros.jl +++ b/stdlib/InteractiveUtils/src/macros.jl @@ -8,6 +8,15 @@ 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) + args = copy(ex0.args[1].args) + insert!(args, (isnothing(i) ? 2 : i+1), ex0.args[2]) + ex0 = Expr(:call, args...) + end if any(a->(Meta.isexpr(a, :kw) || Meta.isexpr(a, :parameters)), ex0.args) return quote local arg1 = $(esc(ex0.args[1])) diff --git a/stdlib/InteractiveUtils/test/runtests.jl b/stdlib/InteractiveUtils/test/runtests.jl index 41c4deea87c55..808aa6de85981 100644 --- a/stdlib/InteractiveUtils/test/runtests.jl +++ b/stdlib/InteractiveUtils/test/runtests.jl @@ -92,6 +92,7 @@ Base.setproperty!(t::T1234321, ::Symbol, ::Symbol) = "foo" # Make sure `do` block works with `@code_...` macros @test (@code_typed map(1:1) do x; x; end).second == Vector{Int} +@test (@code_typed open(`cat`; read=true) do _; 1; end).second == Int module ImportIntrinsics15819 # Make sure changing the lookup path of an intrinsic doesn't break