Skip to content

Commit

Permalink
dispatch: fix incorrect signature narrowing for U{Type, S} (#31671)
Browse files Browse the repository at this point in the history
it should only widen a signature, not narrow it, but was using intersect instead of subtype

fix #31406
  • Loading branch information
vtjnash authored and JeffBezanson committed Apr 11, 2019
1 parent 1001087 commit b4e95cb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/gf.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ JL_DLLEXPORT void jl_set_typeinf_func(jl_value_t *f)

static int very_general_type(jl_value_t *t)
{
return (t == (jl_value_t*)jl_any_type || t == (jl_value_t*)jl_type_type || jl_types_equal(t, (jl_value_t*)jl_type_type));
return (t == (jl_value_t*)jl_any_type || jl_types_equal(t, (jl_value_t*)jl_type_type));
}

jl_value_t *jl_nth_slot_type(jl_value_t *sig, size_t i)
Expand Down Expand Up @@ -528,17 +528,17 @@ static void jl_compilation_sig(
}
}

if (jl_types_equal(elt, (jl_value_t*)jl_typetype_type)) {
if (jl_types_equal(elt, (jl_value_t*)jl_typetype_type)) { // elt == Type{T} where T
// not triggered for isdispatchtuple(tt), this attempts to handle
// some cases of adapting a random signature into a compilation signature
}
else if (!jl_is_datatype(elt) && !jl_has_empty_intersection((jl_value_t*)jl_type_type, elt)) {
else if (!jl_is_datatype(elt) && jl_subtype(elt, (jl_value_t*)jl_type_type)) { // elt <: Type{T}
// not triggered for isdispatchtuple(tt), this attempts to handle
// some cases of adapting a random signature into a compilation signature
if (!*newparams) *newparams = jl_svec_copy(tt->parameters);
jl_svecset(*newparams, i, jl_typetype_type);
}
else if (jl_is_type_type(elt)) {
else if (jl_is_type_type(elt)) { // elt isa Type{T}
if (very_general_type(decl_i)) {
/*
here's a fairly simple heuristic: if this argument slot's
Expand Down
21 changes: 21 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6894,3 +6894,24 @@ function foo31357(b::Bool)
end
@test foo31357(true) == 12345
@test foo31357(false) == 12345

# Issue #31406
abstract type Shape31406 end
struct ValueOf31406 <: Shape31406
ty::Type
end
struct TupleOf31406 <: Shape31406
cols::Vector{Shape31406}
end
TupleOf31406(cols::Union{Shape31406,Type}...) = TupleOf31406(collect(Shape31406, cols))
@test (TupleOf31406(ValueOf31406(Int64), ValueOf31406(Float64))::TupleOf31406).cols ==
Shape31406[ValueOf31406(Int64), ValueOf31406(Float64)]
@test try
TupleOf31406(ValueOf31406(Int64), Float64)
false
catch ex
if !(ex isa MethodError && ex.f === convert && ex.args == (Shape31406, Float64))
rethrow(ex)
end
true
end

0 comments on commit b4e95cb

Please sign in to comment.