Skip to content

Remove some uses of @pure #1253

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 1 commit into from
May 16, 2024
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "StaticArrays"
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.9.3"
version = "1.9.4"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ same vector. We can now do this with the `Scalar` type:
The size of a statically sized array is a static parameter associated with the
type of the array. The `Size` trait is provided as an abstract representation of
the dimensions of a static array. An array `sa::SA` of size `(dims...)` is
associated with `Size{(dims...)}()`. The following are equivalent (`@pure`)
associated with `Size{(dims...)}()`. The following are equivalent
constructors:
```julia
Size{(dims...,)}()
Expand Down
8 changes: 5 additions & 3 deletions src/SHermitianCompact.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ end
end
end

Base.@pure triangularnumber(N::Int) = div(N * (N + 1), 2)
Base.@pure triangularroot(L::Int) = div(isqrt(8 * L + 1) - 1, 2) # from quadratic formula
triangularnumber(N::Int) = div(N * (N + 1), 2)
@generated function triangularroot(::Val{L}) where {L}
return div(isqrt(8 * L + 1) - 1, 2) # from quadratic formula
end
Comment on lines +48 to +50
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have to be a @generated function with a Val argument? To me it looks like constant propagation works like a charm for plain

triangularroot(L::Int) = div(isqrt(8 * L + 1) - 1, 2)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard to tell for me how it would interact with inlining if it were a normal function. We can always change it in a later PR.


lowertriangletype(::Type{SHermitianCompact{N, T, L}}) where {N, T, L} = SVector{L, T}
lowertriangletype(::Type{SHermitianCompact{N, T}}) where {N, T} = SVector{triangularnumber(N), T}
Expand All @@ -55,7 +57,7 @@ lowertriangletype(::Type{SHermitianCompact{N}}) where {N} = SVector{triangularnu
@inline SHermitianCompact{N}(lowertriangle::SVector{L, T}) where {N, T, L} = SHermitianCompact{N, T, L}(lowertriangle)

@inline function SHermitianCompact(lowertriangle::SVector{L, T}) where {T, L}
N = triangularroot(L)
N = triangularroot(Val(L))
SHermitianCompact{N, T, L}(lowertriangle)
end

Expand Down
2 changes: 1 addition & 1 deletion src/SOneTo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ end
Base.first(::SOneTo) = 1
Base.last(::SOneTo{n}) where {n} = n::Int

@pure function Base.iterate(::SOneTo{n}) where {n}
function Base.iterate(::SOneTo{n}) where {n}
if n::Int < 1
return nothing
else
Expand Down
6 changes: 3 additions & 3 deletions src/SUnitRange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct SUnitRange{Start, L} <: StaticVector{L, Int}
end
end

@pure function check_sunitrange_params(L::Int)
function check_sunitrange_params(L::Int)
if L < 0
error("Static unit range length is negative")
end
Expand All @@ -16,9 +16,9 @@ function check_sunitrange_params(L)
throw(TypeError(:SUnitRange, "type parameters must be `Int`", Tuple{Int,}, Tuple{typeof(L),}))
end

@pure SUnitRange(a::Int, b::Int) = SUnitRange{a, max(0, b - a + 1)}()
SUnitRange(a::Int, b::Int) = SUnitRange{a, max(0, b - a + 1)}()

@pure @propagate_inbounds function getindex(x::SUnitRange{Start, L}, i::Int) where {Start, L}
@propagate_inbounds function getindex(x::SUnitRange{Start, L}, i::Int) where {Start, L}
@boundscheck if i < 1 || i > L
throw(BoundsError(x, i))
end
Expand Down
10 changes: 5 additions & 5 deletions src/abstractarray.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
length(a::StaticArrayLike) = prod(Size(a))::Int
length(a::Type{SA}) where {SA <: StaticArrayLike} = prod(Size(SA))::Int

@pure size(::Type{SA}) where {SA <: StaticArrayLike} = Tuple(Size(SA))
size(::Type{SA}) where {SA <: StaticArrayLike} = Tuple(Size(SA))
@inline function size(t::Type{<:StaticArrayLike}, d::Int)
S = size(t)
d > length(S) ? 1 : S[d]
end
@inline size(a::StaticArrayLike) = Tuple(Size(a))

Base.axes(s::StaticArrayLike) = _axes(Size(s))
@pure function _axes(::Size{sizes}) where {sizes}
@generated function _axes(::Size{sizes}) where {sizes}
map(SOneTo, sizes)
end

Expand Down Expand Up @@ -83,9 +83,9 @@ sizedarray_similar_type(::Type{T},s::Size{S},::Type{Val{D}}) where {T,S,D} = Siz
# Utility for computing the eltype of an array instance, type, or type
# constructor. For type constructors without a definite eltype, the default
# value is returned.
Base.@pure _eltype_or(a::AbstractArray, default) = eltype(a)
Base.@pure _eltype_or(::Type{<:AbstractArray{T}}, default) where {T} = T
Base.@pure _eltype_or(::Type{<:AbstractArray}, default) = default # eltype not available
_eltype_or(a::AbstractArray, default) = eltype(a)
_eltype_or(::Type{<:AbstractArray{T}}, default) where {T} = T
_eltype_or(::Type{<:AbstractArray}, default) = default # eltype not available

"""
_construct_similar(a, ::Size, elements::NTuple)
Expand Down
10 changes: 5 additions & 5 deletions src/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ const BadArgs = Args{<:Tuple{Tuple{<:Tuple}}}

# Some help functions.
@pure has_ndims(::Type{<:StaticArray{<:Tuple,<:Any,N}}) where {N} = @isdefined N
@pure has_ndims(::Type{<:StaticArray}) = false
has_ndims(::Type{<:StaticArray}) = false
if VERSION < v"1.7"
Base.ndims(::Type{<:StaticArray{<:Tuple,<:Any,N}}) where {N} = N
end
@pure has_eltype(::Type{<:StaticArray{<:Tuple,T}}) where {T} = @isdefined T
@pure has_eltype(::Type{<:StaticArray}) = false
has_eltype(::Type{<:StaticArray}) = false
@pure has_size(::Type{<:StaticArray{S}}) where {S<:Tuple} = @isdefined S
@pure has_size(::Type{<:StaticArray}) = false
has_size(::Type{<:StaticArray}) = false
# workaround for https://github.com/JuliaArrays/StaticArrays.jl/issues/1047
has_size(::Type{SVector}) = false
has_size(::Type{MVector}) = false
Expand All @@ -28,10 +28,10 @@ has_size(::Type{SMatrix{N}}) where {N} = false
has_size(::Type{MMatrix{N}}) where {N} = false

@pure has_size1(::Type{<:StaticMatrix{M}}) where {M} = @isdefined M
@pure has_size1(::Type{<:StaticMatrix}) = false
has_size1(::Type{<:StaticMatrix}) = false
_size1(::Type{<:StaticMatrix{M}}) where {M} = M
@generated function _sqrt(::Length{L}) where {L}
N = round(Int, sqrt(L))
N = isqrt(L)
N^2 == L && return :($N)
throw(DimensionMismatch("Input's length must be perfect square"))
end
Expand Down
2 changes: 1 addition & 1 deletion src/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ end
## Indexing utilities ##
#########################

@pure unpack_size(::Type{Size{S}}) where {S} = map(Size, S)
@generated unpack_size(::Type{Size{S}}) where {S} = map(Size, S)

@inline index_size(::Size, ::Int) = Size()
@inline index_size(::Size, a::StaticArray) = Size(a)
Expand Down
8 changes: 4 additions & 4 deletions src/traits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@

Length(a::AbstractArray) = Length(Size(a))
Length(::Type{A}) where {A <: AbstractArray} = Length(Size(A))
@pure Length(L::Int) = Length{L}()
Length(L::Int) = Length{L}()
Length(::Size{S}) where {S} = _Length(S...)
_Length(S::Int...) = Length{prod(S)}()
@inline _Length(S...) = Length{Dynamic()}()

# Some convenience functions for `Size`
(::Type{Tuple})(::Size{S}) where {S} = S

@pure getindex(::Size{S}, i::Int) where {S} = i <= length(S) ? S[i] : 1
getindex(::Size{S}, i::Int) where {S} = i <= length(S) ? S[i] : 1

length(::Size{S}) where {S} = length(S)
length_val(::Size{S}) where {S} = Val{length(S)}
Expand All @@ -59,8 +59,8 @@

size_tuple(::Size{S}) where {S} = Tuple{S...}

# Some @pure convenience functions for `Length`
@pure (::Type{Int})(::Length{L}) where {L} = Int(L)
# Some convenience functions for `Length`
(::Type{Int})(::Length{L}) where {L} = Int(L)

Check warning on line 63 in src/traits.jl

View check run for this annotation

Codecov / codecov/patch

src/traits.jl#L63

Added line #L63 was not covered by tests

Base.:(==)(::Length{L}, l::Int) where {L} = L == l
Base.:(==)(l::Int, ::Length{L}) where {L} = l == L
Expand Down
Loading