Skip to content

Commit

Permalink
make type declarations with triangular constraints work properly
Browse files Browse the repository at this point in the history
e.g. fixes #6721

fixes to type instantiation

[ci skip]
  • Loading branch information
JeffBezanson committed Dec 31, 2016
1 parent 0c7a2fd commit 51b5af2
Show file tree
Hide file tree
Showing 20 changed files with 291 additions and 169 deletions.
3 changes: 1 addition & 2 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ should define `linearindexing` in the type-domain:
Base.linearindexing{T<:MyArray}(::Type{T}) = Base.LinearFast()
"""
linearindexing(A::AbstractArray) = linearindexing(typeof(A))
linearindexing(::Type{Union{}}) = LinearFast()
linearindexing{T<:AbstractArray}(::Type{T}) = LinearSlow()
linearindexing{T<:Array}(::Type{T}) = LinearFast()
linearindexing{T<:Range}(::Type{T}) = LinearFast()
Expand Down Expand Up @@ -993,8 +994,6 @@ promote_eltype(v1, vs...) = promote_type(eltype(v1), promote_eltype(vs...))
#TODO: ERROR CHECK
cat(catdim::Integer) = Array{Any,1}(0)

vcat() = Array{Any,1}(0)
hcat() = Array{Any,1}(0)
typed_vcat{T}(::Type{T}) = Array{T,1}(0)
typed_hcat{T}(::Type{T}) = Array{T,1}(0)

Expand Down
4 changes: 4 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,10 @@ end


# concatenations of homogeneous combinations of vectors, horizontal and vertical

vcat() = Array{Any,1}(0)
hcat() = Array{Any,1}(0)

function hcat{T}(V::Vector{T}...)
height = length(V[1])
for j = 2:length(V)
Expand Down
3 changes: 1 addition & 2 deletions base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,7 @@ TypeVar(n::Symbol, ub::ANY) =
TypeVar(n::Symbol, lb::ANY, ub::ANY) =
ccall(:jl_new_typevar, Ref{TypeVar}, (Any, Any, Any), n, lb, ub)

UnionAll(v::TypeVar, t::ANY) =
ccall(:jl_new_unionall_type, Ref{UnionAll}, (Any, Any), v, t)
UnionAll(v::TypeVar, t::ANY) = ccall(:jl_type_unionall, Any, (Any, Any), v, t)

Void() = nothing

Expand Down
12 changes: 6 additions & 6 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ end
@deprecate get_rounding rounding

#13465
@deprecate cov(x::AbstractVector; corrected=true, mean=Base.mean(x)) Base.covm(x, mean, corrected)
@deprecate cov(X::AbstractMatrix; vardim=1, corrected=true, mean=Base.mean(X, vardim)) Base.covm(X, mean, vardim, corrected)
@deprecate cov(x::AbstractVector, y::AbstractVector; corrected=true, mean=(Base.mean(x), Base.mean(y))) Base.covm(x, mean[1], y, mean[2], corrected)
#@deprecate cov(x::AbstractVector; corrected=true, mean=Base.mean(x)) Base.covm(x, mean, corrected)
#@deprecate cov(X::AbstractMatrix; vardim=1, corrected=true, mean=Base.mean(X, vardim)) Base.covm(X, mean, vardim, corrected)
#@deprecate cov(x::AbstractVector, y::AbstractVector; corrected=true, mean=(Base.mean(x), Base.mean(y))) Base.covm(x, mean[1], y, mean[2], corrected)
@deprecate cov(X::AbstractVecOrMat, Y::AbstractVecOrMat; vardim=1, corrected=true, mean=(Base.mean(X, vardim), Base.mean(Y, vardim))) Base.covm(X, mean[1], Y, mean[2], vardim, corrected)

@deprecate cor(x::AbstractVector; mean=Base.mean(x)) Base.corm(x, mean)
@deprecate cor(X::AbstractMatrix; vardim=1, mean=Base.mean(X, vardim)) Base.corm(X, mean, vardim)
@deprecate cor(x::AbstractVector, y::AbstractVector; mean=(Base.mean(x), Base.mean(y))) Base.corm(x, mean[1], y, mean[2])
#@deprecate cor(x::AbstractVector; mean=Base.mean(x)) Base.corm(x, mean)
#@deprecate cor(X::AbstractMatrix; vardim=1, mean=Base.mean(X, vardim)) Base.corm(X, mean, vardim)
#@deprecate cor(x::AbstractVector, y::AbstractVector; mean=(Base.mean(x), Base.mean(y))) Base.corm(x, mean[1], y, mean[2])
@deprecate cor(X::AbstractVecOrMat, Y::AbstractVecOrMat; vardim=1, mean=(Base.mean(X, vardim), Base.mean(Y, vardim))) Base.corm(X, mean[1], Y, mean[2], vardim)

@deprecate_binding SparseMatrix SparseArrays
Expand Down
2 changes: 1 addition & 1 deletion base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ const AnyDict = Dict{Any,Any}

Dict{K,V}(ps::Pair{K,V}...) = Dict{K,V}(ps)
Dict{K }(ps::Pair{K}...,) = Dict{K,Any}(ps)
Dict{V }(ps::Pair{TypeVar(:K),V}...,) = Dict{Any,V}(ps)
Dict{V }(ps::(Pair{K,V} where K)...,) = Dict{Any,V}(ps)
Dict( ps::Pair...) = Dict{Any,Any}(ps)

function Dict(kv)
Expand Down
11 changes: 6 additions & 5 deletions base/docs/Docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ function signature(expr::Expr)
end
push!(sig.args[end].args, argtype(arg))
end
Expr(:let, Expr(:block, sig), typevars(expr)...)
tv = typevars(expr)
for i = length(tv):-1:1
sig = Expr(:where, sig, tv[i])
end
sig
else
signature(expr.args[1])
end
Expand All @@ -103,14 +107,11 @@ end
argtype(other) = :Any

function typevars(expr::Expr)
isexpr(expr, :curly) && return [tvar(x) for x in expr.args[2:end]]
isexpr(expr, :curly) && return expr.args[2:end]
typevars(expr.args[1])
end
typevars(::Symbol) = []

tvar(x::Expr) = :($(x.args[1]) = TypeVar($(quot(x.args[1])), $(x.args[2]), true))
tvar(s::Symbol) = :($(s) = TypeVar($(quot(s)), Any, true))

# Docsystem types.
# ================

Expand Down
3 changes: 1 addition & 2 deletions base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -737,8 +737,7 @@ function invoke_tfunc(f::ANY, types::ANY, argtype::ANY, sv::InferenceState)
return Any
end
meth = entry.func
(ti, env) = ccall(:jl_match_method, Any, (Any, Any, Any),
argtype, meth.sig, meth.tvars)::SimpleVector
(ti, env) = ccall(:jl_match_method, Any, (Any, Any, Any), argtype, meth.sig)::SimpleVector
return typeinf_edge(meth::Method, ti, env, sv)
end

Expand Down
3 changes: 1 addition & 2 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,7 @@ function _dump_function(f::ANY, t::ANY, native::Bool, wrapper::Bool,
t = to_tuple_type(t)
ft = isa(f, Type) ? Type{f} : typeof(f)
tt = Tuple{ft, t.parameters...}
(ti, env) = ccall(:jl_match_method, Any, (Any, Any, Any),
tt, meth.sig, meth.tvars)::SimpleVector
(ti, env) = ccall(:jl_match_method, Any, (Any, Any, Any), tt, meth.sig)::SimpleVector
meth = func_for_method_checked(meth, tt)
linfo = ccall(:jl_specializations_get_linfo, Ref{Core.MethodInstance}, (Any, Any, Any, UInt), meth, tt, env, world)
# get the code for it
Expand Down
2 changes: 1 addition & 1 deletion base/sparse/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ sparse{Tv}(FC::FactorComponent{Tv,:LD}) = sparse(Sparse(Factor(FC)))

# Calculate the offset into the stype field of the cholmod_sparse_struct and
# change the value
let offset = fieldoffset(C_Sparse{Float64}, findfirst(name -> name === :stype, fieldnames(C_Sparse)))
let offset = fieldoffset(C_Sparse{Float64}, findfirst(name -> name === :stype, fieldnames(C_Sparse{Float64})))
global change_stype!
function change_stype!(A::Sparse, i::Integer)
unsafe_store!(convert(Ptr{Cint}, A.p), i, div(offset, 4) + 1)
Expand Down
24 changes: 1 addition & 23 deletions src/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -686,11 +686,6 @@ jl_method_t *jl_new_method(jl_code_info_t *definition,
m->tvars = (jl_svec_t*)jl_svecref(tvars, 0);
else
m->tvars = tvars;
int j;
for(j=(int)jl_svec_len(tvars)-1; j >= 0 ; j--) {
m->sig = jl_new_unionall_type((jl_tvar_t*)jl_svecref(tvars,j), m->sig);
jl_gc_wb(m, m->sig);
}
m->sparam_syms = sparam_syms;
root = (jl_value_t*)m;
jl_method_set_source(m, definition);
Expand Down Expand Up @@ -1170,7 +1165,7 @@ JL_DLLEXPORT jl_datatype_t *jl_new_datatype(jl_sym_t *name, jl_datatype_t *super
int i;
int np = jl_svec_len(parameters);
for (i=np-1; i >= 0; i--) {
t->name->wrapper = (jl_value_t*)jl_new_unionall_type((jl_tvar_t*)jl_svecref(parameters,i), t->name->wrapper);
t->name->wrapper = jl_new_struct(jl_unionall_type, jl_svecref(parameters,i), t->name->wrapper);
jl_gc_wb(t->name, t->name->wrapper);
}
}
Expand Down Expand Up @@ -1207,23 +1202,6 @@ JL_DLLEXPORT jl_datatype_t *jl_new_bitstype(jl_value_t *name, jl_datatype_t *sup
return bt;
}

// unionall types -------------------------------------------------------------

JL_DLLEXPORT jl_tvar_t *jl_new_typevar(jl_sym_t *name, jl_value_t *lb, jl_value_t *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;
tv->lb = lb;
tv->ub = ub;
return tv;
}

JL_DLLEXPORT jl_unionall_t *jl_new_unionall_type(jl_tvar_t *v, jl_value_t *body)
{
return (jl_unionall_t*)jl_new_struct(jl_unionall_type, v, body);
}

// bits constructors ----------------------------------------------------------

#define BOXN_FUNC(nb,nw) \
Expand Down
5 changes: 5 additions & 0 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,11 @@ JL_DLLEXPORT size_t jl_static_show_func_sig(JL_STREAM *s, jl_value_t *type)
}
// TODO: better way to show method parameters
type = jl_unwrap_unionall(type);
if (!jl_is_datatype(type)) {
n += jl_printf(s, " ");
n += jl_static_show(s, type);
return n;
}
size_t tl = jl_nparams(type);
n += jl_printf(s, "(");
size_t i;
Expand Down
2 changes: 1 addition & 1 deletion src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -2783,7 +2783,7 @@ jl_method_instance_t *jl_recache_method_instance(jl_method_instance_t *li, size_
jl_datatype_t *argtypes = li->specTypes;
jl_set_typeof(li, (void*)(intptr_t)0x40); // invalidate the old value to help catch errors
jl_svec_t *env = jl_emptysvec;
jl_value_t *ti = jl_type_intersection_matching((jl_value_t*)m->sig, (jl_value_t*)argtypes, &env, m->tvars);
jl_value_t *ti = jl_type_intersection_matching((jl_value_t*)m->sig, (jl_value_t*)argtypes, &env);
//assert(ti != jl_bottom_type); (void)ti;
if (ti == jl_bottom_type)
env = jl_emptysvec; // the intersection may fail now if the type system had made an incorrect subtype env in the past
Expand Down
14 changes: 5 additions & 9 deletions src/gf.c
Original file line number Diff line number Diff line change
Expand Up @@ -2266,8 +2266,7 @@ jl_value_t *jl_gf_invoke(jl_tupletype_t *types0, jl_value_t **args, size_t nargs
else {
tt = arg_type_tuple(args, nargs);
if (entry->tvars != jl_emptysvec) {
jl_value_t *ti =
jl_lookup_match((jl_value_t*)tt, (jl_value_t*)entry->sig, &tpenv, entry->tvars);
jl_value_t *ti = jl_lookup_match((jl_value_t*)tt, (jl_value_t*)entry->sig, &tpenv);
assert(ti != (jl_value_t*)jl_bottom_type);
(void)ti;
}
Expand Down Expand Up @@ -2334,13 +2333,12 @@ jl_function_t *jl_new_generic_function(jl_sym_t *name, jl_module_t *module)
return jl_new_generic_function_with_supertype(name, module, jl_function_type, 0);
}

JL_DLLEXPORT jl_svec_t *jl_match_method(jl_value_t *type, jl_value_t *sig,
jl_svec_t *tvars)
JL_DLLEXPORT jl_svec_t *jl_match_method(jl_value_t *type, jl_value_t *sig)
{
jl_svec_t *env = jl_emptysvec;
jl_value_t *ti=NULL;
JL_GC_PUSH2(&env, &ti);
ti = jl_lookup_match(type, (jl_value_t*)sig, &env, tvars);
ti = jl_lookup_match(type, (jl_value_t*)sig, &env);
jl_svec_t *result = jl_svec2(ti, env);
JL_GC_POP();
return result;
Expand Down Expand Up @@ -2489,8 +2487,7 @@ static int ml_matches_visitor(jl_typemap_entry_t *ml, struct typemap_intersectio
jl_method_t *mambig = (jl_method_t*)jl_array_ptr_ref(meth->ambig, j);
env = jl_emptysvec;
jl_value_t *mti = jl_type_intersection_matching((jl_value_t*)closure->match.type,
(jl_value_t*)mambig->sig,
&env, mambig->tvars);
(jl_value_t*)mambig->sig, &env);
if (mti != (jl_value_t*)jl_bottom_type) {
if (closure->include_ambiguous) {
assert(done);
Expand All @@ -2512,8 +2509,7 @@ static int ml_matches_visitor(jl_typemap_entry_t *ml, struct typemap_intersectio
// the current method doesn't match if there is an intersection with an
// ambiguous method that covers our intersection with this one.
jl_value_t *ambi = jl_type_intersection_matching((jl_value_t*)ml->sig,
(jl_value_t*)mambig->sig,
&env, mambig->tvars);
(jl_value_t*)mambig->sig, &env);
if (jl_subtype(closure->match.ti, ambi)) {
return_this_match = 0;
break;
Expand Down
Loading

0 comments on commit 51b5af2

Please sign in to comment.