Skip to content

Commit

Permalink
fix return_type_tfunc in the case where no methods match
Browse files Browse the repository at this point in the history
also fix `return_type` on Builtins; this was another disparity with
  `return_type_tfunc`
  • Loading branch information
JeffBezanson committed Mar 17, 2017
1 parent e8e8012 commit 5484807
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
38 changes: 27 additions & 11 deletions base/inference.jl
Expand Up @@ -1081,12 +1081,13 @@ function tuple_tfunc(argtype::ANY)
return argtype
end

function builtin_tfunction(f::ANY, argtypes::Array{Any,1}, sv::InferenceState)
function builtin_tfunction(f::ANY, argtypes::Array{Any,1},
sv::Union{InferenceState,Void}, params::InferenceParams = sv.params)
isva = !isempty(argtypes) && isvarargtype(argtypes[end])
if f === tuple
for a in argtypes
if !isa(a, Const)
return tuple_tfunc(limit_tuple_depth(sv.params, argtypes_to_type(argtypes)))
return tuple_tfunc(limit_tuple_depth(params, argtypes_to_type(argtypes)))
end
end
return Const(tuple(anymap(a->a.val, argtypes)...))
Expand Down Expand Up @@ -1135,7 +1136,7 @@ function builtin_tfunction(f::ANY, argtypes::Array{Any,1}, sv::InferenceState)
else
sigty = nothing
end
if isa(sigty, Type) && sigty <: Tuple
if isa(sigty, Type) && sigty <: Tuple && sv !== nothing
return invoke_tfunc(af, sigty, argtypes_to_type(argtypes[3:end]), sv)
end
end
Expand Down Expand Up @@ -1517,21 +1518,29 @@ function return_type_tfunc(argtypes::ANY, vtypes::VarTable, sv::InferenceState)
if isa(tt, Const) || (isType(tt) && !has_free_typevars(tt))
aft = argtypes[2]
if isa(aft, Const) || (isType(aft) && !has_free_typevars(aft)) ||
(isleaftype(aft) && !(aft <: Builtin) && !(aft <: IntrinsicFunction))
(isleaftype(aft) && !(aft <: Builtin))
af_argtype = isa(tt, Const) ? tt.val : tt.parameters[1]
if isa(af_argtype, DataType) && af_argtype <: Tuple
argtypes_vec = Any[aft, af_argtype.parameters...]
astype = argtypes_to_type(argtypes_vec)
if !(aft Builtin) &&
_methods_by_ftype(astype, 0, sv.params.world,
UInt[typemin(UInt)], UInt[typemax(UInt)]) !== false
# return_type returns Bottom if no methods match, even though
# inference doesn't necessarily.
return Const(Bottom)
end
if isa(aft, Const)
rt = abstract_call(aft.val, (), argtypes_vec, vtypes, sv)
elseif isconstType(aft)
rt = abstract_call(aft.parameters[1], (), argtypes_vec, vtypes, sv)
else
rt = abstract_call_gf_by_type(nothing, argtypes_to_type(argtypes_vec), sv)
rt = abstract_call_gf_by_type(nothing, astype, sv)
end
if isa(rt, Const)
# output was computed to be constant
return Const(typeof(rt.val))
elseif isleaftype(rt)
elseif isleaftype(rt) || rt === Bottom
# output type was known for certain
return Const(rt)
elseif (isa(tt, Const) || isconstType(tt)) &&
Expand Down Expand Up @@ -5345,11 +5354,18 @@ end
function return_type(f::ANY, t::ANY)
params = InferenceParams(ccall(:jl_get_tls_world_age, UInt, ()))
rt = Union{}
for m in _methods(f, t, -1, params.world)
ty = typeinf_type(m[3], m[1], m[2], true, params)
ty === nothing && return Any
rt = tmerge(rt, ty)
rt === Any && break
if isa(f, Builtin)
rt = builtin_tfunction(f, Any[t.parameters...], nothing, params)
if isa(rt, TypeVar)
rt = rt.ub
end
else
for m in _methods(f, t, -1, params.world)
ty = typeinf_type(m[3], m[1], m[2], true, params)
ty === nothing && return Any
rt = tmerge(rt, ty)
rt === Any && break
end
end
return rt
end
Expand Down
9 changes: 9 additions & 0 deletions test/inference.jl
Expand Up @@ -684,3 +684,12 @@ Base.@pure function a20704(x)
end
aa20704(x) = x(nothing)
@test code_typed(aa20704, (typeof(a20704),))[1][1].pure

# issue #20033
# check return_type_tfunc for calls where no method matches
bcast_eltype_20033(f, A) = Core.Inference.return_type(f, Tuple{eltype(A)})
err20033(x::Float64...) = prod(x)
@test bcast_eltype_20033(err20033, [1]) === Union{}
@test Base.return_types(bcast_eltype_20033, (typeof(err20033), Vector{Int},)) == Any[Type{Union{}}]
# return_type on builtins
@test Core.Inference.return_type(tuple, Tuple{Int,Int8,Int}) === Tuple{Int,Int8,Int}

0 comments on commit 5484807

Please sign in to comment.