Skip to content

Commit 8e99085

Browse files
committed
merge Union and UnionType; allow writing union types with Union{ }
1 parent f473737 commit 8e99085

21 files changed

+135
-145
lines changed

base/base.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ ccall(:jl_get_system_hooks, Void, ())
7070
==(w::WeakRef, v) = isequal(w.value, v)
7171
==(w, v::WeakRef) = isequal(w, v.value)
7272

73-
function finalizer(o::ANY, f::Union(Function,Ptr))
73+
function finalizer(o::ANY, f::Union{Function,Ptr})
7474
if isimmutable(o)
7575
error("objects of type ", typeof(o), " cannot be finalized")
7676
end

base/boot.jl

+7-7
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
# pointerfree::Bool
3131
#end
3232

33-
#type UnionType <: Type
33+
#type Union <: Type
3434
# types::Tuple
3535
#end
3636

@@ -121,7 +121,7 @@ import Core.Intrinsics.ccall
121121
export
122122
# key types
123123
Any, DataType, Vararg, ANY, NTuple,
124-
Tuple, Type, TypeConstructor, TypeName, TypeVar, Union, UnionType, Void,
124+
Tuple, Type, TypeConstructor, TypeName, TypeVar, Union, Void,
125125
SimpleVector, AbstractArray, DenseArray,
126126
# special objects
127127
Box, Function, IntrinsicFunction, LambdaStaticData, Method, MethodTable,
@@ -247,7 +247,7 @@ immutable UTF8String <: AbstractString
247247
data::Array{UInt8,1}
248248
end
249249

250-
typealias ByteString Union(ASCIIString,UTF8String)
250+
typealias ByteString Union{ASCIIString,UTF8String}
251251

252252
include(fname::ByteString) = ccall(:jl_load_, Any, (Any,), fname)
253253

@@ -260,14 +260,14 @@ type WeakRef
260260
end
261261

262262
TypeVar(n::Symbol) =
263-
ccall(:jl_new_typevar, Any, (Any, Any, Any), n, Union(), Any)::TypeVar
263+
ccall(:jl_new_typevar, Any, (Any, Any, Any), n, Union{}, Any)::TypeVar
264264
TypeVar(n::Symbol, ub::ANY) =
265265
(isa(ub,Bool) ?
266-
ccall(:jl_new_typevar_, Any, (Any, Any, Any, Any), n, Union(), Any, ub)::TypeVar :
267-
ccall(:jl_new_typevar, Any, (Any, Any, Any), n, Union(), ub::Type)::TypeVar)
266+
ccall(:jl_new_typevar_, Any, (Any, Any, Any, Any), n, Union{}, Any, ub)::TypeVar :
267+
ccall(:jl_new_typevar, Any, (Any, Any, Any), n, Union{}, ub::Type)::TypeVar)
268268
TypeVar(n::Symbol, lb::ANY, ub::ANY) =
269269
(isa(ub,Bool) ?
270-
ccall(:jl_new_typevar_, Any, (Any, Any, Any, Any), n, Union(), lb::Type, ub)::TypeVar :
270+
ccall(:jl_new_typevar_, Any, (Any, Any, Any, Any), n, Union{}, lb::Type, ub)::TypeVar :
271271
ccall(:jl_new_typevar, Any, (Any, Any, Any), n, lb::Type, ub::Type)::TypeVar)
272272
TypeVar(n::Symbol, lb::ANY, ub::ANY, b::Bool) =
273273
ccall(:jl_new_typevar_, Any, (Any, Any, Any, Any), n, lb::Type, ub::Type, b)::TypeVar

base/deepcopy.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
deepcopy(x) = deepcopy_internal(x, ObjectIdDict())
99

1010
deepcopy_internal(x::Union(Symbol,LambdaStaticData,TopNode,QuoteNode,
11-
DataType,UnionType,Task),
11+
DataType,Union,Task),
1212
stackdict::ObjectIdDict) = x
1313
deepcopy_internal(x::Tuple, stackdict::ObjectIdDict) =
1414
ntuple(length(x), i->deepcopy_internal(x[i], stackdict))

