Skip to content

Commit

Permalink
recurse in builtin expansion if we get a new builtin when expanding (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC authored Feb 23, 2022
1 parent d7fbc26 commit bca0175
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/builtins.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ end
const kwinvoke_name = isdefined(Core, Symbol("#kw##invoke")) ? Symbol("#kw##invoke") : Symbol("##invoke")
const kwinvoke_instance = getfield(Core, kwinvoke_name).instance


function maybe_recurse_expanded_builtin(frame, new_expr)
f = new_expr.args[1]
if isa(f, Core.Builtin) || isa(f, Core.IntrinsicFunction)
return maybe_evaluate_builtin(frame, new_expr, true)
else
return new_expr
end
end

"""
ret = maybe_evaluate_builtin(frame, call_expr, expand::Bool)
Expand Down Expand Up @@ -58,7 +68,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
for x in argsflat
push!(new_expr.args, (isa(x, Symbol) || isa(x, Expr) || isa(x, QuoteNode)) ? QuoteNode(x) : x)
end
return new_expr
return maybe_recurse_expanded_builtin(frame, new_expr)
elseif @static isdefined(Core, :_call_latest) ? f === Core._call_latest : false
args = getargs(args, frame)
if !expand
Expand All @@ -69,7 +79,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
for x in args
push!(new_expr.args, (isa(x, Symbol) || isa(x, Expr) || isa(x, QuoteNode)) ? QuoteNode(x) : x)
end
return new_expr
return maybe_recurse_expanded_builtin(frame, new_expr)
elseif @static isdefined(Core, :_apply_latest) ? f === Core._apply_latest : false
args = getargs(args, frame)
if !expand
Expand All @@ -80,7 +90,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
for x in args
push!(new_expr.args, (isa(x, Symbol) || isa(x, Expr) || isa(x, QuoteNode)) ? QuoteNode(x) : x)
end
return new_expr
return maybe_recurse_expanded_builtin(frame, new_expr)
elseif @static isdefined(Core, :_apply_iterate) ? f === Core._apply_iterate : false
argswrapped = getargs(args, frame)
if !expand
Expand All @@ -95,7 +105,7 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
for x in argsflat
push!(new_expr.args, (isa(x, Symbol) || isa(x, Expr) || isa(x, QuoteNode)) ? QuoteNode(x) : x)
end
return new_expr
return maybe_recurse_expanded_builtin(frame, new_expr)
elseif f === Core._apply_pure
return Some{Any}(Core._apply_pure(getargs(args, frame)...))
elseif f === Core._expr
Expand Down
11 changes: 11 additions & 0 deletions test/breakpoints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,14 @@ end
@test last(JuliaInterpreter.debug_command(f, :c)) isa BreakpointRef
end
end

@testset "recursive builtins" begin
g(args...) = args
args = (iterate, g, (1,3))

h() = Core._apply_iterate(iterate, Core._apply_iterate, args)
breakpoint(g)
frame, bp = @interpret h()
var = JuliaInterpreter.locals(leaf(frame))
@test filter(v->v.name === :args, var)[1].value == (1,3)
end

0 comments on commit bca0175

Please sign in to comment.