diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 21be8b8670679..80c1a2d285273 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -128,7 +128,7 @@ prevind(::AbstractArray, i::Integer) = Int(i)-1 nextind(::AbstractArray, i::Integer) = Int(i)+1 eltype(::Type{<:AbstractArray{E}}) where {E} = @isdefined(E) ? E : Any -elsize(::AbstractArray{T}) where {T} = sizeof(T) +elsize(A::AbstractArray) = elsize(typeof(A)) """ ndims(A::AbstractArray) -> Integer diff --git a/base/array.jl b/base/array.jl index 2cab609e7fb4f..a88289af9c71b 100644 --- a/base/array.jl +++ b/base/array.jl @@ -135,7 +135,7 @@ function bitsunionsize(u::Union) end length(a::Array) = arraylen(a) -elsize(a::Array{T}) where {T} = isbits(T) ? sizeof(T) : (isbitsunion(T) ? bitsunionsize(T) : sizeof(Ptr)) +elsize(::Type{<:Array{T}}) where {T} = isbits(T) ? sizeof(T) : (isbitsunion(T) ? bitsunionsize(T) : sizeof(Ptr)) sizeof(a::Array) = Core.sizeof(a) function isassigned(a::Array, i::Int...) diff --git a/base/deprecated.jl b/base/deprecated.jl index 4986eab4d6bcb..e0154169efd79 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1147,8 +1147,8 @@ workspace() = error("`workspace()` is discontinued, consider Revise.jl for an al # Issues #17812 Remove default stride implementation function strides(a::AbstractArray) depwarn(""" - `strides(a::AbstractArray)` is deprecated for general arrays. - Specialize `strides` for custom array types that have the appropriate representation in memory. + The default `strides(a::AbstractArray)` implementation is deprecated for general arrays. + Specialize `strides(::$(typeof(a).name))` if `$(typeof(a).name)` indeed uses a strided representation in memory. Warning: inappropriately implementing this method for an array type that does not use strided storage may lead to incorrect results or segfaults. """, :strides) @@ -1157,6 +1157,18 @@ end @deprecate substrides(s, parent, dim, I::Tuple) substrides(parent, strides(parent), I) +# Issue #26072 Also remove default Base.elsize implementation +function elsize(t::Type{<:AbstractArray{T}}) where T + depwarn(""" + The default `Base.elsize(::Type{<:AbstractArray})` implementation is deprecated for general arrays. + Specialize `Base.elsize(::Type{<:$(t.name)})` if `$(t.name)` indeed has a known representation + in memory such that it represents the distance between two contiguous elements. + Warning: inappropriately implementing this method may lead to incorrect results or segfaults. + """, :elsize) + sizeof(T) +end + + @deprecate lexcmp(x::AbstractArray, y::AbstractArray) cmp(x, y) @deprecate lexcmp(x::Real, y::Real) cmp(isless, x, y) @deprecate lexcmp(x::Complex, y::Complex) cmp((real(x),imag(x)), (real(y),imag(y))) diff --git a/base/reinterpretarray.jl b/base/reinterpretarray.jl index efbec54c1e7e2..2a929abb2ac87 100644 --- a/base/reinterpretarray.jl +++ b/base/reinterpretarray.jl @@ -44,6 +44,7 @@ function size(a::ReinterpretArray{T,N,S} where {N}) where {T,S} tuple(size1, tail(psize)...) end +elsize(::Type{<:ReinterpretArray{T}}) where {T} = sizeof(T) unsafe_convert(::Type{Ptr{T}}, a::ReinterpretArray{T,N,S} where N) where {T,S} = Ptr{T}(unsafe_convert(Ptr{S},a.parent)) @inline @propagate_inbounds getindex(a::ReinterpretArray{T,0}) where {T} = reinterpret(T, a.parent[]) diff --git a/base/reshapedarray.jl b/base/reshapedarray.jl index 2c94ea834c8a8..3ac1c7cac616f 100644 --- a/base/reshapedarray.jl +++ b/base/reshapedarray.jl @@ -179,6 +179,7 @@ IndexStyle(::Type{<:ReshapedArrayLF}) = IndexLinear() parent(A::ReshapedArray) = A.parent parentindices(A::ReshapedArray) = map(s->1:s, size(parent(A))) reinterpret(::Type{T}, A::ReshapedArray, dims::Dims) where {T} = reinterpret(T, parent(A), dims) +elsize(::Type{<:ReshapedArray{<:Any,<:Any,P}}) where {P} = elsize(P) unaliascopy(A::ReshapedArray) = typeof(A)(unaliascopy(A.parent), A.dims, A.mi) dataids(A::ReshapedArray) = dataids(A.parent) diff --git a/base/strings/basic.jl b/base/strings/basic.jl index b6ce281c68d7f..06854d5d21962 100644 --- a/base/strings/basic.jl +++ b/base/strings/basic.jl @@ -648,6 +648,7 @@ length(s::CodeUnits) = ncodeunits(s.s) sizeof(s::CodeUnits{T}) where {T} = ncodeunits(s.s) * sizeof(T) size(s::CodeUnits) = (length(s),) strides(s::CodeUnits) = (1,) +elsize(s::CodeUnits{T}) where {T} = sizeof(T) @propagate_inbounds getindex(s::CodeUnits, i::Int) = codeunit(s.s, i) IndexStyle(::Type{<:CodeUnits}) = IndexLinear() start(s::CodeUnits) = 1 diff --git a/base/subarray.jl b/base/subarray.jl index 1bf1665e202ba..63806a2c26303 100644 --- a/base/subarray.jl +++ b/base/subarray.jl @@ -266,6 +266,8 @@ compute_stride1(s, inds, I::Tuple{AbstractRange, Vararg{Any}}) = s*step(I[1]) compute_stride1(s, inds, I::Tuple{Slice, Vararg{Any}}) = s compute_stride1(s, inds, I::Tuple{Any, Vararg{Any}}) = throw(ArgumentError("invalid strided index type $(typeof(I[1]))")) +elsize(::Type{<:SubArray{<:Any,<:Any,P}}) where {P} = elsize(P) + iscontiguous(A::SubArray) = iscontiguous(typeof(A)) iscontiguous(::Type{<:SubArray}) = false iscontiguous(::Type{<:FastContiguousSubArray}) = true diff --git a/stdlib/Random/src/RNGs.jl b/stdlib/Random/src/RNGs.jl index fa26d349805de..f4e4fda97210d 100644 --- a/stdlib/Random/src/RNGs.jl +++ b/stdlib/Random/src/RNGs.jl @@ -371,6 +371,7 @@ Base.getindex(a::UnsafeView, i::Int) = unsafe_load(a.ptr, i) Base.setindex!(a::UnsafeView, x, i::Int) = unsafe_store!(a.ptr, x, i) Base.pointer(a::UnsafeView) = a.ptr Base.size(a::UnsafeView) = (a.len,) +Base.elsize(::UnsafeView{T}) where {T} = sizeof(T) # this is essentially equivalent to rand!(r, ::AbstractArray{Float64}, I) above, but due to # optimizations which can't be done currently when working with pointers, we have to re-order diff --git a/test/abstractarray.jl b/test/abstractarray.jl index f56b173abb0c1..3ce0bd4f53ebe 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -772,7 +772,6 @@ end @testset "ndims and friends" begin @test ndims(Diagonal(rand(1:5,5))) == 2 @test ndims(Diagonal{Float64}) == 2 - @test Base.elsize(Diagonal(rand(1:5,5))) == sizeof(Int) end @testset "Issue #17811" begin