Skip to content

Commit 8c11d3c

Browse files
authored
tfuncs: Be more robust in the face of uninhabited types (#37945)
Fixes #37943
1 parent 6329dba commit 8c11d3c

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

base/compiler/tfuncs.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ function getfield_tfunc(@nospecialize(s00), @nospecialize(name))
822822
if isType(s) || !isa(s, DataType) || s.abstract
823823
return Any
824824
end
825+
s = s::DataType
825826
if s <: Tuple && name Symbol
826827
return Bottom
827828
end
@@ -831,6 +832,9 @@ function getfield_tfunc(@nospecialize(s00), @nospecialize(name))
831832
end
832833
return Any
833834
end
835+
# If no value has this type, then this statement should be unreachable.
836+
# Bail quickly now.
837+
s.has_concrete_subtype || return Union{}
834838
if s.name === _NAMEDTUPLE_NAME && !isconcretetype(s)
835839
if isa(name, Const) && isa(name.val, Symbol)
836840
if isa(s.parameters[1], Tuple)

src/jltypes.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,9 @@ static jl_svec_t *inst_ftypes(jl_svec_t *p, jl_typeenv_t *env, jl_typestack_t *s
15581558
jl_value_t *pi = jl_svecref(p, i);
15591559
JL_TRY {
15601560
pi = inst_type_w_(pi, env, stack, 1);
1561+
if (!jl_is_type(pi) && !jl_is_typevar(pi)) {
1562+
pi = jl_bottom_type;
1563+
}
15611564
}
15621565
JL_CATCH {
15631566
pi = jl_bottom_type;

test/compiler/inference.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2910,3 +2910,10 @@ end
29102910

29112911
# issue #37638
29122912
@test !(Core.Compiler.return_type(() -> (nothing, Any[]...)[2], Tuple{}) <: Vararg)
2913+
2914+
# Issue #37943
2915+
f37943(x::Any, i::Int) = getfield((x::Pair{false, Int}), i)
2916+
g37943(i::Int) = fieldtype(Pair{false, T} where T, i)
2917+
@test only(Base.return_types(f37943, Tuple{Any, Int})) === Union{}
2918+
@test only(Base.return_types(g37943, Tuple{Int})) === Union{Type{Union{}}, Type{Any}}
2919+

0 commit comments

Comments
 (0)