Skip to content

Commit f47d9fe

Browse files
authoredJul 12, 2016
Merge pull request #17361 from JuliaLang/jb/fix17354
fix #17354, expand `Tuple{Vararg{Any,2}}` on construction
2 parents 2a193bb + ec11db9 commit f47d9fe

File tree

3 files changed

+19
-40
lines changed

3 files changed

+19
-40
lines changed
 

‎base/methodshow.jl

+7-24
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
# Method and method table pretty-printing
44

5-
function argtype_decl(env, n, t) # -> (argname, argtype)
5+
function argtype_decl(env, n, sig, i, nargs, isva) # -> (argname, argtype)
6+
t = sig.parameters[i]
7+
if i == nargs && isva && !isvarargtype(t)
8+
t = Vararg{t,length(sig.parameters)-nargs+1}
9+
end
610
if isa(n,Expr)
711
n = n.args[1] # handle n::T in arg list
812
end
@@ -30,26 +34,6 @@ function argtype_decl(env, n, t) # -> (argname, argtype)
3034
return s, string_with_env(env, t)
3135
end
3236

33-
function argtype_decl_vararg(env, n, t)
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"
45-
end
46-
end
47-
# x::Vararg, x::Vararg{T}, or x::Vararg{T,N} declaration
48-
s, length(n.args[2].args) < 4 ?
49-
string_with_env(env, "Vararg{", t.parameters[1], "}") :
50-
string_with_env(env, "Vararg{", t.parameters[1], ",", t.parameters[2], "}")
51-
end
52-
5337
function arg_decl_parts(m::Method)
5438
tv = m.tvars
5539
if !isa(tv,SimpleVector)
@@ -61,9 +45,8 @@ function arg_decl_parts(m::Method)
6145
file, line = "", 0
6246
if li !== nothing
6347
argnames = li.slotnames[1:li.nargs]
64-
s = Symbol("?")
65-
decls = Any[argtype_decl(:tvar_env => tv, get(argnames, i, s), m.sig.parameters[i])
66-
for i = 1:length(m.sig.parameters)]
48+
decls = Any[argtype_decl(:tvar_env => tv, argnames[i], m.sig, i, li.nargs, li.isva)
49+
for i = 1:li.nargs]
6750
if isdefined(li, :def)
6851
file, line = li.def.file, li.def.line
6952
end

‎src/jltypes.c

+10-16
Original file line numberDiff line numberDiff line change
@@ -2157,28 +2157,22 @@ static jl_value_t *inst_datatype(jl_datatype_t *dt, jl_svec_t *p, jl_value_t **i
21572157
if (nt < 0)
21582158
jl_errorf("apply_type: Vararg length N is negative: %zd", nt);
21592159
va = jl_tparam0(va);
2160-
if (nt == 0 || jl_is_leaf_type(va)) {
2160+
if (nt == 0 || !jl_has_typevars(va)) {
21612161
if (ntp == 1)
21622162
return jl_tupletype_fill(nt, va);
21632163
size_t i, l;
2164+
p = jl_alloc_svec(ntp - 1 + nt);
21642165
for (i = 0, l = ntp - 1; i < l; i++) {
2165-
if (!jl_is_leaf_type(iparams[i]))
2166-
break;
2166+
jl_svecset(p, i, iparams[i]);
21672167
}
2168-
if (i == l) {
2169-
p = jl_alloc_svec(ntp - 1 + nt);
2170-
for (i = 0, l = ntp - 1; i < l; i++) {
2171-
jl_svecset(p, i, iparams[i]);
2172-
}
2173-
l = ntp - 1 + nt;
2174-
for (; i < l; i++) {
2175-
jl_svecset(p, i, va);
2176-
}
2177-
JL_GC_PUSH1(&p);
2178-
jl_value_t *ndt = (jl_value_t*)jl_apply_tuple_type(p);
2179-
JL_GC_POP();
2180-
return ndt;
2168+
l = ntp - 1 + nt;
2169+
for (; i < l; i++) {
2170+
jl_svecset(p, i, va);
21812171
}
2172+
JL_GC_PUSH1(&p);
2173+
jl_value_t *ndt = (jl_value_t*)jl_apply_tuple_type(p);
2174+
JL_GC_POP();
2175+
return ndt;
21822176
}
21832177
}
21842178
}

‎test/core.jl

+2
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ let N = TypeVar(:N,true)
139139
@test is(Bottom,typeintersect(Tuple{Array{Int,N},Vararg{Int,N}}, Tuple{Vector{Int},Real,Real,Real}))
140140
@test is(Bottom,typeintersect(Tuple{Vector{Int},Real,Real,Real}, Tuple{Array{Int,N},Vararg{Int,N}}))
141141
@test Tuple{Int,Vararg{Int,2}} == Tuple{Int,Int,Int}
142+
@test Tuple{Int,Vararg{Int,2}} === Tuple{Int,Int,Int}
143+
@test Tuple{Any, Any} === Tuple{Vararg{Any,2}}
142144
@test Tuple{Int,Vararg{Int,2}} == Tuple{Int,Int,Vararg{Int,1}}
143145
@test Tuple{Int,Vararg{Int,2}} == Tuple{Int,Int,Int,Vararg{Int,0}}
144146
@test !(Tuple{Int,Vararg{Int,2}} <: Tuple{Int,Int,Int,Vararg{Int,1}})

0 commit comments

Comments
 (0)
Please sign in to comment.