Skip to content

Commit 1acec74

Browse files
Make apply_type_nothrow robust against TypeVars in upper bounds (#49863)
For types like `Foo{S, T<:S}`, `apply_type_nothrow` could in some situations check whether the argument is a subtype of the upper bound of `T`, i.e. `S`, but subtyping agaist a plain `TypeVar` would fail. Instead return `false` in this case. Fixes #49785.
1 parent c99d839 commit 1acec74

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

base/compiler/tfuncs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1665,7 +1665,7 @@ function apply_type_nothrow(𝕃::AbstractLattice, argtypes::Vector{Any}, @nospe
16651665
end
16661666
else
16671667
istype || return false
1668-
if !(T <: u.var.ub)
1668+
if isa(u.var.ub, TypeVar) || !(T <: u.var.ub)
16691669
return false
16701670
end
16711671
if exact ? !(u.var.lb <: T) : !(u.var.lb === Bottom)

test/compiler/inference.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4918,3 +4918,17 @@ let src = code_typed1((Bool,Base.RefValue{String}, Base.RefValue{Any},Int,)) do
49184918
end
49194919
@test count(@nospecialize(x)->isa(x, Core.PhiNode), src.code) == 0
49204920
end
4921+
4922+
struct Issue49785{S, T<:S} end
4923+
let 𝕃 = Core.Compiler.OptimizerLattice()
4924+
argtypes = Any[Core.Compiler.Const(Issue49785),
4925+
Union{Type{String},Type{Int}},
4926+
Union{Type{String},Type{Int}}]
4927+
rt = Type{Issue49785{<:Any, Int}}
4928+
# the following should not throw
4929+
@test !Core.Compiler.apply_type_nothrow(𝕃, argtypes, rt)
4930+
@test code_typed() do
4931+
S = Union{Type{String},Type{Int}}[Int][1]
4932+
map(T -> Issue49785{S,T}, (a = S,))
4933+
end isa Vector
4934+
end

0 commit comments

Comments
 (0)