Skip to content

Commit

Permalink
Test disjoint via jl_has_empty_intersection
Browse files Browse the repository at this point in the history
This should be fast, as it has an `obviously_disjoint`-based fast path.
  • Loading branch information
N5N3 committed Apr 18, 2023
1 parent d6d2f29 commit 8156ee8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/jltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,17 @@ jl_value_t *simple_union(jl_value_t *a, jl_value_t *b)
}

int obviously_disjoint(jl_value_t *a, jl_value_t *b, int specificity);

static int simple_disjoint(jl_value_t *a, jl_value_t *b, int hasfree) {
if (jl_is_uniontype(b))
return simple_disjoint(a, ((jl_uniontype_t *)b)->a, hasfree) &&
simple_disjoint(a, ((jl_uniontype_t *)b)->b, hasfree);
else if (!hasfree && !jl_has_free_typevars(b))
return jl_has_empty_intersection(a, b);
else
return obviously_disjoint(a, b, 0);
}

jl_value_t *simple_intersect(jl_value_t *a, jl_value_t *b, int overesi)
{
// Unlike `Union`, we don't unwrap `UnionAll` here to avoid possible widening.
Expand All @@ -733,7 +744,7 @@ jl_value_t *simple_intersect(jl_value_t *a, jl_value_t *b, int overesi)
size_t i, j;
// first remove disjoint elements.
for (i = 0; i < nt; i++) {
if (obviously_disjoint(temp[i], (i < nta ? b : a), 0))
if (simple_disjoint(temp[i], (i < nta ? b : a), jl_has_free_typevars(temp[i])))
temp[i] = NULL;
}
// then check subtyping.
Expand Down
4 changes: 2 additions & 2 deletions test/subtype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2496,8 +2496,8 @@ end
@test !<:(Tuple{Type{Union{Int, Val{1}}}, Int}, Tuple{Type{Union{Int, T1}}, T1} where T1<:Union{Val,Pair})
@test <:(Tuple{Type{Union{Int, Val{1}}}, Int}, Tuple{Type{Union{Int, T1}}, T1} where T1<:Union{Integer,Val})
@test <:(Tuple{Type{Union{Int, Int8}}, Int}, Tuple{Type{Union{Int, T1}}, T1} where T1<:Integer)
@test_broken !<:(Tuple{Type{Union{Pair{Int, Any}, Pair{Int, Int}}}, Pair{Int, Any}},
Tuple{Type{Union{Pair{Int, Any}, T1}}, T1} where T1<:(Pair{T,T} where {T}))
@test !<:(Tuple{Type{Union{Pair{Int, Any}, Pair{Int, Int}}}, Pair{Int, Any}},
Tuple{Type{Union{Pair{Int, Any}, T1}}, T1} where T1<:(Pair{T,T} where {T}))

let A = Tuple{Type{T}, T, Val{T}} where T,
B = Tuple{Type{S}, Val{S}, Val{S}} where S
Expand Down

0 comments on commit 8156ee8

Please sign in to comment.