base/deprecated.jl

+3
Original file line numberDiff line numberDiff line change
@@ -479,3 +479,6 @@ export float32_isvalid, float64_isvalid
479479
# 11379
480480

481481
@deprecate utf32(c::Integer...) UTF32String(UInt32[c...,0])
482+
483+
const UnionType = Union
484+
export UnionType

base/essentials.jl

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# This file is a part of Julia. License is MIT: http://julialang.org/license
22

3+
# TODO: deprecate
4+
call(::Type{Union}, args...) = Union{args...}
5+
36
abstract IO
47

58
typealias Callable Union(Function,DataType)

base/inference.jl

+44-37
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type CallStack
3636
recurred::Bool
3737
cycleid::Int
3838
result
39-
prev::Union(EmptyCallStack,CallStack)
39+
prev::Union{EmptyCallStack,CallStack}
4040
sv::StaticVarInfo
4141

4242
CallStack(ast, mod, types::ANY, prev) = new(ast, mod, types, false, 0, Bottom, prev)
@@ -145,7 +145,7 @@ add_tfunc(getfield(Core.Intrinsics,:ccall), 3, IInf,
145145
if isa(t,DataType) && is((t::DataType).name,Ref.name)
146146
t = t.parameters[1]
147147
if is(t,Any)
148-
return Union() # a return type of Box{Any} is invalid
148+
return Union{} # a return type of Box{Any} is invalid
149149
end
150150
return t
151151
end
@@ -165,12 +165,6 @@ add_tfunc(isa, 2, 2, cmp_tfunc)
165165
add_tfunc(isdefined, 1, IInf, (args...)->Bool)
166166
add_tfunc(Core.sizeof, 1, 1, x->Int)
167167
add_tfunc(nfields, 1, 1, x->Int)
168-
add_tfunc(Union, 0, IInf,
169-
(args...)->(if all(isType,args)
170-
Type{Union(map(t->t.parameters[1],args)...)}
171-
else
172-
Type
173-
end))
174168
add_tfunc(_expr, 1, IInf, (args...)->Expr)
175169
add_tfunc(method_exists, 2, 2, cmp_tfunc)
176170
add_tfunc(applicable, 1, IInf, (f, args...)->Bool)
@@ -196,8 +190,8 @@ const typeof_tfunc = function (t)
196190
else
197191
Type{TypeVar(:_,t)}
198192
end
199-
elseif isa(t,UnionType)
200-
Union(map(typeof_tfunc, t.types)...)
193+
elseif isa(t,Union)
194+
Union{map(typeof_tfunc, t.types)...}
201195
elseif isa(t,TypeVar)
202196
Type{t}
203197
else
@@ -215,12 +209,12 @@ function limit_type_depth(t::ANY, d::Int, cov::Bool, vars)
215209
return t
216210
end
217211
inexact = !cov && d > MAX_TYPE_DEPTH
218-
if isa(t,UnionType)
212+
if isa(t,Union)
219213
t === Bottom && return t
220214
if d > MAX_TYPE_DEPTH
221215
R = Any
222216
else
223-
R = Union(map(x->limit_type_depth(x, d+1, cov, vars), t.types)...)
217+
R = Union{map(x->limit_type_depth(x, d+1, cov, vars), t.types)...}
224218
end
225219
elseif isa(t,DataType)
226220
P = t.parameters
@@ -254,7 +248,7 @@ const getfield_tfunc = function (A, s0, name)
254248
return Any, false
255249
end
256250
end
257-
if isa(s,UnionType)
251+
if isa(s,Union)
258252
return reduce(tmerge, Bottom, map(t->getfield_tfunc(A, t, name)[1], s.types)), false
259253
end
260254
if !isa(s,DataType)
@@ -371,20 +365,39 @@ function extract_simple_tparam(Ai)
371365
return Bottom
372366
end
373367

374-
# TODO: handle e.g. apply_type(T, R::Union(Type{Int32},Type{Float64}))
368+
# TODO: handle e.g. apply_type(T, R::Union{Type{Int32},Type{Float64}})
375369
const apply_type_tfunc = function (A, args...)
376370
if !isType(args[1])
377371
return Type
378372
end
379373
headtype = args[1].parameters[1]
380-
if isa(headtype,UnionType) || isa(headtype,TypeVar)
374+
if isa(headtype,Union)
375+
return Bottom
376+
end
377+
if isa(headtype,TypeVar)
378+
if Union <: args[1]
379+
return Type
380+
end
381381
return args[1]
382382
end
383+
largs = length(args)
384+
if headtype === Union
385+
largs == 1 && return Type{Bottom}
386+
largs == 2 && return args[2]
387+
args = args[2:end]
388+
if all(isType, args)
389+
return Type{Union{map(t->t.parameters[1],args)...}}
390+
else
391+
return Type
392+
end
393+
elseif Union <: headtype
394+
return Type
395+
end
383396
istuple = (headtype === Tuple)
384397
uncertain = false
385398
lA = length(A)
386399
tparams = svec()
387-
for i=2:max(lA,length(args))
400+
for i=2:max(lA,largs)
388401
ai = args[i]
389402
if isType(ai)
390403
uncertain |= (!isleaftype(ai))
@@ -560,10 +573,10 @@ const isconstantref = isconstantfunc
560573
const limit_tuple_depth = t->limit_tuple_depth_(t,0)
561574

562575
const limit_tuple_depth_ = function (t,d::Int)
563-
if isa(t,UnionType)
576+
if isa(t,Union)
564577
# also limit within Union types.
565578
# may have to recur into other stuff in the future too.
566-
return Union(map(x->limit_tuple_depth_(x,d+1), t.types)...)
579+
return Union{map(x->limit_tuple_depth_(x,d+1), t.types)...}
567580
end
568581
if !(isa(t,DataType) && t.name === Tuple.name)
569582
return t
@@ -1092,7 +1105,7 @@ end
10921105
typealias VarTable ObjectIdDict
10931106

10941107
type StateUpdate
1095-
var::Union(Symbol,GenSym)
1108+
var::Union{Symbol,GenSym}
10961109
vtype
10971110
state::VarTable
10981111
end
@@ -1132,7 +1145,7 @@ function type_too_complex(t::ANY, d)
11321145
if d > MAX_TYPE_DEPTH
11331146
return true
11341147
end
1135-
if isa(t,UnionType)
1148+
if isa(t,Union)
11361149
p = t.types
11371150
elseif isa(t,DataType)
11381151
p = t.parameters
@@ -1160,7 +1173,7 @@ function tmerge(typea::ANY, typeb::ANY)
11601173
end
11611174
return Tuple
11621175
end
1163-
u = Union(typea, typeb)
1176+
u = Union{typea, typeb}
11641177
if length(u.types) > MAX_TYPEUNION_LEN || type_too_complex(u, 0)
11651178
# don't let type unions get too big
11661179
# TODO: something smarter, like a common supertype
@@ -1171,7 +1184,7 @@ end
11711184

11721185
issubstate(a::VarState,b::VarState) = (a.typ <: b.typ && a.undef <= b.undef)
11731186

1174-
function smerge(sa::Union(NotFound,VarState), sb::Union(NotFound,VarState))
1187+
function smerge(sa::Union{NotFound,VarState}, sb::Union{NotFound,VarState})
11751188
is(sa, NF) && return sb
11761189
is(sb, NF) && return sa
11771190
issubstate(sa,sb) && return sb
@@ -1185,7 +1198,7 @@ schanged(n::ANY, o::ANY) = is(o,NF) || (!is(n,NF) && !issubstate(n, o))
11851198
stupdate(state::Tuple{}, changes::VarTable, vars) = copy(changes)
11861199
stupdate(state::Tuple{}, changes::StateUpdate, vars) = stupdate(ObjectIdDict(), changes, vars)
11871200

1188-
function stupdate(state::ObjectIdDict, changes::Union(StateUpdate,VarTable), vars)
1201+
function stupdate(state::ObjectIdDict, changes::Union{StateUpdate,VarTable}, vars)
11891202
for i = 1:length(vars)
11901203
v = vars[i]
11911204
newtype = changes[v]
@@ -1197,7 +1210,7 @@ function stupdate(state::ObjectIdDict, changes::Union(StateUpdate,VarTable), var
11971210
state
11981211
end
11991212

1200-
function stchanged(new::Union(StateUpdate,VarTable), old, vars)
1213+
function stchanged(new::Union{StateUpdate,VarTable}, old, vars)
12011214
if is(old,())
12021215
return true
12031216
end
@@ -1683,7 +1696,7 @@ function typeinf_uncached(linfo::LambdaStaticData, atypes::ANY, sparams::SimpleV
16831696
end
16841697
for i = 1:length(gensym_types)
16851698
if gensym_types[i] === NF
1686-
gensym_types[i] = Union()
1699+
gensym_types[i] = Union{}
16871700
end
16881701
end
16891702

@@ -1886,7 +1899,7 @@ end
18861899

18871900
function sym_replace(e::ANY, from1, from2, to1, to2)
18881901
if isa(e,Symbol) || isa(e,GenSym)
1889-
return _sym_repl(e::Union(Symbol,GenSym), from1, from2, to1, to2, e)
1902+
return _sym_repl(e::Union{Symbol,GenSym}, from1, from2, to1, to2, e)
18901903
end
18911904
if isa(e,SymbolNode)
18921905
e2 = _sym_repl(e.name, from1, from2, to1, to2, e)
@@ -1905,12 +1918,12 @@ function sym_replace(e::ANY, from1, from2, to1, to2)
19051918
# on the LHS of assignments, so we make sure not to put
19061919
# something else there
19071920
@assert length(e.args) == 2
1908-
s = e.args[1]::Union(Symbol,GenSym)
1921+
s = e.args[1]::Union{Symbol,GenSym}
19091922
e2 = _sym_repl(s, from1, from2, to1, to2, s)
19101923
if isa(e2, SymbolNode)
19111924
e2 = e2.name
19121925
end
1913-
e.args[1] = e2::Union(Symbol,GenSym)
1926+
e.args[1] = e2::Union{Symbol,GenSym}
19141927
e.args[2] = sym_replace(e.args[2], from1, from2, to1, to2)
19151928
elseif e.head !== :line
19161929
for i=1:length(e.args)
@@ -1920,7 +1933,7 @@ function sym_replace(e::ANY, from1, from2, to1, to2)
19201933
return e
19211934
end
19221935

1923-
function _sym_repl(s::Union(Symbol,GenSym), from1, from2, to1, to2, deflt)
1936+
function _sym_repl(s::Union{Symbol,GenSym}, from1, from2, to1, to2, deflt)
19241937
for i=1:length(from1)
19251938
if is(from1[i],s)
19261939
return to1[i]
@@ -1981,7 +1994,7 @@ function resolve_globals(e::ANY, locals, args, from, to, env1, env2)
19811994
# remove_redundant_temp_vars can only handle Symbols
19821995
# on the LHS of assignments, so we make sure not to put
19831996
# something else there
1984-
e2 = resolve_globals(e.args[1]::Union(Symbol,GenSym), locals, args, from, to, env1, env2)
1997+
e2 = resolve_globals(e.args[1]::Union{Symbol,GenSym}, locals, args, from, to, env1, env2)
19851998
if isa(e2, GlobalRef)
19861999
# abort when trying to inline a function which assigns to a global
19872000
# variable in a different module, since `Mod.X=V` isn't allowed
@@ -1991,7 +2004,7 @@ function resolve_globals(e::ANY, locals, args, from, to, env1, env2)
19912004
# resolve_globals(e.args[2], locals, args, from, to, env1, env2))
19922005
# e.typ = e2.typ
19932006
else
1994-
e.args[1] = e2::Union(Symbol,GenSym)
2007+
e.args[1] = e2::Union{Symbol,GenSym}
19952008
e.args[2] = resolve_globals(e.args[2], locals, args, from, to, env1, env2)
19962009
end
19972010
elseif !is(e.head,:line)
@@ -2224,12 +2237,6 @@ function inlineable(f::ANY, e::Expr, atype::ANY, sv::StaticVarInfo, enclosing_as
22242237
isleaftype(e.typ.parameters[1])
22252238
return (e.typ.parameters[1],())
22262239
end
2227-
if is(f,Union)
2228-
union = e.typ.parameters[1]
2229-
if isa(union,UnionType) && all(isleaftype, (union::UnionType).types)
2230-
return (union,())
2231-
end
2232-
end
22332240
end
22342241
if isa(f,IntrinsicFunction)
22352242
return NF

base/operators.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ eltype(x) = eltype(typeof(x))
174174

175175
# copying immutable things
176176
copy(x::Union(Symbol,Number,AbstractString,Function,Tuple,LambdaStaticData,
177-
TopNode,QuoteNode,DataType,UnionType)) = x
177+
TopNode,QuoteNode,DataType,Union)) = x
178178

179179
# function pipelining
180180
|>(x, f::Callable) = f(x)

base/promotion.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ function typejoin(a::ANY, b::ANY)
1919
if isa(b,TypeVar)
2020
return typejoin(a, b.ub)
2121
end
22-
if isa(a,UnionType) || isa(b,UnionType)
23-
u = Union(a, b)
24-
if !isa(u,UnionType)
22+
if isa(a,Union) || isa(b,Union)
23+
u = Union{a, b}
24+
if !isa(u,Union)
2525
return u
2626
end
2727
return reduce(typejoin, Bottom, u.types)

base/reducedim.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ reducedim_initarray0{T}(A::AbstractArray, region, v0::T) = reducedim_initarray0(
9292
#
9393
# The current scheme is basically following Steven G. Johnson's original implementation
9494
#
95-
promote_union(T::UnionType) = promote_type(T.types...)
95+
promote_union(T::Union) = promote_type(T.types...)
9696
promote_union(T) = T
9797
function reducedim_init{S}(f, op::AddFun, A::AbstractArray{S}, region)
9898
T = promote_union(S)

base/reflection.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ function _methods(f::ANY,t::Array,i,lim::Integer,matching::Array{Any,1})
140140
append!(matching, new::Array{Any,1})
141141
else
142142
ti = t[i]
143-
if isa(ti, UnionType)
144-
for ty in (ti::UnionType).types
143+
if isa(ti, Union)
144+
for ty in (ti::Union).types
145145
t[i] = ty
146146
if _methods(f,t,i-1,lim,matching) === false
147147
t[i] = ty

base/serialize.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ let i = 2
2222
for t = Any[
2323
Symbol, Int8, UInt8, Int16, UInt16, Int32, UInt32,
2424
Int64, UInt64, Int128, UInt128, Float32, Float64, Char, Ptr,
25-
DataType, UnionType, Function,
25+
DataType, Union, Function,
2626
Tuple, Array, Expr, LongSymbol, LongTuple, LongExpr,
2727
LineNumberNode, SymbolNode, LabelNode, GotoNode,
2828
QuoteNode, TopNode, TypeVar, Box, LambdaStaticData,
@@ -544,9 +544,9 @@ function deserialize_expr(s, len)
544544
e
545545
end
546546

547-
function deserialize(s, ::Type{UnionType})
547+
function deserialize(s, ::Type{Union})
548548
types = deserialize(s)
549-
Union(types...)
549+
Union{types...}
550550
end
551551

552552
function deserialize(s, ::Type{DataType})

0 commit comments

Comments
 (0)