Skip to content

Inbounds not propagated when using StaticArrays #220

@lcw

Description

@lcw

I am having an issue with inbounds propagation with the following code.

using StructArrays
using StaticArrays

u = StructArray(zeros(SVector{2,Float64}, 1))
function f!(u)
    a = -zero(MVector{2,Float64})
    @inbounds u[1] = a
end

c = sprint((io, args...) -> code_warntype(io, args...; optimize=true), f!, Tuple{typeof(u)})
occursin("boundserror", c)

The last line returns true even though we have used @inbounds. It turns out the bounds checking is coming from accessing the components of a. I can fix the issue with the following patch.

diff --git a/src/staticarrays_support.jl b/src/staticarrays_support.jl
index ce1875b..4f76085 100644
--- a/src/staticarrays_support.jl
+++ b/src/staticarrays_support.jl
@@ -19,7 +19,7 @@ which subtypes `FieldArray`.
     end
 end
 StructArrays.createinstance(::Type{T}, args...) where {T<:StaticArray} = T(args)
-StructArrays.component(s::StaticArray, i) = getindex(s, i)
+StructArrays.component(s::StaticArray, i) = @inbounds getindex(s, i)
 
 # invoke general fallbacks for a `FieldArray` type.
 @inline function StructArrays.staticschema(T::Type{<:FieldArray})

Is this an okay approach? Or do we need to propagate inbounds all the way to the call to component?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions