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 22, 2018
1 parent cc68c6e commit a3f4331
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
26 changes: 19 additions & 7 deletions base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -724,20 +724,34 @@ 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)
if isa(ai.val, TypeVar) || isa(ai.val, PartialTypeVar)
return hasnonType = true
else
return Union{}
end
end
else
if !isType(ai)
return Any
if !isa(ai, Type) || typeintersect(ai, Type) != Union{}
hasnonType = true
else
return Union{}
end
end
end
end
largs == 1 && return isa(args[1], Type) ? typeintersect(args[1], Type) : Type
hasnonType && return Type
ty = Union{}
allconst = true
for i = 1:largs
Expand All @@ -754,8 +768,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 +786,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 a3f4331

Please sign in to comment.