Skip to content

Commit 5283847

Browse files
committed
fix #32488, type intersection regression (#32509)
Issue was caused by 74305bf, so this reverts part of the new logic from there. (cherry picked from commit a9ad6c2)
1 parent aa79ae4 commit 5283847

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/subtype.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,19 +1690,35 @@ static jl_value_t *intersect_var(jl_tvar_t *b, jl_value_t *a, jl_stenv_t *e, int
16901690
int d = bb->depth0;
16911691
jl_value_t *root=NULL; jl_savedenv_t se;
16921692
if (param == 2) {
1693-
if (!(subtype_in_env_existential(bb->lb, a, e, 0, d) && subtype_in_env_existential(a, bb->ub, e, 1, d)))
1694-
return jl_bottom_type;
1695-
jl_value_t *ub = a;
1693+
jl_value_t *ub = NULL;
1694+
JL_GC_PUSH2(&ub, &root);
1695+
if (!jl_has_free_typevars(a)) {
1696+
save_env(e, &root, &se);
1697+
int issub = subtype_in_env_existential(bb->lb, a, e, 0, d) && subtype_in_env_existential(a, bb->ub, e, 1, d);
1698+
restore_env(e, root, &se);
1699+
free(se.buf);
1700+
if (!issub) {
1701+
JL_GC_POP();
1702+
return jl_bottom_type;
1703+
}
1704+
ub = a;
1705+
}
1706+
else {
1707+
ub = R ? intersect_aside(a, bb->ub, e, 1, d) : intersect_aside(bb->ub, a, e, 0, d);
1708+
// TODO: we should probably check `bb->lb <: ub` here; find a test case for that
1709+
}
16961710
if (ub != (jl_value_t*)b) {
16971711
if (jl_has_free_typevars(ub)) {
16981712
// constraint X == Ref{X} is unsatisfiable. also check variables set equal to X.
16991713
if (var_occurs_inside(ub, b, 0, 0)) {
1714+
JL_GC_POP();
17001715
return jl_bottom_type;
17011716
}
17021717
jl_varbinding_t *btemp = e->vars;
17031718
while (btemp != NULL) {
17041719
if (btemp->lb == (jl_value_t*)b && btemp->ub == (jl_value_t*)b &&
17051720
var_occurs_inside(ub, btemp->var, 0, 0)) {
1721+
JL_GC_POP();
17061722
return jl_bottom_type;
17071723
}
17081724
btemp = btemp->prev;
@@ -1711,6 +1727,7 @@ static jl_value_t *intersect_var(jl_tvar_t *b, jl_value_t *a, jl_stenv_t *e, int
17111727
bb->ub = ub;
17121728
bb->lb = ub;
17131729
}
1730+
JL_GC_POP();
17141731
return ub;
17151732
}
17161733
else if (bb->constraintkind == 0) {

test/subtype.jl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,7 +1479,9 @@ CovType{T} = Union{AbstractArray{T,2},
14791479
# issue #31703
14801480
@testintersect(Pair{<:Any, Ref{Tuple{Ref{Ref{Tuple{Int}}},Ref{Float64}}}},
14811481
Pair{T, S} where S<:(Ref{A} where A<:(Tuple{C,Ref{T}} where C<:(Ref{D} where D<:(Ref{E} where E<:Tuple{FF}) where FF<:B)) where B) where T,
1482-
Pair{Float64, Ref{Tuple{Ref{Ref{Tuple{Int}}},Ref{Float64}}}})
1482+
Pair{T, Ref{Tuple{Ref{Ref{Tuple{Int}}},Ref{Float64}}}} where T)
1483+
# TODO: should be able to get this result
1484+
# Pair{Float64, Ref{Tuple{Ref{Ref{Tuple{Int}}},Ref{Float64}}}}
14831485

14841486
module I31703
14851487
using Test, LinearAlgebra
@@ -1545,6 +1547,13 @@ let X = LinearAlgebra.Symmetric{T, S} where S<:(AbstractArray{U, 2} where U<:T)
15451547
end
15461548

15471549
# issue #32386
1548-
# TODO: intersect currently returns a bad answer here (it has free typevars)
1549-
@test typeintersect(Type{S} where S<:(Array{Pair{_A,N} where N, 1} where _A),
1550-
Type{Vector{T}} where T) != Union{}
1550+
@test typeintersect(Type{S} where S<:(Vector{Pair{_A,N} where N} where _A),
1551+
Type{Vector{T}} where T) == Type{Vector{Pair{_A,N} where N}} where _A
1552+
1553+
# issue #32488
1554+
struct S32488{S <: Tuple, T, N, L}
1555+
data::NTuple{L,T}
1556+
end
1557+
@testintersect(Tuple{Type{T} where T<:(S32488{Tuple{_A}, Int64, 1, _A} where _A), Tuple{Vararg{Int64, D}} where D},
1558+
Tuple{Type{S32488{S, T, N, L}}, Tuple{Vararg{T, L}}} where L where N where T where S,
1559+
Tuple{Type{S32488{Tuple{L},Int64,1,L}},Tuple{Vararg{Int64,L}}} where L)

0 commit comments

Comments
 (0)