Why do unsafe operations check bounds for `Memory`?

I’ve noticed that some “unsafe” operations for Memory actually do check bounds:

julia> v = Memory{Int}(1:3)
3-element Memory{Int64}:
 1
 2
 3

julia> unsafe_copyto!(v, 1, v, 100, 3)
ERROR: BoundsError: attempt to access 3-element Memory{Int64} at index [100]
[...]

julia> g(v, i) = @inbounds v[i]
g (generic function with 1 method)

julia> g(v, 1:100)
ERROR: BoundsError: attempt to access 3-element Memory{Int64} at index [1:100]
[...]

Is this intended? If not (and if I find the time), I might prepare a PR to fix this.

edit: Oops, I failed at reading comprehension for the issue.

On what version do you observe this? I don’t see that, a la

julia> m=[1].ref.mem
1-element Memory{Int64}:
 1

julia> g(v,i)=@inbounds v[i]
g (generic function with 1 method)

julia> g(m,2)
140352011457840

tested on both 1.13.4 and 1.15.0-DEV.971 with x86_64-linux-gnu and default --check-bounds=auto.

I observe it on all version from 1.11 to nightly. Note that in my first post I called g with a range as second argument. With an integer there is indeed no bounds check.

It seems getindex isn’t forwarding the inbounds to copyto!, which runs bounds checks again.

see: inbounds propagation mostly missing in base/genericmemory.jl? · Issue #56145 · JuliaLang/julia · GitHub propagate inbounds in a few more places for memoryref - Pull Request #56151 - JuliaLang/julia - GitHub and probably some other refs

We kept the checks cause they are functionally free in these operations (they usually lower to memcpy/memset) so there’s not much reason to take them out.
The overhead of a bounds check is really only visible if it affects automatic vectorization