Skip to content

Commit

Permalink
Merge pull request #35 from timholy/teh/namedtuple
Browse files Browse the repository at this point in the history
Generate names for `::NamedTuple` arguments (fixes #34)
  • Loading branch information
timholy committed Aug 24, 2018
2 parents f2d7ff5 + 1b7ebce commit c6379d5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/debug.jl
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,13 @@ function signature_names!(sigex::ExLike)
# This argument has a type but no name
return arg, true
end
if isa(arg, Expr) && arg.head == :curly && arg.args[1] == :Type
# Argument of the form ::Type{T}
return arg.args[2], false
if isa(arg, Expr) && arg.head == :curly
if arg.args[1] == :Type
# Argument of the form ::Type{T}
return arg.args[2], false
elseif arg.args[1] == :NamedTuple
return :NamedTuple, true, arg
end
end
return arg
end
Expand All @@ -537,20 +541,22 @@ function signature_names!(sigex::ExLike)
argnames = Union{Symbol,Expr}[]
for i = offset+1:length(sigex.args)
arg = sigex.args[i]
name = argname(arg)
if name isa Tuple
name, genname = name
if genname
retname = argname(arg)
if retname isa Tuple
should_gen = retname[2]
if should_gen
# This argument is missing a real name
argt = name
name = genunderscored(argt)
argt = length(retname) == 3 ? retname[3] : retname[1]
name = genunderscored(retname[1])
sigex.args[i] = :($name::$argt)
else
# This is a ::Type{T} argument. We should remove this from the list of parameters
name = retname[1]
parameternames = filter(!isequal(name), parameternames)
end
retname = name
end
push!(argnames, name)
push!(argnames, retname)
end

return sigex.args[1], tuple(argnames...), kwnames, tuple(parameternames...)
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Base.show(io::IO, ::ErrorsOnShow) = throw(ArgumentError("no show"))
ex = :(f(Tuseless::Type{T}, ::IndexStyle, x::Int) where T)
@test Rebugger.signature_names!(ex) == (:f, (:Tuseless, :__IndexStyle_1, :x), (), (:T,))
@test ex == :(f(Tuseless::Type{T}, __IndexStyle_1::IndexStyle, x::Int) where T)
# issue #34
ex = :(_mapreduce_dim(f, op, ::NamedTuple{()}, A::AbstractArray, ::Colon))
@test Rebugger.signature_names!(ex) == (:_mapreduce_dim, (:f, :op, :__NamedTuple_1, :A, :__Colon_1), (), ())
@test ex == :(_mapreduce_dim(f, op, __NamedTuple_1::NamedTuple{()}, A::AbstractArray, __Colon_1::Colon))
end
@testset "Caller buffer capture and insertion" begin
function run_insertion(str, atstr)
Expand Down

0 comments on commit c6379d5

Please sign in to comment.