Skip to content

Commit 126763e

Browse files
committed
Merge pull request #11432 from JuliaLang/jb/curlyunion
merge Union and UnionType; allow writing union types with `Union{ }`
2 parents 99391b3 + 34d8f63 commit 126763e

22 files changed

+131
-148
lines changed

base/base.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ ccall(:jl_get_system_hooks, Void, ())
6666
==(w::WeakRef, v) = isequal(w.value, v)
6767
==(w, v::WeakRef) = isequal(w, v.value)
6868

69-
function finalizer(o::ANY, f::Union(Function,Ptr))
69+
function finalizer(o::ANY, f::Union{Function,Ptr})
7070
if isimmutable(o)
7171
error("objects of type ", typeof(o), " cannot be finalized")
7272
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

@@ -126,7 +126,7 @@ import Core.Intrinsics.ccall
126126
export
127127
# key types
128128
Any, DataType, Vararg, ANY, NTuple,
129-
Tuple, Type, TypeConstructor, TypeName, TypeVar, Union, UnionType, Void,
129+
Tuple, Type, TypeConstructor, TypeName, TypeVar, Union, Void,
130130
SimpleVector, AbstractArray, DenseArray,
131131
# special objects
132132
Box, Function, IntrinsicFunction, LambdaStaticData, Method, MethodTable,
@@ -249,7 +249,7 @@ immutable UTF8String <: AbstractString
249249
data::Array{UInt8,1}
250250
end
251251

252-
typealias ByteString Union(ASCIIString,UTF8String)
252+
typealias ByteString Union{ASCIIString,UTF8String}
253253

254254
include(fname::ByteString) = ccall(:jl_load_, Any, (Any,), fname)
255255

@@ -262,14 +262,14 @@ type WeakRef
262262
end
263263

264264
TypeVar(n::Symbol) =
265-
ccall(:jl_new_typevar, Any, (Any, Any, Any), n, Union(), Any)::TypeVar
265+
ccall(:jl_new_typevar, Any, (Any, Any, Any), n, Union{}, Any)::TypeVar
266266
TypeVar(n::Symbol, ub::ANY) =
267267
(isa(ub,Bool) ?
268-
ccall(:jl_new_typevar_, Any, (Any, Any, Any, Any), n, Union(), Any, ub)::TypeVar :
269-
ccall(:jl_new_typevar, Any, (Any, Any, Any), n, Union(), ub::Type)::TypeVar)
268+
ccall(:jl_new_typevar_, Any, (Any, Any, Any, Any), n, Union{}, Any, ub)::TypeVar :
269+
ccall(:jl_new_typevar, Any, (Any, Any, Any), n, Union{}, ub::Type)::TypeVar)
270270
TypeVar(n::Symbol, lb::ANY, ub::ANY) =
271271
(isa(ub,Bool) ?
272-
ccall(:jl_new_typevar_, Any, (Any, Any, Any, Any), n, Union(), lb::Type, ub)::TypeVar :
272+
ccall(:jl_new_typevar_, Any, (Any, Any, Any, Any), n, Union{}, lb::Type, ub)::TypeVar :
273273
ccall(:jl_new_typevar, Any, (Any, Any, Any), n, lb::Type, ub::Type)::TypeVar)
274274
TypeVar(n::Symbol, lb::ANY, ub::ANY, b::Bool) =
275275
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(i->deepcopy_internal(x[i], stackdict), length(x))

base/deprecated.jl

+3
Original file line numberDiff line numberDiff line change
@@ -527,3 +527,6 @@ end
527527
function start_timer(t, d, r)
528528
error("start_timer is deprecated. Use Timer(callback, delay, repeat) instead.")
529529
end
530+
531+
const UnionType = Union
532+
export UnionType

base/essentials.jl

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
abstract IO
44

5-
typealias Callable Union(Function,DataType)
5+
typealias Callable Union{Function,DataType}
66

7-
const Bottom = Union()
7+
const Bottom = Union{}
8+
9+
call(::Type{Union}, args...) = Union{args...}
810

911
# The real @inline macro is not available until after array.jl, so this
1012
# internal macro splices the meta Expr directly into the function body.

base/inference.jl

