Skip to content

Commit

Permalink
fix #26453, require obviously-concrete lower bound for a var to be di…
Browse files Browse the repository at this point in the history
…agonal
  • Loading branch information
JeffBezanson committed Apr 4, 2018
1 parent e81dd5e commit eb3dd0f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
14 changes: 6 additions & 8 deletions src/subtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -520,11 +520,9 @@ static int is_leaf_bound(jl_value_t *v)
return !jl_is_type(v) && !jl_is_typevar(v);
}

static int is_leaf_typevar(jl_value_t *v)
static int is_leaf_typevar(jl_tvar_t *v)
{
if (jl_is_typevar(v))
return is_leaf_typevar(((jl_tvar_t*)v)->lb);
return is_leaf_bound(v);
return is_leaf_bound(v->lb);
}

static jl_value_t *widen_Type(jl_value_t *t)
Expand Down Expand Up @@ -630,7 +628,7 @@ static int subtype_unionall(jl_value_t *t, jl_unionall_t *u, jl_stenv_t *e, int8
// !( Tuple{Int, String} <: Tuple{T, T} where T)
// Then check concreteness by checking that the lower bound is not an abstract type.
int diagonal = !vb.occurs_inv && vb.occurs_cov > 1;
if (ans && (vb.concrete || (diagonal && is_leaf_typevar((jl_value_t*)u->var)))) {
if (ans && (vb.concrete || (diagonal && is_leaf_typevar(u->var)))) {
if (vb.concrete && !diagonal && !is_leaf_bound(vb.ub)) {
// a non-diagonal var can only be a subtype of a diagonal var if its
// upper bound is concrete.
Expand Down Expand Up @@ -1560,7 +1558,7 @@ static jl_value_t *intersect_unionall_(jl_value_t *t, jl_unionall_t *u, jl_stenv
else {
res = intersect(u->body, t, e, param);
}
vb->concrete |= (!vb->occurs_inv && vb->occurs_cov > 1 && is_leaf_typevar((jl_value_t*)u->var));
vb->concrete |= (!vb->occurs_inv && vb->occurs_cov > 1 && is_leaf_typevar(u->var));

// handle the "diagonal dispatch" rule, which says that a type var occurring more
// than once, and only in covariant position, is constrained to concrete types. E.g.
Expand Down Expand Up @@ -2028,11 +2026,11 @@ static jl_value_t *intersect(jl_value_t *x, jl_value_t *y, jl_stenv_t *e, int pa
if (ii == jl_bottom_type) return jl_bottom_type;
if (jl_is_typevar(xp1)) {
jl_varbinding_t *xb = lookup(e, (jl_tvar_t*)xp1);
if (xb && is_leaf_typevar((jl_value_t*)xb->var)) xb->concrete = 1;
if (xb && is_leaf_typevar(xb->var)) xb->concrete = 1;
}
if (jl_is_typevar(yp1)) {
jl_varbinding_t *yb = lookup(e, (jl_tvar_t*)yp1);
if (yb && is_leaf_typevar((jl_value_t*)yb->var)) yb->concrete = 1;
if (yb && is_leaf_typevar(yb->var)) yb->concrete = 1;
}
JL_GC_PUSH2(&ii, &i2);
// Vararg{T,N} <: Vararg{T2,N2}; equate N and N2
Expand Down
13 changes: 12 additions & 1 deletion test/subtype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function test_diagonal()
@test !issub(Tuple{Integer,Integer}, @UnionAll T Tuple{T,T})
@test issub(Tuple{Integer,Int}, (@UnionAll T @UnionAll S<:T Tuple{T,S}))
@test issub(Tuple{Integer,Int}, (@UnionAll T @UnionAll T<:S<:T Tuple{T,S}))
@test !issub(Tuple{Integer,Int,Int}, (@UnionAll T @UnionAll T<:S<:T Tuple{T,S,S}))
@test issub(Tuple{Integer,Int,Int}, (@UnionAll T @UnionAll T<:S<:T Tuple{T,S,S}))

@test issub_strict((@UnionAll R Tuple{R,R}),
(@UnionAll T @UnionAll S Tuple{T,S}))
Expand Down Expand Up @@ -132,6 +132,8 @@ function test_diagonal()
@test issub(Tuple{Tuple{T, T} where T>:Int}, Tuple{Tuple{T, T} where T>:Int})
@test issub(Tuple{Tuple{T, T} where T>:Int}, Tuple{Tuple{T, T}} where T>:Int)
@test issub(Tuple{Tuple{T, T}} where T>:Int, Tuple{Tuple{T, T} where T>:Int})
@test issub(Vector{Tuple{T, T} where Number<:T<:Number},
Vector{Tuple{Number, Number}})
end

# level 3: UnionAll
Expand Down Expand Up @@ -1273,3 +1275,12 @@ end
# issue #25240, #26405
f26405(::Type{T}) where {T<:Union{Integer, Missing}} = T
@test f26405(Union{Missing, Int}) == Union{Missing, Int}

# issue #26453
@test (Tuple{A,A,Number} where A>:Number) <: Tuple{T,T,S} where T>:S where S
@test (Tuple{T,T} where {S,T>:S}) == (Tuple{T,T} where {S,T>:S})
f26453(x::T,y::T) where {S,T>:S} = 0
@test f26453(1,2) == 0
@test f26453(1,"") == 0
g26453(x::T,y::T) where {S,T>:S} = T
@test_throws UndefVarError(:T) g26453(1,1)

0 comments on commit eb3dd0f

Please sign in to comment.