Skip to content

Commit

Permalink
Avoid over-pessimization in apply_type_tfunc
Browse files Browse the repository at this point in the history
  • Loading branch information
martinholters committed May 17, 2018
1 parent cc68c6e commit 2c5e9c1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
22 changes: 15 additions & 7 deletions base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -724,20 +724,30 @@ function apply_type_tfunc(@nospecialize(headtypetype), @nospecialize args...)
elseif isconstType(headtypetype)
headtype = headtypetype.parameters[1]
else
return Any
return Type
end
largs = length(args)
if headtype === Union
largs == 0 && return Const(Bottom)
largs == 1 && return args[1]
hasnonType = false
for i = 1:largs
ai = args[i]
if !isa(ai, Const) || !isa(ai.val, Type)
if isa(ai, Const)
if !isa(ai.val, Type)
return Union{}
end
else
if !isType(ai)
return Any
if !isa(ai, Type) || ai <: Type || Type <: ai
hasnonType = true
else
return Union{}
end
end
end
end
largs == 1 && return (!isa(args[1], Type) || args[1] <: Type) ? args[1] : Type
hasnonType && return Type
ty = Union{}
allconst = true
for i = 1:largs
Expand All @@ -754,8 +764,7 @@ function apply_type_tfunc(@nospecialize(headtypetype), @nospecialize args...)
end
istuple = (headtype == Tuple)
if !istuple && !isa(headtype, UnionAll)
# TODO: return `Bottom` for trying to apply a non-UnionAll
return Any
return Union{}
end
uncertain = false
canconst = true
Expand All @@ -773,7 +782,6 @@ function apply_type_tfunc(@nospecialize(headtypetype), @nospecialize args...)
canconst = false
push!(tparams, ai.tv)
else
# TODO: return `Bottom` for trying to apply a non-UnionAll
uncertain = true
# These blocks improve type info but make compilation a bit slower.
# XXX
Expand Down
9 changes: 9 additions & 0 deletions test/compiler/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1581,3 +1581,12 @@ function _g_ifelse_isa_()
ifelse(isa(x, Nothing), 1, x)
end
@test Base.return_types(_g_ifelse_isa_, ()) == [Int]

# Don't pessimize apply_type to anything worse than Type and yield Bottom for invalid Unions
@test Core.Compiler.return_type(Core.apply_type, Tuple{Type{Union}}) == Type{Union{}}
@test Core.Compiler.return_type(Core.apply_type, Tuple{Type{Union},Any}) == Type
@test Core.Compiler.return_type(Core.apply_type, Tuple{Type{Union},Any,Any}) == Type
@test Core.Compiler.return_type(Core.apply_type, Tuple{Type{Union},Int}) == Union{}
@test Core.Compiler.return_type(Core.apply_type, Tuple{Type{Union},Any,Int}) == Union{}
@test Core.Compiler.return_type(Core.apply_type, Tuple{Any}) == Type
@test Core.Compiler.return_type(Core.apply_type, Tuple{Any,Any}) == Type

0 comments on commit 2c5e9c1

Please sign in to comment.