Skip to content

Commit ea42cbc

Browse files
committed
Revert "avoid excessive renaming in subtype of intersection result (#39623)"
This reverts commit 093b2a6.
1 parent 7040ffb commit ea42cbc

3 files changed

Lines changed: 21 additions & 39 deletions

File tree

src/gf.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,22 +1594,26 @@ JL_DLLEXPORT void jl_method_table_disable(jl_methtable_t *mt, jl_method_t *metho
15941594
static int jl_type_intersection2(jl_value_t *t1, jl_value_t *t2, jl_value_t **isect, jl_value_t **isect2)
15951595
{
15961596
*isect2 = NULL;
1597-
int is_subty = 0;
1598-
*isect = jl_type_intersection_env_s(t1, t2, NULL, &is_subty);
1597+
*isect = jl_type_intersection(t1, t2);
15991598
if (*isect == jl_bottom_type)
16001599
return 0;
1601-
if (is_subty)
1602-
return 1;
16031600
// determine if type-intersection can be convinced to give a better, non-bad answer
1604-
// if the intersection was imprecise, see if we can do better by switching the types
1605-
*isect2 = jl_type_intersection(t2, t1);
1606-
if (*isect2 == jl_bottom_type) {
1607-
*isect = jl_bottom_type;
1608-
*isect2 = NULL;
1609-
return 0;
1610-
}
1611-
if (jl_types_egal(*isect2, *isect)) {
1612-
*isect2 = NULL;
1601+
if (!(jl_subtype(*isect, t1) && jl_subtype(*isect, t2))) {
1602+
// if the intersection was imprecise, see if we can do
1603+
// better by switching the types
1604+
*isect2 = jl_type_intersection(t2, t1);
1605+
if (*isect2 == jl_bottom_type) {
1606+
*isect = jl_bottom_type;
1607+
*isect2 = NULL;
1608+
return 0;
1609+
}
1610+
if (jl_subtype(*isect2, t1) && jl_subtype(*isect2, t2)) {
1611+
*isect = *isect2;
1612+
*isect2 = NULL;
1613+
}
1614+
else if (jl_types_equal(*isect2, *isect)) {
1615+
*isect2 = NULL;
1616+
}
16131617
}
16141618
return 1;
16151619
}

src/subtype.c

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2404,9 +2404,7 @@ static jl_value_t *finish_unionall(jl_value_t *res JL_MAYBE_UNROOTED, jl_varbind
24042404
}
24052405
}
24062406

2407-
// prefer generating a fresh typevar, to avoid repeated renaming if the result
2408-
// is compared to one of the intersected types later.
2409-
if (!varval)
2407+
if (!varval && (vb->lb != vb->var->lb || vb->ub != vb->var->ub))
24102408
newvar = jl_new_typevar(vb->var->name, vb->lb, vb->ub);
24112409

24122410
// remove/replace/rewrap free occurrences of this var in the environment
@@ -3266,25 +3264,11 @@ jl_svec_t *jl_outer_unionall_vars(jl_value_t *u)
32663264
static jl_value_t *switch_union_tuple(jl_value_t *a, jl_value_t *b)
32673265
{
32683266
if (jl_is_unionall(a)) {
3269-
jl_unionall_t *ua = (jl_unionall_t*)a;
3270-
if (jl_is_unionall(b)) {
3271-
jl_unionall_t *ub = (jl_unionall_t*)b;
3272-
if (ub->var->lb == ua->var->lb && ub->var->ub == ua->var->ub) {
3273-
jl_value_t *ub2 = jl_instantiate_unionall(ub, (jl_value_t*)ua->var);
3274-
jl_value_t *ans = NULL;
3275-
JL_GC_PUSH2(&ub2, &ans);
3276-
ans = switch_union_tuple(ua->body, ub2);
3277-
if (ans != NULL)
3278-
ans = jl_type_unionall(ua->var, ans);
3279-
JL_GC_POP();
3280-
return ans;
3281-
}
3282-
}
3283-
jl_value_t *ans = switch_union_tuple(ua->body, b);
3267+
jl_value_t *ans = switch_union_tuple(((jl_unionall_t*)a)->body, b);
32843268
if (ans == NULL)
32853269
return NULL;
32863270
JL_GC_PUSH1(&ans);
3287-
ans = jl_type_unionall(ua->var, ans);
3271+
ans = jl_type_unionall(((jl_unionall_t*)a)->var, ans);
32883272
JL_GC_POP();
32893273
return ans;
32903274
}

test/subtype.jl

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,18 +1057,12 @@ function test_intersection()
10571057
end
10581058

10591059
function test_intersection_properties()
1060-
approx = Tuple{Vector{Vector{T}} where T, Vector{Vector{T}} where T}
10611060
for T in menagerie
10621061
for S in menagerie
10631062
I = _type_intersect(T,S)
10641063
I2 = _type_intersect(S,T)
10651064
@test isequal_type(I, I2)
1066-
if I == approx
1067-
# TODO: some of these cases give a conservative answer
1068-
@test issub(I, T) || issub(I, S)
1069-
else
1070-
@test issub(I, T) && issub(I, S)
1071-
end
1065+
@test issub(I, T) && issub(I, S)
10721066
if issub(T, S)
10731067
@test isequal_type(I, T)
10741068
end

0 commit comments

Comments
 (0)