Skip to content

Commit

Permalink
fix soundness of inferred typevar bounds
Browse files Browse the repository at this point in the history
from #24399
  • Loading branch information
JeffBezanson committed Oct 30, 2017
1 parent 4aa4652 commit 5d2cf0a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/subtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,9 @@ static int subtype_unionall(jl_value_t *t, jl_unionall_t *u, jl_stenv_t *e, int8
jl_value_t *oldval = e->envout[e->envidx];
// if we try to assign different variable values (due to checking
// multiple union members), consider the value unknown.
if (!oldval || !jl_is_typevar(oldval) || !jl_is_long(val))
if (oldval && !jl_egal(oldval, val))
e->envout[e->envidx] = (jl_value_t*)u->var;
else
e->envout[e->envidx] = fix_inferred_var_bound(u->var, val);
// TODO: substitute the value (if any) of this variable into previous envout entries
}
Expand Down
15 changes: 15 additions & 0 deletions test/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1258,3 +1258,18 @@ let linfo = get_linfo(Base.convert, Tuple{Type{Int64}, Int32}),
@test opt.min_valid === Core.Inference.min_world(opt.linfo) > 2
@test opt.nargs == 3
end

# approximate static parameters due to unions
let T1 = Array{Float64}, T2 = Array{_1,2} where _1
inference_test_copy(a::T) where {T<:Array} = ccall(:jl_array_copy, Ref{T}, (Any,), a)
rt = Base.return_types(inference_test_copy, (Union{T1,T2},))[1]
@test rt >: T1 && rt >: T2

el(x::T) where {T} = eltype(T)
rt = Base.return_types(el, (Union{T1,Array{Float32,2}},))[1]
@test rt >: Union{Type{Float64}, Type{Float32}}

g(x::Ref{T}) where {T} = T
rt = Base.return_types(g, (Union{Ref{Array{Float64}}, Ref{Array{Float32}}},))[1]
@test rt >: Union{Type{Array{Float64}}, Type{Array{Float32}}}
end
5 changes: 5 additions & 0 deletions test/subtype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1229,3 +1229,8 @@ struct A23764{T, N, S} <: AbstractArray{Union{T, S}, N}; end
struct A23764_2{T, N, S} <: AbstractArray{Union{Ref{T}, S}, N}; end
@test Tuple{A23764_2{T, 1, Void} where T} <: Tuple{AbstractArray{T,N}} where {T,N}
@test Tuple{A23764_2{T, 1, Void} where T} <: Tuple{AbstractArray{T,N} where {T,N}}

# PR #24399
let (t, e) = intersection_env(Tuple{Union{Int,Int8}}, Tuple{T} where T)
@test e[1] isa TypeVar
end

0 comments on commit 5d2cf0a

Please sign in to comment.