+38-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
@@ -158,19 +158,13 @@ add_tfunc(eval(Core.Intrinsics,:cglobal), 1, 2,
158158
isType(t[1]) ? Ptr{t[1].parameters[1]} : Ptr))
159159
add_tfunc(eval(Core.Intrinsics,:select_value), 3, 3,
160160
# TODO: return Bottom if cnd is definitely not a Bool
161-
(cnd, x, y)->Union(x,y))
161+
(cnd, x, y)->Union{x,y})
162162
add_tfunc(is, 2, 2, cmp_tfunc)
163163
add_tfunc(issubtype, 2, 2, cmp_tfunc)
164164
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)
@@ -376,17 +370,30 @@ has_typevars(t::ANY) = ccall(:jl_has_typevars, Cint, (Any,), t)!=0
376370
# TODO: handle e.g. apply_type(T, R::Union(Type{Int32},Type{Float64}))
377371
const apply_type_tfunc = function (A, args...)
378372
if !isType(args[1])
379-
return Type
373+
return Any
380374
end
381375
headtype = args[1].parameters[1]
382-
if isa(headtype,UnionType) || isa(headtype,TypeVar)
376+
if isa(headtype,Union) || isa(headtype,TypeVar)
383377
return args[1]
384378
end
379+
largs = length(args)
380+
if headtype === Union
381+
largs == 1 && return Type{Bottom}
382+
largs == 2 && return args[2]
383+
args = args[2:end]
384+
if all(isType, args)
385+
return Type{Union{map(t->t.parameters[1],args)...}}
386+
else
387+
return Any
388+
end
389+
elseif Union <: headtype
390+
return Any
391+
end
385392
istuple = (headtype === Tuple)
386393
uncertain = false
387394
lA = length(A)
388395
tparams = svec()
389-
for i=2:max(lA,length(args))
396+
for i=2:max(lA,largs)
390397
ai = args[i]
391398
if isType(ai)
392399
aip1 = ai.parameters[1]
@@ -563,10 +570,10 @@ const isconstantref = isconstantfunc
563570
const limit_tuple_depth = t->limit_tuple_depth_(t,0)
564571

565572
const limit_tuple_depth_ = function (t,d::Int)
566-
if isa(t,UnionType)
573+
if isa(t,Union)
567574
# also limit within Union types.
568575
# may have to recur into other stuff in the future too.
569-
return Union(map(x->limit_tuple_depth_(x,d+1), t.types)...)
576+
return Union{map(x->limit_tuple_depth_(x,d+1), t.types)...}
570577
end
571578
if !(isa(t,DataType) && t.name === Tuple.name)
572579
return t
@@ -1088,7 +1095,7 @@ end
10881095
typealias VarTable ObjectIdDict
10891096

10901097
type StateUpdate
1091-
var::Union(Symbol,GenSym)
1098+
var::Union{Symbol,GenSym}
10921099
vtype
10931100
state::VarTable
10941101
end
@@ -1130,7 +1137,7 @@ function type_too_complex(t::ANY, d)
11301137
if d > MAX_TYPE_DEPTH
11311138
return true
11321139
end
1133-
if isa(t,UnionType)
1140+
if isa(t,Union)
11341141
p = t.types
11351142
elseif isa(t,DataType)
11361143
p = t.parameters
@@ -1158,7 +1165,7 @@ function tmerge(typea::ANY, typeb::ANY)
11581165
end
11591166
return Tuple
11601167
end
1161-
u = Union(typea, typeb)
1168+
u = Union{typea, typeb}
11621169
if length(u.types) > MAX_TYPEUNION_LEN || type_too_complex(u, 0)
11631170
# don't let type unions get too big
11641171
# TODO: something smarter, like a common supertype
@@ -1169,7 +1176,7 @@ end
11691176

11701177
issubstate(a::VarState,b::VarState) = (a.typ <: b.typ && a.undef <= b.undef)
11711178

1172-
function smerge(sa::Union(NotFound,VarState), sb::Union(NotFound,VarState))
1179+
function smerge(sa::Union{NotFound,VarState}, sb::Union{NotFound,VarState})
11731180
is(sa, NF) && return sb
11741181
is(sb, NF) && return sa
11751182
issubstate(sa,sb) && return sb
@@ -1183,7 +1190,7 @@ schanged(n::ANY, o::ANY) = is(o,NF) || (!is(n,NF) && !issubstate(n, o))
11831190
stupdate(state::Tuple{}, changes::VarTable, vars) = copy(changes)
11841191
stupdate(state::Tuple{}, changes::StateUpdate, vars) = stupdate(ObjectIdDict(), changes, vars)
11851192

