From d76cdcff24fca12a552f3ed092b3ac16d3ede952 Mon Sep 17 00:00:00 2001 From: N5N3 <2642243996@qq.com> Date: Tue, 16 Jan 2024 21:04:54 +0800 Subject: [PATCH] Fix `widen_diagonal` bug for nested `UnionAll` (#52924) --- src/subtype.c | 9 ++++----- test/core.jl | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/subtype.c b/src/subtype.c index 74c82e72b8002..86b441f9aaec0 100644 --- a/src/subtype.c +++ b/src/subtype.c @@ -4415,19 +4415,18 @@ static jl_value_t *insert_nondiagonal(jl_value_t *type, jl_varbinding_t *troot, jl_value_t *newbody = insert_nondiagonal(body, troot, widen2ub); if (v) v->var = var; // And restore it after inner insertation. jl_value_t *newvar = NULL; - JL_GC_PUSH2(&newbody, &newvar); + JL_GC_PUSH3(&newbody, &newvar, &type); if (body == newbody || jl_has_typevar(newbody, var)) { if (body != newbody) - newbody = jl_new_struct(jl_unionall_type, var, newbody); + type = jl_new_struct(jl_unionall_type, var, newbody); // n.b. we do not widen lb, since that would be the wrong direction newvar = insert_nondiagonal(var->ub, troot, widen2ub); if (newvar != var->ub) { newvar = (jl_value_t*)jl_new_typevar(var->name, var->lb, newvar); - newbody = jl_apply_type1(newbody, newvar); - newbody = jl_type_unionall((jl_tvar_t*)newvar, newbody); + newbody = jl_apply_type1(type, newvar); + type = jl_type_unionall((jl_tvar_t*)newvar, newbody); } } - type = newbody; JL_GC_POP(); } else if (jl_is_uniontype(type)) { diff --git a/test/core.jl b/test/core.jl index 2f5c6f734a9a1..eefeeb8d1240d 100644 --- a/test/core.jl +++ b/test/core.jl @@ -8074,12 +8074,12 @@ end @test Core.Compiler.is_foldable(Base.infer_effects(getindex, (Core.SimpleVector,Int))) # Test correctness of widen_diagonal -let widen_diagonal(x::UnionAll) = Base.rewrap_unionall(Base.widen_diagonal(Base.unwrap_unionall(x), x), x), - check_widen_diagonal(x, y) = !<:(x, y) && x <: widen_diagonal(y) +let widen_diagonal(x::UnionAll) = Base.rewrap_unionall(Base.widen_diagonal(Base.unwrap_unionall(x), x), x) @test Tuple{Int,Float64} <: widen_diagonal(NTuple) @test Tuple{Int,Float64} <: widen_diagonal(Tuple{T,T} where {T}) @test Tuple{Real,Int,Float64} <: widen_diagonal(Tuple{S,Vararg{T}} where {S, T<:S}) @test Tuple{Int,Int,Float64,Float64} <: widen_diagonal(Tuple{S,S,Vararg{T}} where {S, T<:S}) @test Union{Tuple{T}, Tuple{T,Int}} where {T} === widen_diagonal(Union{Tuple{T}, Tuple{T,Int}} where {T}) @test Tuple === widen_diagonal(Union{Tuple{Vararg{S}}, Tuple{Vararg{T}}} where {S, T}) + @test Tuple{Vararg{Val{<:Set}}} == widen_diagonal(Tuple{Vararg{T}} where T<:Val{<:Set}) end