Skip to content

Commit 236de57

Browse files
committed
typeintersect: more organized innervar wrapping
1 parent 9dc5efb commit 236de57

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/subtype.c

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3018,7 +3018,8 @@ static jl_value_t *finish_unionall(jl_value_t *res JL_MAYBE_UNROOTED, jl_varbind
30183018
}
30193019

30203020
if (vb->innervars != NULL) {
3021-
for (size_t i = 0; i < jl_array_nrows(vb->innervars); i++) {
3021+
size_t len = jl_array_nrows(vb->innervars), count = 0;
3022+
for (size_t i = 0; i < len; i++) {
30223023
jl_tvar_t *var = (jl_tvar_t*)jl_array_ptr_ref(vb->innervars, i);
30233024
// the `btemp->prev` walk is only giving a sort of post-order guarantee (since we are
30243025
// iterating 2 trees at once), so once we set `wrap`, there might remain other branches
@@ -3033,10 +3034,38 @@ static jl_value_t *finish_unionall(jl_value_t *res JL_MAYBE_UNROOTED, jl_varbind
30333034
if (wrap->innervars == NULL)
30343035
wrap->innervars = jl_alloc_array_1d(jl_array_any_type, 0);
30353036
jl_array_ptr_1d_push(wrap->innervars, (jl_value_t*)var);
3037+
jl_array_ptr_set(vb->innervars, i, (jl_value_t*)NULL);
30363038
}
3037-
else if (res != jl_bottom_type) {
3038-
if (jl_has_typevar(res, var))
3039-
res = jl_type_unionall((jl_tvar_t*)var, res);
3039+
}
3040+
for (size_t i = 0; i < len; i++) {
3041+
jl_tvar_t *var = (jl_tvar_t*)jl_array_ptr_ref(vb->innervars, i);
3042+
if (var) count++;
3043+
if (count < i + 1)
3044+
jl_array_ptr_set(vb->innervars, count-1, (jl_value_t*)var);
3045+
}
3046+
if (count != len)
3047+
jl_array_del_end(vb->innervars, len - count);
3048+
if (res != jl_bottom_type) {
3049+
while (count > 1) {
3050+
int changed = 0;
3051+
for (size_t i = 0; i < count - 1; i++) {
3052+
jl_tvar_t *vari = (jl_tvar_t*)jl_array_ptr_ref(vb->innervars, i);
3053+
for (size_t j = i+1; j < count; j++) {
3054+
jl_tvar_t *varj = (jl_tvar_t*)jl_array_ptr_ref(vb->innervars, j);
3055+
if (jl_has_typevar(varj->lb, vari) || jl_has_typevar(varj->ub, vari)) {
3056+
jl_array_ptr_set(vb->innervars, j, (jl_value_t*)vari);
3057+
jl_array_ptr_set(vb->innervars, i, (jl_value_t*)varj);
3058+
changed = 1;
3059+
break;
3060+
}
3061+
}
3062+
if (changed) break;
3063+
}
3064+
if (!changed) break;
3065+
}
3066+
for (size_t i = 0; i < count; i++) {
3067+
jl_tvar_t *var = (jl_tvar_t*)jl_array_ptr_ref(vb->innervars, i);
3068+
res = jl_type_unionall(var, res);
30403069
}
30413070
}
30423071
}

test/subtype.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,8 +1216,7 @@ let a = Tuple{T1,T1} where T1,
12161216
end
12171217
let a = Val{Tuple{T1,T1}} where T1,
12181218
b = Val{Tuple{Val{S2},S6}} where S2 where S6
1219-
# @testintersect(a, b, Val{Tuple{Val{T},Val{T}}} where T)
1220-
@test_broken !Base.has_free_typevars(typeintersect(b, a))
1219+
@testintersect(a, b, Val{Tuple{Val{T},Val{T}}} where T)
12211220
end
12221221
let a = Tuple{Float64,T3,T4} where T4 where T3,
12231222
b = Tuple{S2,Tuple{S3},S3} where S2 where S3

0 commit comments

Comments
 (0)