1186-
function stupdate(state::ObjectIdDict, changes::Union(StateUpdate,VarTable), vars)
1193+
function stupdate(state::ObjectIdDict, changes::Union{StateUpdate,VarTable}, vars)
11871194
for i = 1:length(vars)
11881195
v = vars[i]
11891196
newtype = changes[v]
@@ -1195,7 +1202,7 @@ function stupdate(state::ObjectIdDict, changes::Union(StateUpdate,VarTable), var
11951202
state
11961203
end
11971204

1198-
function stchanged(new::Union(StateUpdate,VarTable), old, vars)
1205+
function stchanged(new::Union{StateUpdate,VarTable}, old, vars)
11991206
if is(old,())
12001207
return true
12011208
end
@@ -1680,7 +1687,7 @@ function typeinf_uncached(linfo::LambdaStaticData, atypes::ANY, sparams::SimpleV
16801687
end
16811688
for i = 1:length(gensym_types)
16821689
if gensym_types[i] === NF
1683-
gensym_types[i] = Union()
1690+
gensym_types[i] = Union{}
16841691
end
16851692
end
16861693

@@ -1883,7 +1890,7 @@ end
18831890

18841891
function sym_replace(e::ANY, from1, from2, to1, to2)
18851892
if isa(e,Symbol) || isa(e,GenSym)
1886-
return _sym_repl(e::Union(Symbol,GenSym), from1, from2, to1, to2, e)
1893+
return _sym_repl(e::Union{Symbol,GenSym}, from1, from2, to1, to2, e)
18871894
end
18881895
if isa(e,SymbolNode)
18891896
e2 = _sym_repl(e.name, from1, from2, to1, to2, e)
@@ -1907,7 +1914,7 @@ function sym_replace(e::ANY, from1, from2, to1, to2)
19071914
if isa(e2, SymbolNode)
19081915
e2 = e2.name
19091916
end
1910-
e.args[1] = e2::Union(Symbol,GenSym)
1917+
e.args[1] = e2::Union{Symbol,GenSym}
19111918
end
19121919
e.args[2] = sym_replace(e.args[2], from1, from2, to1, to2)
19131920
elseif e.head !== :line
@@ -1918,7 +1925,7 @@ function sym_replace(e::ANY, from1, from2, to1, to2)
19181925
return e
19191926
end
19201927

1921-
function _sym_repl(s::Union(Symbol,GenSym), from1, from2, to1, to2, deflt)
1928+
function _sym_repl(s::Union{Symbol,GenSym}, from1, from2, to1, to2, deflt)
19221929
for i=1:length(from1)
19231930
if is(from1[i],s)
19241931
return to1[i]
@@ -1979,7 +1986,7 @@ function resolve_globals(e::ANY, locals, args, from, to, env1, env2)
19791986
# remove_redundant_temp_vars can only handle Symbols
19801987
# on the LHS of assignments, so we make sure not to put
19811988
# something else there
1982-
e2 = resolve_globals(e.args[1]::Union(Symbol,GenSym), locals, args, from, to, env1, env2)
1989+
e2 = resolve_globals(e.args[1]::Union{Symbol,GenSym}, locals, args, from, to, env1, env2)
19831990
if isa(e2, GlobalRef)
19841991
# abort when trying to inline a function which assigns to a global
19851992
# variable in a different module, since `Mod.X=V` isn't allowed
@@ -1989,7 +1996,7 @@ function resolve_globals(e::ANY, locals, args, from, to, env1, env2)
19891996
# resolve_globals(e.args[2], locals, args, from, to, env1, env2))
19901997
# e.typ = e2.typ
19911998
else
1992-
e.args[1] = e2::Union(Symbol,GenSym)
1999+
e.args[1] = e2::Union{Symbol,GenSym}
19932000
e.args[2] = resolve_globals(e.args[2], locals, args, from, to, env1, env2)
19942001
end
19952002
elseif !is(e.head,:line)
@@ -2236,12 +2243,6 @@ function inlineable(f::ANY, e::Expr, atype::ANY, sv::StaticVarInfo, enclosing_as
22362243
isleaftype(e.typ.parameters[1])
22372244
return (e.typ.parameters[1],())
22382245
end
2239-
if is(f,Union)
2240-
union = e.typ.parameters[1]
2241-
if isa(union,UnionType) && all(isleaftype, (union::UnionType).types)
2242-
return (union,())
2243-
end
2244-
end
22452246
end
22462247
if isa(f,IntrinsicFunction)
22472248
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
@@ -143,8 +143,8 @@ function _methods(f::ANY,t::Array,i,lim::Integer,matching::Array{Any,1})
143143
append!(matching, new::Array{Any,1})
144144
else
145145
ti = t[i]
146-
if isa(ti, UnionType)
147-
for ty in (ti::UnionType).types
146+
if isa(ti, Union)
147+
for ty in (ti::Union).types
148148
t[i] = ty
149149
if _methods(f,t,i-1,lim,matching) === false
150150
t[i] = ty

base/serialize.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export serialize, deserialize
1313
const TAGS = Any[
1414
Symbol, Int8, UInt8, Int16, UInt16, Int32, UInt32,
1515
Int64, UInt64, Int128, UInt128, Float32, Float64, Char, Ptr,
16-
DataType, UnionType, Function,
16+
DataType, Union, Function,
1717
Tuple, Array, Expr,
1818
#LongSymbol, LongTuple, LongExpr,
1919
Symbol, Tuple, Expr, # dummy entries, intentionally shadowed by earlier ones
@@ -613,9 +613,9 @@ function deserialize_expr(s::SerializationState, len)
613613
e
614614
end
615615

616-
function deserialize(s::SerializationState, ::Type{UnionType})
616+
function deserialize(s::SerializationState, ::Type{Union})
617617
types = deserialize(s)
618-
Union(types...)
618+
Union{types...}
619619
end
620620

621621
function deserialize_datatype(s::SerializationState)

0 commit comments

Comments
 (0)