Skip to content

Commit 508992c

Browse files
committed
replace NULL with a innervar
1 parent b745293 commit 508992c

File tree

2 files changed

+23
-49
lines changed

2 files changed

+23
-49
lines changed

src/subtype.c

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,8 +2389,16 @@ static jl_value_t *bound_var_below(jl_tvar_t *tv, jl_varbinding_t *bb, jl_stenv_
23892389
return bb->lb;
23902390
return jl_box_long(blb - offset);
23912391
}
2392-
if (offset > 0)
2393-
return NULL;
2392+
if (offset > 0) {
2393+
if (bb->innervars == NULL)
2394+
bb->innervars = jl_alloc_array_1d(jl_array_any_type, 0);
2395+
jl_value_t *ntv = NULL;
2396+
JL_GC_PUSH1(&ntv);
2397+
ntv = (jl_value_t *)jl_new_typevar(tv->name, jl_bottom_type, (jl_value_t *)jl_any_type);
2398+
jl_array_ptr_1d_push(bb->innervars, ntv);
2399+
JL_GC_POP();
2400+
return ntv;
2401+
}
23942402
return (jl_value_t*)tv;
23952403
}
23962404

@@ -2520,26 +2528,6 @@ static int check_unsat_bound(jl_value_t *t, jl_tvar_t *v, jl_stenv_t *e) JL_NOTS
25202528
return 0;
25212529
}
25222530

2523-
//TODO: This doesn't work for nested `Tuple`.
2524-
static int has_free_vararg_length(jl_value_t *a, jl_stenv_t *e) {
2525-
if (jl_is_unionall(a))
2526-
a = jl_unwrap_unionall(a);
2527-
if (jl_is_datatype(a) && jl_is_tuple_type((jl_datatype_t *)a)) {
2528-
size_t lx = jl_nparams((jl_datatype_t *)a);
2529-
if (lx > 0) {
2530-
jl_value_t *la = jl_tparam((jl_datatype_t *)a, lx-1);
2531-
if (jl_is_vararg(la)) {
2532-
jl_value_t *len = jl_unwrap_vararg_num((jl_vararg_t *)la);
2533-
// return 1 if we meet a vararg with Null length
2534-
if (!len) return 1;
2535-
// or a typevar not in the current env.
2536-
if (jl_is_typevar(len))
2537-
return lookup(e, (jl_tvar_t *)len) == NULL;
2538-
}
2539-
}
2540-
}
2541-
return 0;
2542-
}
25432531

25442532
static jl_value_t *intersect_var(jl_tvar_t *b, jl_value_t *a, jl_stenv_t *e, int8_t R, int param)
25452533
{
@@ -2583,11 +2571,6 @@ static jl_value_t *intersect_var(jl_tvar_t *b, jl_value_t *a, jl_stenv_t *e, int
25832571
JL_GC_POP();
25842572
return jl_bottom_type;
25852573
}
2586-
if (jl_is_uniontype(ub) && !jl_is_uniontype(a)) {
2587-
bb->ub = ub;
2588-
bb->lb = jl_bottom_type;
2589-
ub = (jl_value_t*)b;
2590-
}
25912574
}
25922575
if (ub != (jl_value_t*)b) {
25932576
if (jl_has_free_typevars(ub)) {
@@ -2597,12 +2580,11 @@ static jl_value_t *intersect_var(jl_tvar_t *b, jl_value_t *a, jl_stenv_t *e, int
25972580
}
25982581
}
25992582
bb->ub = ub;
2600-
// We get a imprecise Tuple here. Don't change `lb` and return the typevar directly.
2601-
if (has_free_vararg_length(ub, e) && !has_free_vararg_length(a, e)) {
2602-
JL_GC_POP();
2603-
return (jl_value_t*)b;
2604-
}
2605-
bb->lb = ub;
2583+
if ((jl_is_uniontype(ub) && !jl_is_uniontype(a)) ||
2584+
(jl_is_unionall(ub) && !jl_is_unionall(a)))
2585+
ub = (jl_value_t*)b;
2586+
else
2587+
bb->lb = ub;
26062588
}
26072589
JL_GC_POP();
26082590
return ub;

test/subtype.jl

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,11 +1043,7 @@ function test_intersection()
10431043
Type{Tuple{Int,T}} where T<:Integer)
10441044
@testintersect(Type{<:Tuple{Any,Vararg{Any}}},
10451045
Type{Tuple{Vararg{Int,N}}} where N,
1046-
!Union{})
1047-
1048-
@test typeintersect(Type{<:Tuple{Any,Vararg{Any}}}, Type{Tuple{Vararg{Int,N}}} where N) != Type{Tuple{Int,Vararg{Int}}}
1049-
@test_broken typeintersect(Type{<:Tuple{Any,Vararg{Any}}}, Type{Tuple{Vararg{Int,N}}} where N) == Type{Tuple{Int,Vararg{Int,N}}} where N
1050-
@test_broken typeintersect(Type{<:Tuple{Any,Vararg{Any}}}, Type{Tuple{Vararg{Int,N}}} where N) != Type{<:Tuple{Int,Vararg{Int}}}
1046+
Type{Tuple{Int,Vararg{Int,N}}} where N)
10511047

10521048
@testintersect(Type{<:Array},
10531049
Type{AbstractArray{T}} where T,
@@ -2206,23 +2202,19 @@ let A = Pair{NTuple{N, Int}, NTuple{N, Int}} where N,
22062202
Bs = (Pair{<:Tuple{Int, Vararg{Int}}, <:Tuple{Int, Int, Vararg{Int}}},
22072203
Pair{Tuple{Int, Vararg{Int,N1}}, Tuple{Int, Int, Vararg{Int,N2}}} where {N1,N2},
22082204
Pair{<:Tuple{Int, Vararg{Int,N}} where {N}, <:Tuple{Int, Int, Vararg{Int,N}} where {N}})
2209-
Cerr = Pair{Tuple{Int, Vararg{Int, N}}, Tuple{Int, Int, Vararg{Int, N}}} where {N}
2210-
for B in Bs
2211-
C = typeintersect(A, B)
2212-
@test C == typeintersect(B, A) != Union{}
2213-
@test C != Cerr
2214-
@test_broken C != B
2205+
Cs = (Bs[2], Bs[2], Bs[3])
2206+
for (B, C) in zip(Bs, Cs)
2207+
# TODO: The ideal result is Pair{Tuple{Int, Int, Vararg{Int, N}}, Tuple{Int, Int, Vararg{Int, N}}} where {N}
2208+
@testintersect(A, B, C)
22152209
end
22162210
end
22172211

22182212
# Example from pr#39098
22192213
@testintersect(NTuple, Tuple{Any,Vararg}, Tuple{T, Vararg{T}} where {T})
22202214

2221-
let S = Val{T} where T<:Tuple{Tuple{Any, Vararg{Any}}}
2222-
T = Val{Tuple{Tuple{Vararg{Any, N}}}} where {N}
2223-
@testintersect(S, T, !Union{})
2224-
@test_broken typeintersect(S, T) != Val{Tuple{Tuple{Any, Vararg{Any}}}}
2225-
end
2215+
@testintersect(Val{T} where T<:Tuple{Tuple{Any, Vararg{Any}}},
2216+
Val{Tuple{Tuple{Vararg{Any, N}}}} where {N},
2217+
Val{Tuple{Tuple{Any, Vararg{Any, N}}}} where {N})
22262218

22272219
let A = Pair{NTuple{N, Int}, Val{N}} where N,
22282220
Bs = (Pair{<:Tuple{Int, Vararg{Int}}, <:Val},

0 commit comments

Comments
 (0)