Skip to content

Commit

Permalink
Support do block with @code_... macros (#35283)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf authored and staticfloat committed Apr 21, 2020
1 parent 046bad9 commit ea7e2d2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
9 changes: 9 additions & 0 deletions stdlib/InteractiveUtils/src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down
4 changes: 4 additions & 0 deletions stdlib/InteractiveUtils/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ 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}
@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
# the heuristic for type instability warning.
Expand Down

0 comments on commit ea7e2d2

Please sign in to comment.