Skip to content

Commit 25bad18

Browse files
JeffBezansonKristofferC
authored andcommitted
fix #47658, state stack overflow on unions containing typevars (#48221)
(cherry picked from commit 596ce65)
1 parent b3d42d1 commit 25bad18

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/subtype.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,19 @@ static int subtype_tuple(jl_datatype_t *xd, jl_datatype_t *yd, jl_stenv_t *e, in
11741174
return ans;
11751175
}
11761176

1177+
static int equal_unions(jl_uniontype_t *x, jl_uniontype_t *y, jl_stenv_t *e)
1178+
{
1179+
jl_value_t *saved=NULL; jl_savedenv_t se;
1180+
JL_GC_PUSH1(&saved);
1181+
save_env(e, &saved, &se);
1182+
int eq = forall_exists_equal(x->a, y->a, e) && forall_exists_equal(x->b, y->b, e);
1183+
if (!eq)
1184+
restore_env(e, saved, &se);
1185+
free_env(&se);
1186+
JL_GC_POP();
1187+
return eq;
1188+
}
1189+
11771190
// `param` means we are currently looking at a parameter of a type constructor
11781191
// (as opposed to being outside any type constructor, or comparing variable bounds).
11791192
// this is used to record the positions where type variables occur for the
@@ -1363,6 +1376,27 @@ static int forall_exists_equal(jl_value_t *x, jl_value_t *y, jl_stenv_t *e)
13631376
(is_definite_length_tuple_type(x) && is_indefinite_length_tuple_type(y)))
13641377
return 0;
13651378

1379+
if (jl_is_uniontype(x) && jl_is_uniontype(y)) {
1380+
// For 2 unions, try a more efficient greedy algorithm that compares the unions
1381+
// componentwise. If it returns `false`, we forget it and proceed with the usual
1382+
// algorithm. If it returns `true` we try returning `true`, but need to come back
1383+
// here to try the usual algorithm if subtyping later fails.
1384+
jl_unionstate_t *state = &e->Runions;
1385+
jl_saved_unionstate_t oldRunions; push_unionstate(&oldRunions, state);
1386+
if (state->depth >= state->used) {
1387+
statestack_set(state, state->used, 0);
1388+
state->used++;
1389+
}
1390+
int ui = statestack_get(state, state->depth);
1391+
state->depth++;
1392+
if (ui == 0) {
1393+
state->more = state->depth; // memorize that this was the deepest available choice
1394+
if (equal_unions((jl_uniontype_t*)x, (jl_uniontype_t*)y, e))
1395+
return 1;
1396+
pop_unionstate(state, &oldRunions);
1397+
}
1398+
}
1399+
13661400
jl_saved_unionstate_t oldLunions; push_unionstate(&oldLunions, &e->Lunions);
13671401
e->Lunions.used = 0;
13681402
int sub;

test/subtype.jl

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2241,6 +2241,16 @@ let S = Tuple{T2, V2} where {T2, N2, V2<:(Array{S2, N2} where {S2 <: T2})},
22412241
@testintersect(S, T, !Union{})
22422242
end
22432243

2244+
@test only(intersection_env(Val{Union{Val{Val{T}} where {T},Int}}, Val{Union{T,Int}} where T)[2]) === Val{Val{T}} where {T}
2245+
2246+
# issue 47654
2247+
Vec47654{T} = Union{AbstractVector{T}, AbstractVector{Union{T,Nothing}}}
2248+
struct Wrapper47654{T, V<:Vec47654{T}}
2249+
v::V
2250+
end
2251+
abstract type P47654{A} end
2252+
@test Wrapper47654{P47654, Vector{Union{P47654,Nothing}}} <: Wrapper47654
2253+
22442254
@testset "known subtype/intersect issue" begin
22452255
#issue 45874
22462256
# Causes a hang due to jl_critical_error calling back into malloc...
@@ -2278,7 +2288,7 @@ end
22782288
@test_broken typeintersect(Tuple{Type{Tuple{T,Val{T}}}, Val{T}} where T, Tuple{Type{Tuple{Val{T},T}}, Val{T}} where T) <: Any
22792289

22802290
# issue 24333
2281-
@test_broken (Type{Union{Ref,Cvoid}} <: Type{Union{T,Cvoid}} where T)
2291+
@test (Type{Union{Ref,Cvoid}} <: Type{Union{T,Cvoid}} where T)
22822292

22832293
# issue 22123
22842294
t1 = Ref{Ref{Ref{Union{Int64, T}}} where T}
@@ -2289,4 +2299,16 @@ end
22892299
@test_broken (Tuple{T1,T1} where T1<:(Val{T2} where T2)) <: (Tuple{Val{S},Val{S}} where S)
22902300
end
22912301

2302+
# issue #47658
2303+
let T = Ref{NTuple{8, Ref{Union{Int, P}}}} where P,
2304+
S = Ref{NTuple{8, Ref{Union{Int, P}}}} where P
2305+
# note T and S are identical but we need 2 copies to avoid being fooled by pointer equality
2306+
@test T <: Union{Int, S}
2307+
end
2308+
2309+
# try to fool a greedy algorithm that picks X=Int, Y=String here
2310+
@test Tuple{Ref{Union{Int,String}}, Ref{Union{Int,String}}} <: Tuple{Ref{Union{X,Y}}, Ref{X}} where {X,Y}
2311+
# this slightly more complex case has been broken since 1.0 (worked in 0.6)
2312+
@test_broken Tuple{Ref{Union{Int,String,Missing}}, Ref{Union{Int,String}}} <: Tuple{Ref{Union{X,Y}}, Ref{X}} where {X,Y}
2313+
22922314
@test !(Tuple{Any, Any, Any} <: Tuple{Any, Vararg{T}} where T)

0 commit comments

Comments
 (0)