Skip to content

Commit

Permalink
fixes to show and serialize for new types
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Sep 13, 2016
1 parent 5c3c038 commit cb4ee8e
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 41 deletions.
4 changes: 2 additions & 2 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ for (Fun, func) in [(:IdFun, :identity),
(::Type{typeof($(func))})() = $(func)
end
end
@deprecate_binding CentralizedAbs2Fun typeof(centralizedabs2fun(0)).name.primary
(::Type{typeof(centralizedabs2fun(0)).name.primary})(m::Number) = centralizedabs2fun(m)
@deprecate_binding CentralizedAbs2Fun typeof(centralizedabs2fun(0)).name.wrapper
(::Type{typeof(centralizedabs2fun(0)).name.wrapper})(m::Number) = centralizedabs2fun(m)
@deprecate specialized_unary(f::Function) f
@deprecate specialized_binary(f::Function) f
@deprecate specialized_bitwise_unary(f::Function) f
Expand Down
16 changes: 10 additions & 6 deletions base/methodshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function argtype_decl(env, n, sig, i, nargs, isva) # -> (argname, argtype)
return s, ""
end
if isvarargtype(t)
t = unwrap_unionall(t)
tt, tn = t.parameters[1], t.parameters[2]
if isa(tn, TypeVar) && !tn.bound
if tt === Any || (isa(tt, TypeVar) && !tt.bound)
Expand All @@ -43,7 +44,8 @@ function arg_decl_parts(m::Method)
file, line = "", 0
if li !== nothing && isdefined(li, :slotnames)
argnames = li.slotnames[1:li.nargs]
decls = Any[argtype_decl(:tvar_env => tv, argnames[i], m.sig, i, li.nargs, li.isva)
sig = unwrap_unionall(m.sig)
decls = Any[argtype_decl(:tvar_env => tv, argnames[i], sig, i, li.nargs, li.isva)
for i = 1:li.nargs]
if isdefined(li, :def)
file, line = li.def.file, li.def.line
Expand Down Expand Up @@ -72,9 +74,10 @@ end

function show(io::IO, m::Method; kwtype::Nullable{DataType}=Nullable{DataType}())
tv, decls, file, line = arg_decl_parts(m)
ft = m.sig.parameters[1]
sig = unwrap_unionall(m.sig)
ft = sig.parameters[1]
d1 = decls[1]
if m.sig === Tuple
if sig === Tuple
print(io, m.name)
decls = Any[(), ("...", "")]
elseif ft <: Function &&
Expand All @@ -99,7 +102,7 @@ function show(io::IO, m::Method; kwtype::Nullable{DataType}=Nullable{DataType}()
join(io, [isempty(d[2]) ? d[1] : d[1]*"::"*d[2] for d in decls[2:end]],
", ", ", ")
if !isnull(kwtype)
kwargs = kwarg_decl(m.sig, get(kwtype))
kwargs = kwarg_decl(sig, get(kwtype))
if !isempty(kwargs)
print(io, "; ")
join(io, kwargs, ", ", ", ")
Expand Down Expand Up @@ -197,7 +200,8 @@ end

function show(io::IO, ::MIME"text/html", m::Method; kwtype::Nullable{DataType}=Nullable{DataType}())
tv, decls, file, line = arg_decl_parts(m)
ft = m.sig.parameters[1]
sig = unwrap_unionall(m.sig)
ft = sig.parameters[1]
d1 = decls[1]
if ft <: Function &&
isdefined(ft.name.module, ft.name.mt.name) &&
Expand All @@ -222,7 +226,7 @@ function show(io::IO, ::MIME"text/html", m::Method; kwtype::Nullable{DataType}=N
join(io, [isempty(d[2]) ? d[1] : d[1]*"::<b>"*d[2]*"</b>"
for d in decls[2:end]], ", ", ", ")
if !isnull(kwtype)
kwargs = kwarg_decl(m.sig, get(kwtype))
kwargs = kwarg_decl(sig, get(kwtype))
if !isempty(kwargs)
print(io, "; <i>")
join(io, kwargs, ", ", ", ")
Expand Down
5 changes: 3 additions & 2 deletions base/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,9 @@ function show_method_candidates(io::IO, ex::MethodError, kwargs::Vector=Any[])
for (func,arg_types_param) in funcs
for method in methods(func)
buf = IOBuffer()
s1 = method.sig.parameters[1]
sig = method.sig.parameters[2:end]
sig0 = unwrap_unionall(method.sig)
s1 = sig0.parameters[1]
sig = sig0.parameters[2:end]
print(buf, " ")
if !isa(func, s1)
# function itself doesn't match
Expand Down
39 changes: 21 additions & 18 deletions base/serialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ const TAGS = Any[
#LongSymbol, LongTuple, LongExpr,
Symbol, Tuple, Expr, # dummy entries, intentionally shadowed by earlier ones
LineNumberNode, Slot, LabelNode, GotoNode,
QuoteNode, :reserved23 #=was TopNode=#, TypeVar, Core.Box, LambdaInfo,
QuoteNode, UnionAll, TypeVar, Core.Box, LambdaInfo,
Module, #=UndefRefTag=#Symbol, Task, String, Float16,
SimpleVector, #=BackrefTag=#Symbol, Method, GlobalRef,

(), Bool, Any, :Any, Bottom, :reserved21, :reserved22, Type,
(), Bool, Any, :Any, Bottom, Core.BottomType, :reserved22, Type,
:Array, :TypeVar, :Box,
:lambda, :body, :return, :call, Symbol("::"),
:(=), :null, :gotoifnot, :A, :B, :C, :M, :N, :T, :S, :X, :Y,
Expand Down Expand Up @@ -384,7 +384,8 @@ function serialize(s::AbstractSerializer, g::GlobalRef)
writetag(s.io, GLOBALREF_TAG)
if g.mod === Main && isdefined(g.mod, g.name) && isconst(g.mod, g.name)
v = getfield(g.mod, g.name)
if isa(v, DataType) && v === v.name.primary && should_send_whole_type(s, v)
unw = unwrap_unionall(v)
if isa(unw,DataType) && v === unw.name.wrapper && should_send_whole_type(s, unw)
# handle references to types in Main by sending the whole type.
# needed to be able to send nested functions (#15451).
write(s.io, UInt8(1))
Expand All @@ -408,13 +409,14 @@ end
function serialize_typename(s::AbstractSerializer, t::TypeName)
serialize(s, t.name)
serialize(s, t.names)
serialize(s, t.primary.super)
serialize(s, t.primary.parameters)
serialize(s, t.primary.types)
serialize(s, isdefined(t.primary, :instance))
serialize(s, t.primary.abstract)
serialize(s, t.primary.mutable)
serialize(s, t.primary.ninitialized)
primary = unwrap_unionall(t.wrapper)
serialize(s, primary.super)
serialize(s, primary.parameters)
serialize(s, primary.types)
serialize(s, isdefined(primary, :instance))
serialize(s, primary.abstract)
serialize(s, primary.mutable)
serialize(s, primary.ninitialized)
if isdefined(t, :mt)
serialize(s, t.mt.name)
serialize(s, t.mt.defs)
Expand Down Expand Up @@ -469,7 +471,7 @@ function serialize_type_data(s, t::DataType, type_itself::Bool)
serialize(s, mod)
end
if !isempty(t.parameters)
if (whole ? (t === t.name.primary) : (isdefined(mod,tname) && t === getfield(mod,tname)))
if (whole ? (t === unwrap_unionall(t.name.wrapper)) : (isdefined(mod,tname) && t === getfield(mod,tname)))
serialize(s, svec())
else
serialize(s, t.parameters)
Expand Down Expand Up @@ -778,12 +780,13 @@ function deserialize_typename(s::AbstractSerializer, number)
tn.names = names
# TODO: there's an unhanded cycle in the dependency graph at this point:
# while deserializing super and/or types, we may have encountered
# tn.primary and throw UndefRefException before we get to this point
tn.primary = ccall(:jl_new_datatype, Any, (Any, Any, Any, Any, Any, Cint, Cint, Cint),
tn, super, parameters, names, types,
abstr, mutable, ninitialized)
ty = tn.primary
ccall(:jl_set_const, Void, (Any, Any, Any), tn.module, tn.name, ty)
# tn.wrapper and throw UndefRefException before we get to this point
ndt = ccall(:jl_new_datatype, Any, (Any, Any, Any, Any, Any, Cint, Cint, Cint),
tn, super, parameters, names, types,
abstr, mutable, ninitialized)
tn.wrapper = ndt.name.wrapper
ccall(:jl_set_const, Void, (Any, Any, Any), tn.module, tn.name, tn.wrapper)
ty = tn.wrapper
if has_instance && !isdefined(ty, :instance)
# use setfield! directly to avoid `fieldtype` lowering expecting to see a Singleton object already on ty
Core.setfield!(ty, :instance, ccall(:jl_new_struct, Any, (Any, Any...), ty))
Expand Down Expand Up @@ -816,7 +819,7 @@ function deserialize_datatype(s::AbstractSerializer)
form = read(s.io, UInt8)::UInt8
if (form&2) != 0
tname = deserialize(s)::TypeName
ty = tname.primary
ty = tname.wrapper
else
name = deserialize(s)::Symbol
mod = deserialize(s)::Module
Expand Down
38 changes: 28 additions & 10 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,27 @@ function show(io::IO, x::Core.IntrinsicFunction)
print(io, unsafe_string(name))
end

show(io::IO, ::Core.BottomType) = print(io, "Union{}")

function show(io::IO, x::Union)
print(io, "Union")
sorted_types = sort!(collect(x.types); by=string)
sorted_types = sort!(uniontypes(x); by=string)
show_comma_array(io, sorted_types, '{', '}')
end

function print_without_params(x::ANY)
if isa(x,UnionAll)
b = unwrap_unionall(x)
return isa(b,DataType) && b.name.wrapper === x
end
return false
end

function show(io::IO, x::UnionAll)
has_tvar_env = get(io, :tvar_env, false) !== false
if has_tvar_env && print_without_params(x)
return show(io, unwrap_unionall(x).name)
end
show(io, x.body)
print(io, " where ")
show(io, x.var)
Expand All @@ -201,8 +215,7 @@ function show_datatype(io::IO, x::DataType)
# and `true` if we are printing type parameters outside a method signature.
has_tvar_env = get(io, :tvar_env, false) !== false

if ((!isempty(x.parameters) || x.name === Tuple.name) && x !== Tuple &&
!(has_tvar_env && x.name.primary === x))
if (!isempty(x.parameters) || x.name === Tuple.name) && x !== Tuple
n = length(x.parameters)

# Print homogeneous tuples with more than 3 elements compactly as NTuple{N, T}
Expand Down Expand Up @@ -1069,14 +1082,19 @@ function show(io::IO, tv::TypeVar)
have_env = false
in_env = false
end
if !in_env && !is(tv.lb, Bottom)
show(io, tv.lb)
lb, ub = tv.lb, tv.ub
if !in_env && !is(lb, Bottom)
isa(lb,UnionAll) && print(io, "(")
show(io, lb)
isa(lb,UnionAll) && print(io, ")")
print(io, "<:")
end
write(io, tv.name)
if have_env ? !in_env : !is(tv.ub, Any)
if have_env ? !in_env : !is(ub, Any)
print(io, "<:")
show(io, tv.ub)
isa(ub,UnionAll) && print(io, "(")
show(io, ub)
isa(ub,UnionAll) && print(io, ")")
end
nothing
end
Expand Down Expand Up @@ -1197,7 +1215,7 @@ end

directsubtype(a::DataType, b::DataType) = supertype(a).name === b.name
directsubtype(a::UnionAll, b::DataType) = directsubtype(a.body, b)
directsubtype(a::Union, b::DataType) = any(t->directsubtype(t, b), a.types)
directsubtype(a::Union, b::DataType) = directsubtype(a.a, b) || directsubtype(a.b, b)
# Fallback to handle TypeVar's
directsubtype(a, b::DataType) = false
function dumpsubtypes(io::IO, x::DataType, m::Module, n::Int, indent)
Expand Down Expand Up @@ -1659,8 +1677,8 @@ end
# returns compact, prefix
function array_eltype_show_how(X)
e = eltype(X)
if isa(e,DataType) && e === e.name.primary
str = string(e.name) # Print "Array" rather than "Array{T,N}"
if print_without_params(e)
str = string(unwrap_unionall(e).name) # Print "Array" rather than "Array{T,N}"
else
str = string(e)
end
Expand Down
11 changes: 8 additions & 3 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -1378,12 +1378,17 @@ static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt
n += jl_static_show_x(out, (jl_value_t*)((jl_unionall_t*)v)->var, depth);
}
else if (vt == jl_tvar_type) {
if (((jl_tvar_t*)v)->lb != jl_bottom_type) {
n += jl_static_show(out, ((jl_tvar_t*)v)->lb);
jl_value_t *lb = ((jl_tvar_t*)v)->lb, *ub = ((jl_tvar_t*)v)->ub;
if (lb != jl_bottom_type) {
if (jl_is_unionall(lb)) n += jl_printf(out, "(");
n += jl_static_show(out, lb);
if (jl_is_unionall(lb)) n += jl_printf(out, ")");
n += jl_printf(out, "<:");
}
n += jl_printf(out, "%s<:", jl_symbol_name(((jl_tvar_t*)v)->name));
n += jl_static_show(out, ((jl_tvar_t*)v)->ub);
if (jl_is_unionall(ub)) n += jl_printf(out, "(");
n += jl_static_show(out, ub);
if (jl_is_unionall(ub)) n += jl_printf(out, ")");
}
else if (vt == jl_module_type) {
jl_module_t *m = (jl_module_t*)v;
Expand Down

0 comments on commit cb4ee8e

Please sign in to comment.