From 30d11a3b2f4a557fe24cc7ba205b01dd7f0c3bc9 Mon Sep 17 00:00:00 2001 From: Martin Holters Date: Tue, 24 Jan 2023 22:16:49 +0100 Subject: [PATCH] Fix `apply_type_tfunc` for `Union{T::TypeVar}` (#48384) The type parameters to `Union` may be `Type`s or `TypeVar`s, but `apply_type_tfunc` failed to recognize the latter as valid in the single-argument case. --- base/compiler/tfuncs.jl | 2 +- test/compiler/inference.jl | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/base/compiler/tfuncs.jl b/base/compiler/tfuncs.jl index 1ff427a480a7d..1673929df1129 100644 --- a/base/compiler/tfuncs.jl +++ b/base/compiler/tfuncs.jl @@ -1713,7 +1713,7 @@ const _tvarnames = Symbol[:_A, :_B, :_C, :_D, :_E, :_F, :_G, :_H, :_I, :_J, :_K, end end if largs == 1 # Union{T} --> T - u1 = typeintersect(widenconst(args[1]), Type) + u1 = typeintersect(widenconst(args[1]), Union{Type,TypeVar}) valid_as_lattice(u1) || return Bottom return u1 end diff --git a/test/compiler/inference.jl b/test/compiler/inference.jl index 71f9903f85324..f70b1f73f55ad 100644 --- a/test/compiler/inference.jl +++ b/test/compiler/inference.jl @@ -2732,9 +2732,9 @@ end |> only === Int # correct `apply_type` inference of `NamedTuple{(), <:Any}` @test (() -> NamedTuple{(), <:Any})() isa UnionAll -# Don't pessimize apply_type to anything worse than Type and yield Bottom for invalid Unions +# Don't pessimize apply_type to anything worse than Type (or TypeVar) and yield Bottom for invalid Unions @test only(Base.return_types(Core.apply_type, Tuple{Type{Union}})) == Type{Union{}} -@test only(Base.return_types(Core.apply_type, Tuple{Type{Union},Any})) == Type +@test only(Base.return_types(Core.apply_type, Tuple{Type{Union},Any})) == Union{Type,TypeVar} @test only(Base.return_types(Core.apply_type, Tuple{Type{Union},Any,Any})) == Type @test only(Base.return_types(Core.apply_type, Tuple{Type{Union},Int})) == Union{} @test only(Base.return_types(Core.apply_type, Tuple{Type{Union},Any,Int})) == Union{} @@ -4715,3 +4715,6 @@ f_no_bail_effects_any(x::Any) = x f_no_bail_effects_any(x::NamedTuple{(:x,), Tuple{Any}}) = getfield(x, 1) g_no_bail_effects_any(x::Any) = f_no_bail_effects_any(x) @test Core.Compiler.is_total(Base.infer_effects(g_no_bail_effects_any, Tuple{Any})) + +# issue #48374 +@test (() -> Union{<:Nothing})() == Nothing