Skip to content

Commit 5dbcf20

Browse files
authored
fix type intersection bug affecting Dolang.jl and SolverTools.jl (#32853)
1 parent 5331e12 commit 5dbcf20

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
@@ -536,10 +536,13 @@ static void record_var_occurrence(jl_varbinding_t *vb, jl_stenv_t *e, int param)
536536
{
537537
if (vb != NULL && param) {
538538
// saturate counters at 2; we don't need values bigger than that
539-
if (param == 2 && (vb->right ? e->Rinvdepth : e->invdepth) > vb->depth0 && vb->occurs_inv < 2)
540-
vb->occurs_inv++;
541-
else if (vb->occurs_cov < 2)
539+
if (param == 2 && (vb->right ? e->Rinvdepth : e->invdepth) > vb->depth0) {
540+
if (vb->occurs_inv < 2)
541+
vb->occurs_inv++;
542+
}
543+
else if (vb->occurs_cov < 2) {
542544
vb->occurs_cov++;
545+
}
543546
}
544547
}
545548

@@ -1275,7 +1278,15 @@ static int subtype(jl_value_t *x, jl_value_t *y, jl_stenv_t *e, int param)
12751278
jl_value_t *tp0 = jl_tparam0(yd);
12761279
if (!jl_is_typevar(tp0) || !jl_is_kind(x))
12771280
return 0;
1278-
return subtype((jl_value_t*)jl_type_type, y, e, param);
1281+
// DataType.super is special, so `DataType <: Type{T}` (T free) needs special handling.
1282+
// The answer is true iff `T` has full bounds (as in `Type`), but this needs to
1283+
// be checked at the same depth where `Type{T}` occurs --- the depth of the LHS
1284+
// doesn't matter because it (e.g. `DataType`) doesn't actually contain the variable.
1285+
int saved = e->invdepth;
1286+
e->invdepth = e->Rinvdepth;
1287+
int issub = subtype((jl_value_t*)jl_type_type, y, e, param);
1288+
e->invdepth = saved;
1289+
return issub;
12791290
}
12801291
while (xd != jl_any_type && xd->name != yd->name) {
12811292
if (xd->super == NULL)

test/subtype.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,3 +1672,7 @@ c32703(::Type{<:Str{C}}, str::Str{C}) where {C<:CSE} = str
16721672
@test_broken typeintersect(Tuple{Vector{Vector{Float32}},Matrix,Matrix},
16731673
Tuple{Vector{V},Matrix{Int},Matrix{S}} where {S, V<:AbstractVector{S}}) ==
16741674
Tuple{Array{Array{Float32,1},1},Array{Int,2},Array{Float32,2}}
1675+
1676+
@testintersect(Tuple{Pair{Int, DataType}, Any},
1677+
Tuple{Pair{A, B} where B<:Type, Int} where A,
1678+
Tuple{Pair{Int, DataType}, Int})

0 commit comments

Comments
 (0)