diff --git a/src/subtype.c b/src/subtype.c index 39ec6da875c37..08260f6761bd2 100644 --- a/src/subtype.c +++ b/src/subtype.c @@ -2445,11 +2445,22 @@ static jl_value_t *intersect_all(jl_value_t *x, jl_value_t *y, jl_stenv_t *e) e->Runions.more = 0; memset(e->Runions.stack, 0, sizeof(e->Runions.stack)); jl_value_t **is; - JL_GC_PUSHARGS(is, 2); + JL_GC_PUSHARGS(is, 3); + jl_value_t **saved = &is[2]; + jl_savedenv_t se; + save_env(e, saved, &se); int lastset = 0, niter = 0, total_iter = 0; jl_value_t *ii = intersect(x, y, e, 0); + if (ii == jl_bottom_type) { + restore_env(e, *saved, &se); + } + else { + free(se.buf); + save_env(e, saved, &se); + } while (e->Runions.more) { if (e->emptiness_only && ii != jl_bottom_type) { + free(se.buf); JL_GC_POP(); return ii; } @@ -2463,6 +2474,13 @@ static jl_value_t *intersect_all(jl_value_t *x, jl_value_t *y, jl_stenv_t *e) is[0] = ii; is[1] = intersect(x, y, e, 0); + if (is[1] == jl_bottom_type) { + restore_env(e, *saved, &se); + } + else { + free(se.buf); + save_env(e, saved, &se); + } if (is[0] == jl_bottom_type) ii = is[1]; else if (is[1] == jl_bottom_type) @@ -2474,10 +2492,12 @@ static jl_value_t *intersect_all(jl_value_t *x, jl_value_t *y, jl_stenv_t *e) } total_iter++; if (niter > 3 || total_iter > 400000) { + free(se.buf); JL_GC_POP(); return y; } } + free(se.buf); JL_GC_POP(); return ii; } diff --git a/test/subtype.jl b/test/subtype.jl index f5a465d9e658b..20b0c8a0abba7 100644 --- a/test/subtype.jl +++ b/test/subtype.jl @@ -1464,3 +1464,10 @@ end @testintersect(Tuple{Type{Val{T}},Integer,T} where T>:Integer, Tuple{Type,Int,Int}, Tuple{Type{Val{T}},Int,Int} where T>:Integer) + +# issue #31496 +CovType{T} = Union{AbstractArray{T,2}, + Vector{UpperTriangular{T,Matrix{T}}}} +@testintersect(Pair{<:Any, <:AbstractMatrix}, + Pair{T, <:CovType{T}} where T<:AbstractFloat, + Pair{T,S} where S<:AbstractArray{T,2} where T<:AbstractFloat)