From 224caa0a0e63b61790bcfb1bc8e744305b683cc9 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Thu, 7 Mar 2019 08:32:35 -0600 Subject: [PATCH] Fix handling of kwargs in atsign-interpret (fixes #113) --- src/JuliaInterpreter.jl | 24 ++++++++++-------------- test/interpret.jl | 4 ++++ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/JuliaInterpreter.jl b/src/JuliaInterpreter.jl index b5059970cad1c1..1cf5b39cd395b5 100644 --- a/src/JuliaInterpreter.jl +++ b/src/JuliaInterpreter.jl @@ -1038,22 +1038,18 @@ end lower(mod, arg) = false ? expand(arg) : Meta.lower(mod, arg) -# This is a version of gen_call_with_extracted_types, except that is passes back the call expression -# for further processing. +separate_kwargs(args...; kwargs...) = (args, kwargs.data) + +# This is a version of InteractiveUtils.gen_call_with_extracted_types, except that is passes back the +# call expression for further processing. function extract_args(__module__, ex0) if isa(ex0, Expr) - kws = collect(filter(x->isexpr(x,:kw),ex0.args)) - if !isempty(kws) - names = [] - values = Tuple(map(x-> begin - push!(names,x.args[1]) - x.args[2] - end,kws)) - names = Tuple(names) - return Expr(:tuple,:(Core.kwfunc($(ex0.args[1]))), - Expr(:call, NamedTuple{names,typeof(values)}, values), - map(x->isexpr(x, :parameters) ? QuoteNode(x) : x, - filter(x->!isexpr(x, :kw),ex0.args))...) + if any(a->(Meta.isexpr(a, :kw) || Meta.isexpr(a, :parameters)), ex0.args) + return quote + local arg1 = $(ex0.args[1]) + local args, kwargs = $separate_kwargs($(ex0.args[2:end]...)) + tuple(Core.kwfunc(arg1), kwargs, arg1, args...) + end elseif ex0.head == :. return Expr(:tuple, :getproperty, ex0.args...) elseif ex0.head == :(<:) diff --git a/test/interpret.jl b/test/interpret.jl index 4dbd6733c795bd..f2111578586afd 100644 --- a/test/interpret.jl +++ b/test/interpret.jl @@ -314,6 +314,10 @@ end @interpret f106c() @interpret f106d() +# issue #113 +f113(;x) = x +@test @interpret(f113(;x=[1,2,3])) == f113(;x=[1,2,3]) + # Some expression can appear nontrivial but lower to nothing @test isa(JuliaInterpreter.prepare_thunk(Main, :(@static if ccall(:jl_get_UNAME, Any, ()) == :NoOS 1+1 end)), Nothing) @test isa(JuliaInterpreter.prepare_thunk(Main, :(Base.BaseDocs.@kw_str "using")), Nothing)