Skip to content

Commit 4550811

Browse files
tkfravibitsgoa
authored andcommitted
Support do block with @code_... macros (JuliaLang#35283)
1 parent 6802245 commit 4550811

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

stdlib/InteractiveUtils/src/macros.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ separate_kwargs(args...; kwargs...) = (args, kwargs.data)
88

99
function gen_call_with_extracted_types(__module__, fcn, ex0, kws=Expr[])
1010
if isa(ex0, Expr)
11+
if ex0.head === :do && Meta.isexpr(get(ex0.args, 1, nothing), :call)
12+
if length(ex0.args) != 2
13+
return Expr(:call, :error, "ill-formed do call")
14+
end
15+
i = findlast(a->(Meta.isexpr(a, :kw) || Meta.isexpr(a, :parameters)), ex0.args[1].args)
16+
args = copy(ex0.args[1].args)
17+
insert!(args, (isnothing(i) ? 2 : i+1), ex0.args[2])
18+
ex0 = Expr(:call, args...)
19+
end
1120
if any(a->(Meta.isexpr(a, :kw) || Meta.isexpr(a, :parameters)), ex0.args)
1221
return quote
1322
local arg1 = $(esc(ex0.args[1]))

stdlib/InteractiveUtils/test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ Base.getproperty(t::T1234321, ::Symbol) = "foo"
9090
Base.setproperty!(t::T1234321, ::Symbol, ::Symbol) = "foo"
9191
@test (@code_typed T1234321(1).f = :foo).second == String
9292

93+
# Make sure `do` block works with `@code_...` macros
94+
@test (@code_typed map(1:1) do x; x; end).second == Vector{Int}
95+
@test (@code_typed open(`cat`; read=true) do _; 1; end).second == Int
96+
9397
module ImportIntrinsics15819
9498
# Make sure changing the lookup path of an intrinsic doesn't break
9599
# the heuristic for type instability warning.

0 commit comments

Comments
 (0)