Skip to content

Commit

Permalink
Fix apply_type_tfunc for Union{T::TypeVar}
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.

(cherry picked from commit fd79b58)
  • Loading branch information
martinholters authored and KristofferC committed Feb 1, 2023
1 parent fcd3ca8 commit 017365c
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 @@ -1541,7 +1541,7 @@ function apply_type_tfunc(@nospecialize(headtypetype), @nospecialize args...)
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 @@ -2364,9 +2364,9 @@ end |> only === Int
# Equivalence of Const(T.instance) and T for singleton types
@test Const(nothing) Nothing && Nothing Const(nothing)

# 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 @@ -4277,3 +4277,6 @@ end
# Issue #46839: `abstract_invoke` should handle incorrect call type
@test only(Base.return_types(()->invoke(BitSet, Any, x), ())) === Union{}
@test only(Base.return_types(()->invoke(BitSet, Union{Tuple{Int32},Tuple{Int64}}, 1), ())) === Union{}

# issue #48374
@test (() -> Union{<:Nothing})() == Nothing

0 comments on commit 017365c

Please sign in to comment.