Skip to content

Commit a9ad6c2

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

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
@@ -2129,19 +2129,35 @@ static jl_value_t *intersect_var(jl_tvar_t *b, jl_value_t *a, jl_stenv_t *e, int
21292129
int d = bb->depth0;
21302130
jl_value_t *root=NULL; jl_savedenv_t se;
21312131
if (param == 2) {
2132-
if (!(subtype_in_env_existential(bb->lb, a, e, 0, d) && subtype_in_env_existential(a, bb->ub, e, 1, d)))
2133-
return jl_bottom_type;
2134-
jl_value_t *ub = a;
2132+
jl_value_t *ub = NULL;
2133+
JL_GC_PUSH2(&ub, &root);
2134+
if (!jl_has_free_typevars(a)) {
2135+
save_env(e, &root, &se);
2136+
int issub = subtype_in_env_existential(bb->lb, a, e, 0, d) && subtype_in_env_existential(a, bb->ub, e, 1, d);
2137+
restore_env(e, root, &se);
2138+
free(se.buf);
2139+
if (!issub) {
2140+
JL_GC_POP();
2141+
return jl_bottom_type;
2142+
}
2143+
ub = a;
2144+
}
2145+
else {
2146+
ub = R ? intersect_aside(a, bb->ub, e, 1, d) : intersect_aside(bb->ub, a, e, 0, d);
2147+
// TODO: we should probably check `bb->lb <: ub` here; find a test case for that
2148+
}
21352149
if (ub != (jl_value_t*)b) {
21362150
if (jl_has_free_typevars(ub)) {
21372151
// constraint X == Ref{X} is unsatisfiable. also check variables set equal to X.
21382152
if (var_occurs_inside(ub, b, 0, 0)) {
2153+
JL_GC_POP();
21392154
return jl_bottom_type;
21402155
}
21412156
jl_varbinding_t *btemp = e->vars;
21422157
while (btemp != NULL) {
21432158
if (btemp->lb == (jl_value_t*)b && btemp->ub == (jl_value_t*)b &&
21442159
var_occurs_inside(ub, btemp->var, 0, 0)) {
2160+
JL_GC_POP();
21452161
return jl_bottom_type;
21462162
}
21472163
btemp = btemp->prev;
@@ -2150,6 +2166,7 @@ static jl_value_t *intersect_var(jl_tvar_t *b, jl_value_t *a, jl_stenv_t *e, int
21502166
bb->ub = ub;
21512167
bb->lb = ub;
21522168
}
2169+
JL_GC_POP();
21532170
return ub;
21542171
}
21552172
else if (bb->constraintkind == 0) {

test/subtype.jl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,9 @@ CovType{T} = Union{AbstractArray{T,2},
14811481
# issue #31703
14821482
@testintersect(Pair{<:Any, Ref{Tuple{Ref{Ref{Tuple{Int}}},Ref{Float64}}}},
14831483
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,
1484-
Pair{Float64, Ref{Tuple{Ref{Ref{Tuple{Int}}},Ref{Float64}}}})
1484+
Pair{T, Ref{Tuple{Ref{Ref{Tuple{Int}}},Ref{Float64}}}} where T)
1485+
# TODO: should be able to get this result
1486+
# Pair{Float64, Ref{Tuple{Ref{Ref{Tuple{Int}}},Ref{Float64}}}}
14851487

14861488
module I31703
14871489
using Test, LinearAlgebra
@@ -1608,6 +1610,13 @@ end
16081610
#end
16091611

16101612
# issue #32386
1611-
# TODO: intersect currently returns a bad answer here (it has free typevars)
1612-
@test typeintersect(Type{S} where S<:(Array{Pair{_A,N} where N, 1} where _A),
1613-
Type{Vector{T}} where T) != Union{}
1613+
@test typeintersect(Type{S} where S<:(Vector{Pair{_A,N} where N} where _A),
1614+
Type{Vector{T}} where T) == Type{Vector{Pair{_A,N} where N}} where _A
1615+
1616+
# issue #32488
1617+
struct S32488{S <: Tuple, T, N, L}
1618+
data::NTuple{L,T}
1619+
end
1620+
@testintersect(Tuple{Type{T} where T<:(S32488{Tuple{_A}, Int64, 1, _A} where _A), Tuple{Vararg{Int64, D}} where D},
1621+
Tuple{Type{S32488{S, T, N, L}}, Tuple{Vararg{T, L}}} where L where N where T where S,
1622+
Tuple{Type{S32488{Tuple{L},Int64,1,L}},Tuple{Vararg{Int64,L}}} where L)

0 commit comments

Comments
 (0)