Skip to content

Commit

Permalink
Fix apply_type_tfunc for Union{T::TypeVar} (#48384)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
martinholters authored Jan 24, 2023
1 parent dfab7be commit 30d11a3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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

0 comments on commit 30d11a3

Please sign in to comment.