Repository: JuliaLang/julia
File: stdlib/InteractiveUtils/src/macros.jl (Julia)

Code before:
    if isexpr(ex, :...)
        newarg = Expr(:..., newarg)
        push!(args, only(ex.args))
    elseif isexpr(ex, :kw)
        newarg = Expr(:kw, ex.args[1], newarg)
        push!(args, ex.args[end])
    else
        push!(args, ex)
    end
    return newarg, i+1
end

function extract_farg(@nospecialize arg)
    !isexpr(arg, :(::), 1) && return arg
    fT = arg.args[1]
    :($construct_callable($fT))
end

function construct_callable(@nospecialize(func::Type))
    # Support function singleton types such as `(::typeof(f))(args...)`
    Base.issingletontype(func) && isdefined(func, :instance) && return func.instance
    # Don't support type annotations otherwise, we don't want to give wrong answers
    # for callables such as `(::Returns{Int})(args...)` where using `Returns{Int}`
    # would give us code for the constructor, not for the callable object.
    throw(ArgumentError("If a function type is explicitly provided, it must be a singleton whose only instance is the callable object.
                         To alleviate this restriction, the reflection macro may set `use_signature_tuple = true` if using `gen_call_with_extracted_types`."))
end

function separate_kwargs(exs::Vector{Any})
    args = []
    kwargs = []
    for ex in exs
        if isexpr(ex, :kw)
            push!(kwargs, ex)
        elseif isexpr(ex, :parameters)
            for kw in ex.args
                push!(kwargs, kw)
            end
        else
            push!(args, ex)
        end
    end
    args, kwargs
end

function are_kwargs_valid(kwargs::Vector{Any})
    for kwarg in kwargs
        isexpr(kwarg, :..., 1) && continue
        isexpr(kwarg, :kw, 2) && isa(kwarg.args[1], Symbol) && continue
        isexpr(kwarg, :(::), 2) && continue
        isa(kwarg, Symbol) && continue

Review comment (refactor):
```suggestion
                         To remove this restriction, the reflection macro may set `use_signature_tuple = true` if the reflection function accepts "functor" arguments, specified as a `Tuple{...}` type."))
```

Code after:
        elseif isexpr(ex, :.)
            (start, branches) = (1, ex.args[2].args)
        else
            (start, branches) = (2, ex.args)
        end
        for j in start:length(branches)::Int
            branch, i = recursive_dotcalls!(branches[j], args, i)
            branches[j] = branch
        end
        return ex, i
    elseif isexpr(ex, :parameters)
        for j in eachindex(ex.args)
            param, i = recursive_dotcalls!(ex.args[j], args, i)
            ex.args[j] = param
        end
        return ex, i
    end
    newarg = Symbol('x', i)
    if isexpr(ex, :...)
        newarg = Expr(:..., newarg)
        push!(args, only(ex.args))
    elseif isexpr(ex, :kw)
        newarg = Expr(:kw, ex.args[1], newarg)
        push!(args, ex.args[end])
    else
        push!(args, ex)
    end
    return newarg, i+1
end

function extract_farg(@nospecialize arg)
    !isexpr(arg, :(::), 1) && return arg
    fT = arg.args[1]
    :($construct_callable($fT))
end

function construct_callable(@nospecialize(func::Type))
    # Support function singleton types such as `(::typeof(f))(args...)`
    Base.issingletontype(func) && isdefined(func, :instance) && return func.instance
    # Don't support type annotations otherwise, we don't want to give wrong answers
    # for callables such as `(::Returns{Int})(args...)` where using `Returns{Int}`
    # would give us code for the constructor, not for the callable object.
    throw(ArgumentError("If the function type is explicitly provided via a type annotation, it must be a singleton whose only instance is the callable object.
                         To remove this restriction, the reflection macro must set `use_signature_tuple = true` if the reflection function supports a single signature tuple type argument, such as `Tuple{typeof(f), argtypes...}`"))
end

function separate_kwargs(exs::Vector{Any})
    args = []
    kwargs = []
    for ex in exs
        if isexpr(ex, :kw)