Skip to content

Commit bd375ac

Browse files
committed
fix type intersection bug affecting Dolang.jl and SolverTools.jl (#32853)
(cherry picked from commit 5dbcf20)
1 parent 6d9dc08 commit bd375ac

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/subtype.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,10 +499,13 @@ static void record_var_occurrence(jl_varbinding_t *vb, jl_stenv_t *e, int param)
499499
{
500500
if (vb != NULL && param) {
501501
// saturate counters at 2; we don't need values bigger than that
502-
if (param == 2 && (vb->right ? e->Rinvdepth : e->invdepth) > vb->depth0 && vb->occurs_inv < 2)
503-
vb->occurs_inv++;
504-
else if (vb->occurs_cov < 2)
502+
if (param == 2 && (vb->right ? e->Rinvdepth : e->invdepth) > vb->depth0) {
503+
if (vb->occurs_inv < 2)
504+
vb->occurs_inv++;
505+
}
506+
else if (vb->occurs_cov < 2) {
505507
vb->occurs_cov++;
508+
}
506509
}
507510
}
508511

@@ -1233,7 +1236,15 @@ static int subtype(jl_value_t *x, jl_value_t *y, jl_stenv_t *e, int param)
12331236
jl_value_t *tp0 = jl_tparam0(yd);
12341237
if (!jl_is_typevar(tp0) || !jl_is_kind(x))
12351238
return 0;
1236-
return subtype((jl_value_t*)jl_type_type, y, e, param);
1239+
// DataType.super is special, so `DataType <: Type{T}` (T free) needs special handling.
1240+
// The answer is true iff `T` has full bounds (as in `Type`), but this needs to
1241+
// be checked at the same depth where `Type{T}` occurs --- the depth of the LHS
1242+
// doesn't matter because it (e.g. `DataType`) doesn't actually contain the variable.
1243+
int saved = e->invdepth;
1244+
e->invdepth = e->Rinvdepth;
1245+
int issub = subtype((jl_value_t*)jl_type_type, y, e, param);
1246+
e->invdepth = saved;
1247+
return issub;
12371248
}
12381249
while (xd != jl_any_type && xd->name != yd->name) {
12391250
if (xd->super == NULL)

test/subtype.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,3 +1652,7 @@ c32703(::Type{<:Str{C}}, str::Str{C}) where {C<:CSE} = str
16521652
@test_broken typeintersect(Tuple{Vector{Vector{Float32}},Matrix,Matrix},
16531653
Tuple{Vector{V},Matrix{Int},Matrix{S}} where {S, V<:AbstractVector{S}}) ==
16541654
Tuple{Array{Array{Float32,1},1},Array{Int,2},Array{Float32,2}}
1655+
1656+
@testintersect(Tuple{Pair{Int, DataType}, Any},
1657+
Tuple{Pair{A, B} where B<:Type, Int} where A,
1658+
Tuple{Pair{Int, DataType}, Int})

0 commit comments

Comments
 (0)