Skip to content

Commit

Permalink
tfuncs: Be more robust in the face of uninhabited types
Browse files Browse the repository at this point in the history
Fixes #37943
  • Loading branch information
Keno committed Oct 18, 2020
1 parent 2655a3a commit 9d9d4c3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ function getfield_tfunc(@nospecialize(s00), @nospecialize(name))
if isType(s) || !isa(s, DataType) || s.abstract
return Any
end
s = s::DataType
if s <: Tuple && name Symbol
return Bottom
end
Expand All @@ -831,6 +832,9 @@ function getfield_tfunc(@nospecialize(s00), @nospecialize(name))
end
return Any
end
# If no value has this type, then this statement should be unreachable.
# Bail quickly now.
s.has_concrete_subtype || return Union{}
if s.name === _NAMEDTUPLE_NAME && !isconcretetype(s)
if isa(name, Const) && isa(name.val, Symbol)
if isa(s.parameters[1], Tuple)
Expand Down
3 changes: 3 additions & 0 deletions src/jltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,9 @@ static jl_svec_t *inst_ftypes(jl_svec_t *p, jl_typeenv_t *env, jl_typestack_t *s
jl_value_t *pi = jl_svecref(p, i);
JL_TRY {
pi = inst_type_w_(pi, env, stack, 1);
if (!jl_is_type(pi) && !jl_is_typevar(pi)) {
pi = jl_bottom_type;
}
}
JL_CATCH {
pi = jl_bottom_type;
Expand Down
7 changes: 7 additions & 0 deletions test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2910,3 +2910,10 @@ end

# issue #37638
@test !(Core.Compiler.return_type(() -> (nothing, Any[]...)[2], Tuple{}) <: Vararg)

# Issue #37943
f37943(x::Any, i::Int) = getfield((x::Pair{false, Int}), i)
g37943(i::Int) = fieldtype(Pair{false, T} where T, i)
@test only(Base.return_types(f37943, Tuple{Any, Int})) === Union{}
@test only(Base.return_types(g37943, Tuple{Int})) === Union{Type{Union{}}, Type{Any}}

0 comments on commit 9d9d4c3

Please sign in to comment.