Skip to content

Commit 45efa5d

Browse files
committed
fix a regression in parametric dispatch with unions
This is a bit of an undisciplined hack, but it should also be quite harmless. No reason not to get Union{T,Void} working ASAP.
1 parent c061f92 commit 45efa5d

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/jltypes.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,16 @@ static jl_value_t *intersect_union(jl_uniontype_t *a, jl_value_t *b,
369369
cenv_t *penv, cenv_t *eqc, variance_t var)
370370
{
371371
int eq0 = eqc->n, co0 = penv->n;
372-
jl_svec_t *t = jl_alloc_svec(jl_svec_len(a->types));
372+
size_t i, l = jl_svec_len(a->types);
373+
// shortcut an easy case: union contains type b
374+
if (!jl_is_typevar(b)) {
375+
for(i=0; i < l; i++) {
376+
if (jl_svecref(a->types,i) == b)
377+
return b;
378+
}
379+
}
380+
jl_svec_t *t = jl_alloc_svec(l);
373381
JL_GC_PUSH1(&t);
374-
size_t i, l=jl_svec_len(t);
375382
for(i=0; i < l; i++) {
376383
int eq_l = eqc->n, co_l = penv->n;
377384
jl_value_t *ti = jl_type_intersect(jl_svecref(a->types,i), b,

test/core.jl

+6
Original file line numberDiff line numberDiff line change
@@ -3619,3 +3619,9 @@ end
36193619
@test @m8846(a) === (:a, 0)
36203620
@test @m8846(a,1) === (:a, 1)
36213621
@test_throws MethodError eval(:(@m8846(a,b,c)))
3622+
3623+
# a simple case of parametric dispatch with unions
3624+
let foo{T}(x::Union{T,Void},y::Union{T,Void}) = 1
3625+
@test foo(1, nothing) === 1
3626+
@test_throws MethodError foo(nothing, nothing) # can't determine T
3627+
end

0 commit comments

Comments
 (0)