From b23811e1a030073de8b5a745965f37bb3a55ab6f Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki <40514306+aviatesk@users.noreply.github.com> Date: Tue, 22 Oct 2024 05:23:04 +0900 Subject: [PATCH] inference: fix inference error from constructing invalid `TypeVar` (#56264) - fixes JuliaLang/julia#56248 (cherry picked from commit 08d11d041b22fe90380e56be4fb4d44aaf46ec85) --- base/compiler/tfuncs.jl | 12 ++++++++++-- test/compiler/inference.jl | 8 ++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/base/compiler/tfuncs.jl b/base/compiler/tfuncs.jl index 7c5503d2e6068e..e2773752c68e6b 100644 --- a/base/compiler/tfuncs.jl +++ b/base/compiler/tfuncs.jl @@ -574,8 +574,16 @@ add_tfunc(svec, 0, INT_INF, @nospecs((𝕃::AbstractLattice, args...)->SimpleVec return TypeVar end end - tv = TypeVar(nval, lb, ub) - return PartialTypeVar(tv, lb_certain, ub_certain) + lb_valid = lb isa Type || lb isa TypeVar + ub_valid = ub isa Type || ub isa TypeVar + if lb_valid && ub_valid + tv = TypeVar(nval, lb, ub) + return PartialTypeVar(tv, lb_certain, ub_certain) + elseif !lb_valid && lb_certain + return Union{} + elseif !ub_valid && ub_certain + return Union{} + end end return TypeVar end diff --git a/test/compiler/inference.jl b/test/compiler/inference.jl index 6f4aabddbedf88..270622c2332a26 100644 --- a/test/compiler/inference.jl +++ b/test/compiler/inference.jl @@ -5164,3 +5164,11 @@ end issue55882_nfields(x::Union{T,Nothing}) where T<:Number = nfields(x) @test only(Base.return_types(issue55882_nfields)) <: Int + +# JuliaLang/julia#56248 +@test Base.infer_return_type() do + TypeVar(:Issue56248, 1) +end === Union{} +@test Base.infer_return_type() do + TypeVar(:Issue56248, Any, 1) +end === Union{}