Skip to content

Commit c66aeb3

Browse files
committed
Fix Vararg method printing
1 parent 1f4ae21 commit c66aeb3

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

base/methodshow.jl

+25-13
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
# Method and method table pretty-printing
44

55
function argtype_decl(env, n, t) # -> (argname, argtype)
6-
if isvarargtype(t)
7-
return argtype_decl_vararg(env, n, t)
8-
end
96
if isa(n,Expr)
107
n = n.args[1] # handle n::T in arg list
118
end
@@ -17,20 +14,35 @@ function argtype_decl(env, n, t) # -> (argname, argtype)
1714
if t === Any && !isempty(s)
1815
return s, ""
1916
end
20-
return s, string(t)
17+
if isvarargtype(t)
18+
tt, tn = t.parameters[1], t.parameters[2]
19+
if isa(tn, TypeVar) && !tn.bound
20+
if tt === Any || (isa(tt, TypeVar) && !tt.bound)
21+
return string(s, "..."), ""
22+
else
23+
return s, string_with_env(env, tt) * "..."
24+
end
25+
end
26+
return s, string_with_env(env, "Vararg{", tt, ",", tn, "}")
27+
elseif t == String
28+
return s, "String"
29+
end
30+
return s, string_with_env(env, t)
2131
end
2232

2333
function argtype_decl_vararg(env, n, t)
24-
s = string(n.args[1])
25-
if n.args[2].head == :...
26-
# x... or x::T... declaration
27-
if t.parameters[1] === Any
28-
return string(s, "..."), ""
29-
else
30-
return s, string_with_env(env, t.parameters[1]) * "..."
34+
if isa(n, Expr)
35+
s = string(n.args[1])
36+
if n.args[2].head == :...
37+
# x... or x::T... declaration
38+
if t.parameters[1] === Any
39+
return string(s, "..."), ""
40+
else
41+
return s, string_with_env(env, t.parameters[1]) * "..."
42+
end
43+
elseif t == String
44+
return s, "String"
3145
end
32-
elseif t == String
33-
return s, "String"
3446
end
3547
# x::Vararg, x::Vararg{T}, or x::Vararg{T,N} declaration
3648
s, length(n.args[2].args) < 4 ?

test/show.jl

+12-14
Original file line numberDiff line numberDiff line change
@@ -400,22 +400,20 @@ function test_mt(f, str)
400400
io = IOBuffer()
401401
show(io, defs)
402402
strio = takebuf_string(io)
403+
strio = split(strio, " at")[1]
403404
@test strio[1:length(str)] == str
404405
end
405-
begin
406-
local f1, f2, f3, f4, f5
407-
f1(x...) = [x...]
408-
f2(x::Vararg{Any}) = [x...]
409-
f3(x::Vararg) = [x...]
410-
f4(x::Vararg{Any,3}) = [x...]
411-
f5{T,N}(A::AbstractArray{T,N}, indexes::Vararg{Int,N}) = [indexes...]
412-
test_mt(f1, "f1(x...)")
413-
test_mt(f2, "f2(x::Vararg{Any})")
414-
test_mt(f3, "f3(x::Vararg{T<:Any})") # FIXME? better as x::Vararg?
415-
# test_mt(f4, "f4(x::Vararg{Any,3})")
416-
intstr = string(Int)
417-
test_mt(f5, "f5{T,N}(A::AbstractArray{T,N}, indexes::Vararg{$intstr,N})")
418-
end
406+
show_f1(x...) = [x...]
407+
show_f2(x::Vararg{Any}) = [x...]
408+
show_f3(x::Vararg) = [x...]
409+
show_f4(x::Vararg{Any,3}) = [x...]
410+
show_f5{T,N}(A::AbstractArray{T,N}, indexes::Vararg{Int,N}) = [indexes...]
411+
test_mt(show_f1, "show_f1(x...)")
412+
test_mt(show_f2, "show_f2(x...)")
413+
test_mt(show_f3, "show_f3(x...)")
414+
test_mt(show_f4, "show_f4(x::Vararg{Any,3})")
415+
intstr = string(Int)
416+
test_mt(show_f5, "show_f5{T,N}(A::AbstractArray{T,N}, indexes::Vararg{$intstr,N})")
419417

420418
# Issue #15525, printing of vcat
421419
@test sprint(show, :([a;])) == ":([a;])"

0 commit comments

Comments
 (0)