Skip to content

MemoryRef bounds errors can be confusing #57184

Open
@LilithHafner

Description

@LilithHafner

Because of how MemoryRefs can represent views and because the MemoryRef itself is not captured in BoundsErrors, we can get confusing errors like this:

julia> x = Memory{Int}([1,2,3,4])
4-element Memory{Int64}:
 1
 2
 3
 4

julia> copyto!(x, 3, x, 3, 3)
ERROR: BoundsError: attempt to access MemoryRef{Int64} at index [3]
Stacktrace:
 [1] memoryref
   @ ./boot.jl:524 [inlined]
 [2] unsafe_copyto!
   @ ./genericmemory.jl:115 [inlined]
 [3] unsafe_copyto!
   @ ./genericmemory.jl:139 [inlined]
 [4] copyto!(dest::Memory{Int64}, doffs::Int64, src::Memory{Int64}, soffs::Int64, n::Int64)
   @ Base ./genericmemory.jl:175
 [5] top-level scope
   @ REPL[1]:1

This example is confusing because one could think that the memory has length 4 so accessing it at index 3 should be fine. What's happening is that a MemoryRef is constructed at an offset and then indexed out of bounds at index 3 when the new MemoryRef only has two indices.

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrays[a, r, r, a, y, s]display and printingAesthetics and correctness of printed representations of objects.error messagesBetter, more actionable error messagesgood first issueIndicates a good issue for first-time contributors to Juliahelp wantedIndicates that a maintainer wants help on an issue or pull request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions