From a5ad11f984a80bdbd47ba8e02c344523ad368d6e Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Sat, 12 Jan 2019 15:34:32 -0500 Subject: [PATCH] restrict second argument of `size` and `axes` to Integer for most types --- base/abstractarray.jl | 4 ++-- base/array.jl | 2 +- base/bitarray.jl | 2 +- base/char.jl | 2 +- base/essentials.jl | 2 +- base/number.jl | 4 ++-- base/tuple.jl | 2 +- 7 files changed, 9 insertions(+), 9 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 92a9c4cc36247..0ee06afb71d06 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -35,7 +35,7 @@ julia> size(A, 2) 3 ``` """ -size(t::AbstractArray{T,N}, d) where {T,N} = d <= N ? size(t)[d] : 1 +size(t::AbstractArray{T,N}, d) where {T,N} = d::Integer <= N ? size(t)[d] : 1 """ axes(A, d) @@ -54,7 +54,7 @@ Base.OneTo(6) """ function axes(A::AbstractArray{T,N}, d) where {T,N} @_inline_meta - d <= N ? axes(A)[d] : OneTo(1) + d::Integer <= N ? axes(A)[d] : OneTo(1) end """ diff --git a/base/array.jl b/base/array.jl index f3d6ed916ed1e..e37a4cab166a4 100644 --- a/base/array.jl +++ b/base/array.jl @@ -151,7 +151,7 @@ function vect(X...) return copyto!(Vector{T}(undef, length(X)), X) end -size(a::Array, d) = arraysize(a, d) +size(a::Array, d::Integer) = arraysize(a, convert(Int, d)) size(a::Vector) = (arraysize(a,1),) size(a::Matrix) = (arraysize(a,1), arraysize(a,2)) size(a::Array{<:Any,N}) where {N} = (@_inline_meta; ntuple(M -> size(a, M), Val(N))) diff --git a/base/bitarray.jl b/base/bitarray.jl index aaefad792c8aa..078da2c0ae02a 100644 --- a/base/bitarray.jl +++ b/base/bitarray.jl @@ -77,7 +77,7 @@ length(B::BitArray) = B.len size(B::BitVector) = (B.len,) size(B::BitArray) = B.dims -@inline function size(B::BitVector, d) +@inline function size(B::BitVector, d::Integer) d < 1 && throw_boundserror(size(B), d) ifelse(d == 1, B.len, 1) end diff --git a/base/char.jl b/base/char.jl index 3f9d60380212b..6d7fdb045efc3 100644 --- a/base/char.jl +++ b/base/char.jl @@ -187,7 +187,7 @@ typemax(::Type{Char}) = reinterpret(Char, typemax(UInt32)) typemin(::Type{Char}) = reinterpret(Char, typemin(UInt32)) size(c::AbstractChar) = () -size(c::AbstractChar,d) = convert(Int, d) < 1 ? throw(BoundsError()) : 1 +size(c::AbstractChar, d::Integer) = d < 1 ? throw(BoundsError()) : 1 ndims(c::AbstractChar) = 0 ndims(::Type{<:AbstractChar}) = 0 length(c::AbstractChar) = 1 diff --git a/base/essentials.jl b/base/essentials.jl index 31ca8ade184d2..984ef41ff0129 100644 --- a/base/essentials.jl +++ b/base/essentials.jl @@ -570,7 +570,7 @@ eltype(::Type{SimpleVector}) = Any keys(v::SimpleVector) = OneTo(length(v)) isempty(v::SimpleVector) = (length(v) == 0) axes(v::SimpleVector) = (OneTo(length(v)),) -axes(v::SimpleVector, d) = d <= 1 ? axes(v)[d] : OneTo(1) +axes(v::SimpleVector, d::Integer) = d <= 1 ? axes(v)[d] : OneTo(1) function ==(v1::SimpleVector, v2::SimpleVector) length(v1)==length(v2) || return false diff --git a/base/number.jl b/base/number.jl index f2787a01acfad..4a4b001b2a79f 100644 --- a/base/number.jl +++ b/base/number.jl @@ -60,9 +60,9 @@ true isone(x) = x == one(x) # fallback method size(x::Number) = () -size(x::Number,d) = convert(Int,d)<1 ? throw(BoundsError()) : 1 +size(x::Number, d::Integer) = d < 1 ? throw(BoundsError()) : 1 axes(x::Number) = () -axes(x::Number,d) = convert(Int,d)<1 ? throw(BoundsError()) : OneTo(1) +axes(x::Number, d::Integer) = d < 1 ? throw(BoundsError()) : OneTo(1) eltype(::Type{T}) where {T<:Number} = T ndims(x::Number) = 0 ndims(::Type{<:Number}) = 0 diff --git a/base/tuple.jl b/base/tuple.jl index 3852ae14368c1..c8cf96b13502f 100644 --- a/base/tuple.jl +++ b/base/tuple.jl @@ -19,7 +19,7 @@ NTuple length(@nospecialize t::Tuple) = nfields(t) firstindex(@nospecialize t::Tuple) = 1 lastindex(@nospecialize t::Tuple) = length(t) -size(@nospecialize(t::Tuple), d) = (d == 1) ? length(t) : throw(ArgumentError("invalid tuple dimension $d")) +size(@nospecialize(t::Tuple), d::Integer) = (d == 1) ? length(t) : throw(ArgumentError("invalid tuple dimension $d")) axes(@nospecialize t::Tuple) = (OneTo(length(t)),) @eval getindex(@nospecialize(t::Tuple), i::Int) = getfield(t, i, $(Expr(:boundscheck))) @eval getindex(@nospecialize(t::Tuple), i::Real) = getfield(t, convert(Int, i), $(Expr(:boundscheck)))