Skip to content

example use of @inbounds in the @inbounds documentation uses it incorrectly #39367

@yurivish

Description

@yurivish

It appears that the example use of @inbounds given in the documentation for @inbounds is not a safe use of @inbounds:

image

The paragraph above the example implies that it is meant as an example to be followed, not one to be avoided. However, the warning immediately below the example strongly recommends only using @inbounds when it is certain from the information locally available that all accesses are in bounds.

This is not the case in the example, whose method signature explicitly allows all AbstractArrays, including those which do not use one-based indexing.

I verified that the example silently produces incorrect results due to out-of-bounds memory access on Julia 1.5.3:

julia> function sum(A::AbstractArray)
           r = zero(eltype(A))
           for i = 1:length(A)
               @inbounds r += A[i]
           end
           return r
       end
sum (generic function with 1 method)

julia> using OffsetArrays

julia> sum(OffsetArray(collect(1:11), -5:5))
8858289599

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsThis change adds or pertains to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions