Skip to content

Commit

Permalink
fix method lookup intersection error
Browse files Browse the repository at this point in the history
fix #23024 and improve subtype test macro to always check for this

Ref #23058
(cherry picked from commit fef5313)
  • Loading branch information
vtjnash committed Sep 14, 2017
1 parent 22f5ec1 commit 0e59aa3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
15 changes: 2 additions & 13 deletions src/subtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -2146,20 +2146,9 @@ jl_value_t *jl_type_intersection_env_s(jl_value_t *a, jl_value_t *b, jl_svec_t *
else {
sz = szb;
// TODO: compute better `env` directly during intersection.
// we assume that if the intersection is a leaf type, we have
// full information in `env`. however the intersection algorithm
// does not yet provide that in all cases so use subtype.
// for now, we attempt to compute env by using subtype on the intersection result
if (szb > 0 && !jl_types_equal(b, (jl_value_t*)jl_type_type)) {
if (jl_subtype_env(*ans, b, env, szb)) {
if (jl_is_leaf_type(*ans)) {
for(i=0; i < sz; i++) {
if (jl_is_typevar(env[i])) {
*ans = jl_bottom_type; goto bot;
}
}
}
}
else {
if (!jl_subtype_env(*ans, b, env, szb)) {
sz = 0;
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -884,3 +884,12 @@ let isa_tfunc = Core.Inference.t_ffunc_val[
@test isa_tfunc(c, Type{Complex{T}} where T) === Const(false)
end
end

function f23024(::Type{T}, ::Int) where T
1 + 1
end
v23024 = 0
g23024(TT::Tuple{DataType}) = f23024(TT[1], v23024)
@test Base.return_types(f23024, (DataType, Any)) == Any[Int]
@test Base.return_types(g23024, (Tuple{DataType},)) == Any[Int]
@test g23024((UInt8,)) === 2
25 changes: 20 additions & 5 deletions test/subtype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -600,15 +600,25 @@ function test_properties()
end

macro testintersect(a, b, result)
if isa(result,Expr) && result.head === :call && length(result.args)==2 && result.args[1] === :!
if isa(result, Expr) && result.head === :call && length(result.args) == 2 && result.args[1] === :!
result = result.args[2]
cmp = :notequal_type
cmp = :(!=)
else
cmp = :isequal_type
cmp = :(==)
end
cmp = esc(cmp)
a = esc(a)
b = esc(b)
result = esc(result)
Base.remove_linenums!(quote
@test $(esc(cmp))(_type_intersect($(esc(a)), $(esc(b))), $(esc(result)))
@test $(esc(cmp))(_type_intersect($(esc(b)), $(esc(a))), $(esc(result)))
# test real intersect
@test $cmp(_type_intersect($a, $b), $result)
@test $cmp(_type_intersect($b, $a), $result)
# test simplified intersect
if !($result === Union{})
@test typeintersect($a, $b) != Union{}
@test typeintersect($b, $a) != Union{}
end
end)
end

Expand Down Expand Up @@ -1115,3 +1125,8 @@ end
@testintersect(Val{Pair{T,T}} where T,
Val{Pair{Int,T}} where T,
Val{Pair{Int,Int}})

# issue #23024
@testintersect(Tuple{DataType, Any},
Tuple{Type{T}, Int} where T,
Tuple{DataType, Int})

0 comments on commit 0e59aa3

Please sign in to comment.