diff --git a/base/compiler/tfuncs.jl b/base/compiler/tfuncs.jl index f8db243ce4dae..ac8aa0c639dc5 100644 --- a/base/compiler/tfuncs.jl +++ b/base/compiler/tfuncs.jl @@ -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 @@ -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) diff --git a/src/jltypes.c b/src/jltypes.c index 31a06e2bdc092..b7a58a3610027 100644 --- a/src/jltypes.c +++ b/src/jltypes.c @@ -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; diff --git a/test/compiler/inference.jl b/test/compiler/inference.jl index 24ddd9a13dc1b..2758e510c9380 100644 --- a/test/compiler/inference.jl +++ b/test/compiler/inference.jl @@ -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}} +