Skip to content

fixes for WeakKeyDict, and some other error checks #20092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ immutable Const
Const(v::ANY) = new(v)
end

function rewrap(t::ANY, u::ANY)
isa(t, Const) && return t
rewrap_unionall(t, u)
end

type InferenceState
sp::SimpleVector # static parameters
label_counter::Int # index of the current highest label for this function
Expand Down Expand Up @@ -149,9 +154,9 @@ type InferenceState
end
s_types[1][la] = VarState(Tuple, false)
else
s_types[1][la] = VarState(rewrap_unionall(tuple_tfunc(limit_tuple_depth(params,
tupletype_tail(atypes, la))),
linfo.specTypes),
s_types[1][la] = VarState(rewrap(tuple_tfunc(limit_tuple_depth(params,
tupletype_tail(atypes, la))),
linfo.specTypes),
false)
end
la -= 1
Expand Down Expand Up @@ -572,8 +577,8 @@ function getfield_tfunc(s00::ANY, name)
end
s = DataType # typeof(p1)
elseif isa(s,Union)
return rewrap_unionall(tmerge(getfield_tfunc(s.a, name), getfield_tfunc(s.b, name)),
s00)
return rewrap(tmerge(getfield_tfunc(s.a, name), getfield_tfunc(s.b, name)),
s00)
elseif isa(s,Const)
sv = s.val
if isa(name, Const)
Expand Down
3 changes: 1 addition & 2 deletions base/weakkeydict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ function WeakKeyDict(kv)
try
Base.associative_with_eltype((K, V) -> WeakKeyDict{K, V}, kv, eltype(kv))
catch e
if any(x->isempty(methods(x, (typeof(kv),))), [start, next, done]) ||
!all(x->isa(x,Union{Tuple,Pair}),kv)
if !applicable(start, kv) || !all(x->isa(x,Union{Tuple,Pair}),kv)
throw(ArgumentError("WeakKeyDict(kv): kv needs to be an iterator of tuples or pairs"))
else
rethrow(e)
Expand Down
6 changes: 6 additions & 0 deletions src/jltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ JL_DLLEXPORT jl_value_t *jl_type_union(jl_value_t **ts, size_t n)

JL_DLLEXPORT jl_tvar_t *jl_new_typevar(jl_sym_t *name, jl_value_t *lb, jl_value_t *ub)
{
if (lb != jl_bottom_type && !jl_is_type(lb) && !jl_is_typevar(lb))
jl_type_error_rt("TypeVar", "lower bound", (jl_value_t*)jl_type_type, lb);
if (ub != (jl_value_t*)jl_any_type && !jl_is_type(ub) && !jl_is_typevar(ub))
jl_type_error_rt("TypeVar", "upper bound", (jl_value_t*)jl_type_type, ub);
jl_ptls_t ptls = jl_get_ptls_states();
jl_tvar_t *tv = (jl_tvar_t*)jl_gc_alloc(ptls, sizeof(jl_tvar_t), jl_tvar_type);
tv->name = name;
Expand All @@ -319,6 +323,8 @@ JL_DLLEXPORT jl_tvar_t *jl_new_typevar(jl_sym_t *name, jl_value_t *lb, jl_value_

JL_DLLEXPORT jl_value_t *jl_type_unionall(jl_tvar_t *v, jl_value_t *body)
{
if (!jl_is_type(body) && !jl_is_typevar(body))
jl_type_error_rt("UnionAll", "", (jl_value_t*)jl_type_type, body);
// normalize `T where T<:S` => S
if (body == (jl_value_t*)v)
return v->ub;
Expand Down
6 changes: 6 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ f47{T}(x::Vector{Vector{T}}) = 0
@test_throws MethodError f47(Array{Vector}(0))
@test f47(Array{Vector{Int}}(0)) == 0

# checking unionall and typevar components
@test_throws TypeError ([] where T)
@test_throws TypeError ([T] where T)
@test_throws TypeError (Array{T} where T<:[])
@test_throws TypeError (Array{T} where T>:[])

# issue #8652
args_morespecific(a, b) = ccall(:jl_type_morespecific, Cint, (Any,Any), a, b) != 0
let
Expand Down
5 changes: 5 additions & 0 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,11 @@ d = Dict(:a=>"a")
@test_throws ArgumentError Dict([1])
@test_throws ArgumentError Dict([(1,2),0])

# test Dict constructor's argument checking (for an iterable of pairs or tuples)
# make sure other errors can propagate when the nature of the iterator is not the problem
@test_throws InexactError Dict(convert(Int,1.5) for i=1:1)
@test_throws InexactError WeakKeyDict(convert(Int,1.5) for i=1:1)

# ImmutableDict
import Base.ImmutableDict
let d = ImmutableDict{String, String}(),
Expand Down