Skip to content

Commit

Permalink
reduce method overwrite warning occurances
Browse files Browse the repository at this point in the history
this moves the lowest-level Array constructor to Core

and refactors a bit of other code to remove some other constructors from
the Core.Inference image

the net result is that only a single method-overwritten warning remains
from the process of building the sysimg.jl file:

    WARNING: Method definition (::Type{#T<:Any})(Any) in module Inference at essentials.jl:21 overwritten in module Base at essentials.jl:21.
  • Loading branch information
vtjnash authored and JeffBezanson committed Jan 26, 2016
1 parent abf9d70 commit 9c6239a
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 64 deletions.
33 changes: 0 additions & 33 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,8 @@ typealias DenseVector{T} DenseArray{T,1}
typealias DenseMatrix{T} DenseArray{T,2}
typealias DenseVecOrMat{T} Union{DenseVector{T}, DenseMatrix{T}}

call{T}(::Type{Vector{T}}, m::Integer) = Array{T}(m)
call{T}(::Type{Vector{T}}) = Array{T}(0)
call(::Type{Vector}, m::Integer) = Array{Any}(m)
call(::Type{Vector}) = Array{Any}(0)

call{T}(::Type{Matrix{T}}, m::Integer, n::Integer) = Array{T}(m, n)
call{T}(::Type{Matrix{T}}) = Array{T}(0, 0)
call(::Type{Matrix}, m::Integer, n::Integer) = Array{Any}(m, n)
call(::Type{Matrix}) = Array{Any}(0, 0)

## Basic functions ##

# convert Arrays to pointer arrays for ccall
function call{P<:Ptr,T<:Ptr}(::Type{Ref{P}}, a::Array{T}) # Ref{P<:Ptr}(a::Array{T<:Ptr})
return RefArray(a) # effectively a no-op
end
function call{P<:Ptr,T}(::Type{Ref{P}}, a::Array{T}) # Ref{P<:Ptr}(a::Array)
if (!isbits(T) && T <: eltype(P))
# this Array already has the right memory layout for the requested Ref
return RefArray(a,1,false) # root something, so that this function is type-stable
else
ptrs = Array(P, length(a)+1)
roots = Array(Any, length(a))
for i = 1:length(a)
root = cconvert(P, a[i])
ptrs[i] = unsafe_convert(P, root)::P
roots[i] = root
end
ptrs[length(a)+1] = C_NULL
return RefArray(ptrs,1,roots)
end
end
cconvert{P<:Ptr,T<:Ptr}(::Union{Type{Ptr{P}},Type{Ref{P}}}, a::Array{T}) = a
cconvert{P<:Ptr}(::Union{Type{Ptr{P}},Type{Ref{P}}}, a::Array) = Ref{P}(a)

size(a::Array, d) = arraysize(a, d)
size(a::Vector) = (arraysize(a,1),)
size(a::Matrix) = (arraysize(a,1), arraysize(a,2))
Expand Down
21 changes: 21 additions & 0 deletions base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,25 @@ convert{T}(::Type{T}, x::T) = x
cconvert(T::Type, x) = convert(T, x)
unsafe_convert{T}(::Type{T}, x::T) = x

# primitive array constructors
(::Type{Array{T,N}}){T,N}(d::NTuple{N,Int}) =
ccall(:jl_new_array, Array{T,N}, (Any,Any), Array{T,N}, d)
(::Type{Array{T,1}}){T}(m::Int) =
ccall(:jl_alloc_array_1d, Array{T,1}, (Any,Int), Array{T,1}, m)
(::Type{Array{T,2}}){T}(m::Int, n::Int) =
ccall(:jl_alloc_array_2d, Array{T,2}, (Any,Int,Int), Array{T,2}, m, n)
(::Type{Array{T,3}}){T}(m::Int, n::Int, o::Int) =
ccall(:jl_alloc_array_3d, Array{T,3}, (Any,Int,Int,Int), Array{T,3}, m, n, o)

(::Type{Array{T}}){T,N}(d::NTuple{N,Int}) = Array{T,N}(d)
(::Type{Array{T}}){T}(m::Int) = Array{T,1}(m)
(::Type{Array{T}}){T}(m::Int, n::Int) = Array{T,2}(m, n)
(::Type{Array{T}}){T}(m::Int, n::Int, o::Int) = Array{T,3}(m, n, o)

# TODO: possibly turn these into deprecations
Array{T}(::Type{T}, d::Int...) = Array{T}(d)
Array{T}(::Type{T}, m::Int) = Array{T,1}(m)
Array{T}(::Type{T}, m::Int,n::Int) = Array{T,2}(m,n)
Array{T}(::Type{T}, m::Int,n::Int,o::Int) = Array{T,3}(m,n,o)

ccall(:jl_set_istopmod, Void, (Bool,), true)
26 changes: 1 addition & 25 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ macro _propagate_inbounds_meta()
Expr(:meta, :inline, :propagate_inbounds)
end

# The specialization for 1 arg is important when running with --inline=no, see #11158
call{T}(::Type{T}, arg) = convert(T, arg)::T
call{T}(::Type{T}, args...) = convert(T, args...)::T

# `convert` fallbacks for constructors defined in boot.jl
(T::Type{ASCIIString})(args...) = convert(T, args...)
(T::Type{UTF8String})(args...) = convert(T, args...)
(::Type{T}){T}(arg) = convert(T, arg)::T

convert{T}(::Type{T}, x::T) = x

Expand Down Expand Up @@ -155,24 +149,6 @@ macro goto(name::Symbol)
Expr(:symbolicgoto, name)
end

call{T,N}(::Type{Array{T}}, d::NTuple{N,Int}) =
ccall(:jl_new_array, Array{T,N}, (Any,Any), Array{T,N}, d)
call{T}(::Type{Array{T}}, d::Integer...) = Array{T}(convert(Tuple{Vararg{Int}}, d))

call{T}(::Type{Array{T}}, m::Integer) =
ccall(:jl_alloc_array_1d, Array{T,1}, (Any,Int), Array{T,1}, m)
call{T}(::Type{Array{T}}, m::Integer, n::Integer) =
ccall(:jl_alloc_array_2d, Array{T,2}, (Any,Int,Int), Array{T,2}, m, n)
call{T}(::Type{Array{T}}, m::Integer, n::Integer, o::Integer) =
ccall(:jl_alloc_array_3d, Array{T,3}, (Any,Int,Int,Int), Array{T,3}, m, n, o)

# TODO: possibly turn these into deprecations
Array{T,N}(::Type{T}, d::NTuple{N,Int}) = Array{T}(d)
Array{T}(::Type{T}, d::Integer...) = Array{T}(convert(Tuple{Vararg{Int}}, d))
Array{T}(::Type{T}, m::Integer) = Array{T}(m)
Array{T}(::Type{T}, m::Integer,n::Integer) = Array{T}(m,n)
Array{T}(::Type{T}, m::Integer,n::Integer,o::Integer) = Array{T}(m,n,o)

# SimpleVector

function getindex(v::SimpleVector, i::Int)
Expand Down
4 changes: 2 additions & 2 deletions base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1406,8 +1406,8 @@ CYCLE_ID = 1

# def is the original unspecialized version of a method. we aggregate all
# saved type inference data there.
function typeinf(linfo::LambdaStaticData,atypes::ANY,sparams::SimpleVector, def, cop, needtree)
if linfo.module === Core
function typeinf(linfo::LambdaStaticData, atypes::ANY, sparams::SimpleVector, def, cop, needtree)
if linfo.module === Core && isempty(sparams) && isempty(linfo.sparam_vals)
atypes = Tuple
end
#dbg =
Expand Down
23 changes: 23 additions & 0 deletions base/refpointer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,29 @@ function unsafe_convert(P::Type{Ptr{Any}}, b::RefArray{Any})
end
unsafe_convert{T}(::Type{Ptr{Void}}, b::RefArray{T}) = convert(Ptr{Void}, unsafe_convert(Ptr{T}, b))

# convert Arrays to pointer arrays for ccall
function call{P<:Ptr,T<:Ptr}(::Type{Ref{P}}, a::Array{T}) # Ref{P<:Ptr}(a::Array{T<:Ptr})
return RefArray(a) # effectively a no-op
end
function call{P<:Ptr,T}(::Type{Ref{P}}, a::Array{T}) # Ref{P<:Ptr}(a::Array)
if (!isbits(T) && T <: eltype(P))
# this Array already has the right memory layout for the requested Ref
return RefArray(a,1,false) # root something, so that this function is type-stable
else
ptrs = Array(P, length(a)+1)
roots = Array(Any, length(a))
for i = 1:length(a)
root = cconvert(P, a[i])
ptrs[i] = unsafe_convert(P, root)::P
roots[i] = root
end
ptrs[length(a)+1] = C_NULL
return RefArray(ptrs,1,roots)
end
end
cconvert{P<:Ptr,T<:Ptr}(::Union{Type{Ptr{P}},Type{Ref{P}}}, a::Array{T}) = a
cconvert{P<:Ptr}(::Union{Type{Ptr{P}},Type{Ref{P}}}, a::Array) = Ref{P}(a)

###

getindex(b::RefValue) = b.x
Expand Down
4 changes: 0 additions & 4 deletions base/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,6 @@ function showerror_nostdio(err, msg::AbstractString)
ccall(:jl_printf, UInt, (Ptr{Void},Cstring), stderr_stream, "\n")
end

const UNSHOWN_METHODS = ObjectIdDict(
which(Type, Tuple{Vararg{Any}}) => true
)
function show_method_candidates(io::IO, ex::MethodError)
is_arg_types = isa(ex.args, DataType)
arg_types = is_arg_types ? ex.args : typesof(ex.args...)
Expand All @@ -270,7 +267,6 @@ function show_method_candidates(io::IO, ex::MethodError)

for (func,arg_types_param) in funcs
for method in func.name.mt
haskey(UNSHOWN_METHODS, method) && continue
buf = IOBuffer()
s1 = method.sig.parameters[1]
sig = method.sig.parameters[2:end]
Expand Down
23 changes: 23 additions & 0 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,29 @@ include("abstractarray.jl")
include("subarray.jl")
include("array.jl")

# Array convenience converting constructors
(::Type{Array{T}}){T}(m::Integer) = Array{T}(Int(m))
(::Type{Array{T}}){T}(m::Integer, n::Integer) = Array{T}(Int(m), Int(n))
(::Type{Array{T}}){T}(m::Integer, n::Integer, o::Integer) = Array{T}(Int(m), Int(n), Int(o))
(::Type{Array{T}}){T}(d::Integer...) = Array{T}(convert(Tuple{Vararg{Int}}, d))

(::Type{Vector{T}}){T}(m::Integer) = Array{T}(m)
(::Type{Vector{T}}){T}() = Array{T}(0)
(::Type{Vector})(m::Integer) = Array{Any}(m)
(::Type{Vector})() = Array{Any}(0)

(::Type{Matrix{T}}){T}(m::Integer, n::Integer) = Array{T}(m, n)
(::Type{Matrix{T}}){T}() = Array{T}(0, 0)
(::Type{Matrix})(m::Integer, n::Integer) = Array{Any}(m, n)
(::Type{Matrix})() = Array{Any}(0, 0)

# TODO: possibly turn these into deprecations
Array{T,N}(::Type{T}, d::NTuple{N,Int}) = Array{T}(d)
Array{T}(::Type{T}, d::Integer...) = Array{T}(convert(Tuple{Vararg{Int}}, d))
Array{T}(::Type{T}, m::Integer) = Array{T}(m)
Array{T}(::Type{T}, m::Integer,n::Integer) = Array{T}(m,n)
Array{T}(::Type{T}, m::Integer,n::Integer,o::Integer) = Array{T}(m,n,o)

# numeric operations
include("hashing.jl")
include("rounding.jl")
Expand Down

0 comments on commit 9c6239a

Please sign in to comment.