Skip to content

Throw a different error for @inbounds [][-1] when --check-bounds=yes #60530

@LilithHafner

Description

@LilithHafner

A bounds error of the form x[out_of_bounds_index] = 0 is very different from a bounds error of the form @inbounds x[out_of_bounds_index] = 0. The former throws a bounds error and the latter is UB.

When --check-bounds=yes, I would like @inbounds x[out_of_bounds_index] = 0 to throw an UnsafeBoundsError or somesuch.

This will enable testing that no unsafe @inbounds usage is hit in a test while still allowing bounds errors. For example this currently passes with --check-bounds=yes and is UB by default:

struct OffsetVector <: AbstractVector{Int}
    x::Vector{Int}
    offset::Int
end
Base.axes(x::OffsetVector) = (x.offset:x.offset+length(x.x),)
function Base.getindex(x::OffsetVector, i::Int)
    checkbounds(x, i)
    @inbounds x.x[i-x.offset]
end

x = OffsetVector([1,2,3], 5);
@test_throws BoundsError x[5]
@test_throws BoundsError x[9]

I'd like x[5] to throw an UnsafeBoundsError error and fail the test.

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrays[a, r, r, a, y, s]error messagesBetter, more actionable error messagesfeatureIndicates new feature / enhancement requestsneeds pkgevalTests for all registered packages should be run with this changetestThis change adds or pertains to unit tests

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions