Skip to content

Commit cb4ee8e

Browse files
committed
fixes to show and serialize for new types
1 parent 5c3c038 commit cb4ee8e

File tree

6 files changed

+72
-41
lines changed

6 files changed

+72
-41
lines changed

base/deprecated.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ for (Fun, func) in [(:IdFun, :identity),
337337
(::Type{typeof($(func))})() = $(func)
338338
end
339339
end
340-
@deprecate_binding CentralizedAbs2Fun typeof(centralizedabs2fun(0)).name.primary
341-
(::Type{typeof(centralizedabs2fun(0)).name.primary})(m::Number) = centralizedabs2fun(m)
340+
@deprecate_binding CentralizedAbs2Fun typeof(centralizedabs2fun(0)).name.wrapper
341+
(::Type{typeof(centralizedabs2fun(0)).name.wrapper})(m::Number) = centralizedabs2fun(m)
342342
@deprecate specialized_unary(f::Function) f
343343
@deprecate specialized_binary(f::Function) f
344344
@deprecate specialized_bitwise_unary(f::Function) f

base/methodshow.jl

+10-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function argtype_decl(env, n, sig, i, nargs, isva) # -> (argname, argtype)
1919
return s, ""
2020
end
2121
if isvarargtype(t)
22+
t = unwrap_unionall(t)
2223
tt, tn = t.parameters[1], t.parameters[2]
2324
if isa(tn, TypeVar) && !tn.bound
2425
if tt === Any || (isa(tt, TypeVar) && !tt.bound)
@@ -43,7 +44,8 @@ function arg_decl_parts(m::Method)
4344
file, line = "", 0
4445
if li !== nothing && isdefined(li, :slotnames)
4546
argnames = li.slotnames[1:li.nargs]
46-
decls = Any[argtype_decl(:tvar_env => tv, argnames[i], m.sig, i, li.nargs, li.isva)
47+
sig = unwrap_unionall(m.sig)
48+
decls = Any[argtype_decl(:tvar_env => tv, argnames[i], sig, i, li.nargs, li.isva)
4749
for i = 1:li.nargs]
4850
if isdefined(li, :def)
4951
file, line = li.def.file, li.def.line
@@ -72,9 +74,10 @@ end
7274

7375
function show(io::IO, m::Method; kwtype::Nullable{DataType}=Nullable{DataType}())
7476
tv, decls, file, line = arg_decl_parts(m)
75-
ft = m.sig.parameters[1]
77+
sig = unwrap_unionall(m.sig)
78+
ft = sig.parameters[1]
7679
d1 = decls[1]
77-
if m.sig === Tuple
80+
if sig === Tuple
7881
print(io, m.name)
7982
decls = Any[(), ("...", "")]
8083
elseif ft <: Function &&
@@ -99,7 +102,7 @@ function show(io::IO, m::Method; kwtype::Nullable{DataType}=Nullable{DataType}()
99102
join(io, [isempty(d[2]) ? d[1] : d[1]*"::"*d[2] for d in decls[2:end]],
100103
", ", ", ")
101104
if !isnull(kwtype)
102-
kwargs = kwarg_decl(m.sig, get(kwtype))
105+
kwargs = kwarg_decl(sig, get(kwtype))
103106
if !isempty(kwargs)
104107
print(io, "; ")
105108
join(io, kwargs, ", ", ", ")
@@ -197,7 +200,8 @@ end
197200

198201
function show(io::IO, ::MIME"text/html", m::Method; kwtype::Nullable{DataType}=Nullable{DataType}())
199202
tv, decls, file, line = arg_decl_parts(m)
200-
ft = m.sig.parameters[1]
203+
sig = unwrap_unionall(m.sig)
204+
ft = sig.parameters[1]
201205
d1 = decls[1]
202206
if ft <: Function &&
203207
isdefined(ft.name.module, ft.name.mt.name) &&
@@ -222,7 +226,7 @@ function show(io::IO, ::MIME"text/html", m::Method; kwtype::Nullable{DataType}=N
222226
join(io, [isempty(d[2]) ? d[1] : d[1]*"::<b>"*d[2]*"</b>"
223227
for d in decls[2:end]], ", ", ", ")
224228
if !isnull(kwtype)
225-
kwargs = kwarg_decl(m.sig, get(kwtype))
229+
kwargs = kwarg_decl(sig, get(kwtype))
226230
if !isempty(kwargs)
227231
print(io, "; <i>")
228232
join(io, kwargs, ", ", ", ")

base/replutil.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,9 @@ function show_method_candidates(io::IO, ex::MethodError, kwargs::Vector=Any[])
440440
for (func,arg_types_param) in funcs
441441
for method in methods(func)
442442
buf = IOBuffer()
443-
s1 = method.sig.parameters[1]
444-
sig = method.sig.parameters[2:end]
443+
sig0 = unwrap_unionall(method.sig)
444+
s1 = sig0.parameters[1]
445+
sig = sig0.parameters[2:end]
445446
print(buf, " ")
446447
if !isa(func, s1)
447448
# function itself doesn't match

base/serialize.jl

+21-18
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ const TAGS = Any[
2929
#LongSymbol, LongTuple, LongExpr,
3030
Symbol, Tuple, Expr, # dummy entries, intentionally shadowed by earlier ones
3131
LineNumberNode, Slot, LabelNode, GotoNode,
32-
QuoteNode, :reserved23 #=was TopNode=#, TypeVar, Core.Box, LambdaInfo,
32+
QuoteNode, UnionAll, TypeVar, Core.Box, LambdaInfo,
3333
Module, #=UndefRefTag=#Symbol, Task, String, Float16,
3434
SimpleVector, #=BackrefTag=#Symbol, Method, GlobalRef,
3535

36-
(), Bool, Any, :Any, Bottom, :reserved21, :reserved22, Type,
36+
(), Bool, Any, :Any, Bottom, Core.BottomType, :reserved22, Type,
3737
:Array, :TypeVar, :Box,
3838
:lambda, :body, :return, :call, Symbol("::"),
3939
:(=), :null, :gotoifnot, :A, :B, :C, :M, :N, :T, :S, :X, :Y,
@@ -384,7 +384,8 @@ function serialize(s::AbstractSerializer, g::GlobalRef)
384384
writetag(s.io, GLOBALREF_TAG)
385385
if g.mod === Main && isdefined(g.mod, g.name) && isconst(g.mod, g.name)
386386
v = getfield(g.mod, g.name)
387-
if isa(v, DataType) && v === v.name.primary && should_send_whole_type(s, v)
387+
unw = unwrap_unionall(v)
388+
if isa(unw,DataType) && v === unw.name.wrapper && should_send_whole_type(s, unw)
388389
# handle references to types in Main by sending the whole type.
389390
# needed to be able to send nested functions (#15451).
390391
write(s.io, UInt8(1))
@@ -408,13 +409,14 @@ end
408409
function serialize_typename(s::AbstractSerializer, t::TypeName)
409410
serialize(s, t.name)
410411
serialize(s, t.names)
411-
serialize(s, t.primary.super)
412-
serialize(s, t.primary.parameters)
413-
serialize(s, t.primary.types)
414-
serialize(s, isdefined(t.primary, :instance))
415-
serialize(s, t.primary.abstract)
416-
serialize(s, t.primary.mutable)
417-
serialize(s, t.primary.ninitialized)
412+
primary = unwrap_unionall(t.wrapper)
413+
serialize(s, primary.super)
414+
serialize(s, primary.parameters)
415+
serialize(s, primary.types)
416+
serialize(s, isdefined(primary, :instance))
417+
serialize(s, primary.abstract)
418+
serialize(s, primary.mutable)
419+
serialize(s, primary.ninitialized)
418420
if isdefined(t, :mt)
419421
serialize(s, t.mt.name)
420422
serialize(s, t.mt.defs)
@@ -469,7 +471,7 @@ function serialize_type_data(s, t::DataType, type_itself::Bool)
469471
serialize(s, mod)
470472
end
471473
if !isempty(t.parameters)
472-
if (whole ? (t === t.name.primary) : (isdefined(mod,tname) && t === getfield(mod,tname)))
474+
if (whole ? (t === unwrap_unionall(t.name.wrapper)) : (isdefined(mod,tname) && t === getfield(mod,tname)))
473475
serialize(s, svec())
474476
else
475477
serialize(s, t.parameters)
@@ -778,12 +780,13 @@ function deserialize_typename(s::AbstractSerializer, number)
778780
tn.names = names
779781
# TODO: there's an unhanded cycle in the dependency graph at this point:
780782
# while deserializing super and/or types, we may have encountered
781-
# tn.primary and throw UndefRefException before we get to this point
782-
tn.primary = ccall(:jl_new_datatype, Any, (Any, Any, Any, Any, Any, Cint, Cint, Cint),
783-
tn, super, parameters, names, types,
784-
abstr, mutable, ninitialized)
785-
ty = tn.primary
786-
ccall(:jl_set_const, Void, (Any, Any, Any), tn.module, tn.name, ty)
783+
# tn.wrapper and throw UndefRefException before we get to this point
784+
ndt = ccall(:jl_new_datatype, Any, (Any, Any, Any, Any, Any, Cint, Cint, Cint),
785+
tn, super, parameters, names, types,
786+
abstr, mutable, ninitialized)
787+
tn.wrapper = ndt.name.wrapper
788+
ccall(:jl_set_const, Void, (Any, Any, Any), tn.module, tn.name, tn.wrapper)
789+
ty = tn.wrapper
787790
if has_instance && !isdefined(ty, :instance)
788791
# use setfield! directly to avoid `fieldtype` lowering expecting to see a Singleton object already on ty
789792
Core.setfield!(ty, :instance, ccall(:jl_new_struct, Any, (Any, Any...), ty))
@@ -816,7 +819,7 @@ function deserialize_datatype(s::AbstractSerializer)
816819
form = read(s.io, UInt8)::UInt8
817820
if (form&2) != 0
818821
tname = deserialize(s)::TypeName
819-
ty = tname.primary
822+
ty = tname.wrapper
820823
else
821824
name = deserialize(s)::Symbol
822825
mod = deserialize(s)::Module

base/show.jl

+28-10
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,27 @@ function show(io::IO, x::Core.IntrinsicFunction)
174174
print(io, unsafe_string(name))
175175
end
176176

177+
show(io::IO, ::Core.BottomType) = print(io, "Union{}")
178+
177179
function show(io::IO, x::Union)
178180
print(io, "Union")
179-
sorted_types = sort!(collect(x.types); by=string)
181+
sorted_types = sort!(uniontypes(x); by=string)
180182
show_comma_array(io, sorted_types, '{', '}')
181183
end
182184

185+
function print_without_params(x::ANY)
186+
if isa(x,UnionAll)
187+
b = unwrap_unionall(x)
188+
return isa(b,DataType) && b.name.wrapper === x
189+
end
190+
return false
191+
end
192+
183193
function show(io::IO, x::UnionAll)
194+
has_tvar_env = get(io, :tvar_env, false) !== false
195+
if has_tvar_env && print_without_params(x)
196+
return show(io, unwrap_unionall(x).name)
197+
end
184198
show(io, x.body)
185199
print(io, " where ")
186200
show(io, x.var)
@@ -201,8 +215,7 @@ function show_datatype(io::IO, x::DataType)
201215
# and `true` if we are printing type parameters outside a method signature.
202216
has_tvar_env = get(io, :tvar_env, false) !== false
203217

204-
if ((!isempty(x.parameters) || x.name === Tuple.name) && x !== Tuple &&
205-
!(has_tvar_env && x.name.primary === x))
218+
if (!isempty(x.parameters) || x.name === Tuple.name) && x !== Tuple
206219
n = length(x.parameters)
207220

208221
# Print homogeneous tuples with more than 3 elements compactly as NTuple{N, T}
@@ -1069,14 +1082,19 @@ function show(io::IO, tv::TypeVar)
10691082
have_env = false
10701083
in_env = false
10711084
end
1072-
if !in_env && !is(tv.lb, Bottom)
1073-
show(io, tv.lb)
1085+
lb, ub = tv.lb, tv.ub
1086+
if !in_env && !is(lb, Bottom)
1087+
isa(lb,UnionAll) && print(io, "(")
1088+
show(io, lb)
1089+
isa(lb,UnionAll) && print(io, ")")
10741090
print(io, "<:")
10751091
end
10761092
write(io, tv.name)
1077-
if have_env ? !in_env : !is(tv.ub, Any)
1093+
if have_env ? !in_env : !is(ub, Any)
10781094
print(io, "<:")
1079-
show(io, tv.ub)
1095+
isa(ub,UnionAll) && print(io, "(")
1096+
show(io, ub)
1097+
isa(ub,UnionAll) && print(io, ")")
10801098
end
10811099
nothing
10821100
end
@@ -1197,7 +1215,7 @@ end
11971215

11981216
directsubtype(a::DataType, b::DataType) = supertype(a).name === b.name
11991217
directsubtype(a::UnionAll, b::DataType) = directsubtype(a.body, b)
1200-
directsubtype(a::Union, b::DataType) = any(t->directsubtype(t, b), a.types)
1218+
directsubtype(a::Union, b::DataType) = directsubtype(a.a, b) || directsubtype(a.b, b)
12011219
# Fallback to handle TypeVar's
12021220
directsubtype(a, b::DataType) = false
12031221
function dumpsubtypes(io::IO, x::DataType, m::Module, n::Int, indent)
@@ -1659,8 +1677,8 @@ end
16591677
# returns compact, prefix
16601678
function array_eltype_show_how(X)
16611679
e = eltype(X)
1662-
if isa(e,DataType) && e === e.name.primary
1663-
str = string(e.name) # Print "Array" rather than "Array{T,N}"
1680+
if print_without_params(e)
1681+
str = string(unwrap_unionall(e).name) # Print "Array" rather than "Array{T,N}"
16641682
else
16651683
str = string(e)
16661684
end

src/builtins.c

+8-3
Original file line numberDiff line numberDiff line change
@@ -1378,12 +1378,17 @@ static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt
13781378
n += jl_static_show_x(out, (jl_value_t*)((jl_unionall_t*)v)->var, depth);
13791379
}
13801380
else if (vt == jl_tvar_type) {
1381-
if (((jl_tvar_t*)v)->lb != jl_bottom_type) {
1382-
n += jl_static_show(out, ((jl_tvar_t*)v)->lb);
1381+
jl_value_t *lb = ((jl_tvar_t*)v)->lb, *ub = ((jl_tvar_t*)v)->ub;
1382+
if (lb != jl_bottom_type) {
1383+
if (jl_is_unionall(lb)) n += jl_printf(out, "(");
1384+
n += jl_static_show(out, lb);
1385+
if (jl_is_unionall(lb)) n += jl_printf(out, ")");
13831386
n += jl_printf(out, "<:");
13841387
}
13851388
n += jl_printf(out, "%s<:", jl_symbol_name(((jl_tvar_t*)v)->name));
1386-
n += jl_static_show(out, ((jl_tvar_t*)v)->ub);
1389+
if (jl_is_unionall(ub)) n += jl_printf(out, "(");
1390+
n += jl_static_show(out, ub);
1391+
if (jl_is_unionall(ub)) n += jl_printf(out, ")");
13871392
}
13881393
else if (vt == jl_module_type) {
13891394
jl_module_t *m = (jl_module_t*)v;

0 commit comments

Comments
 (0)