help bounds checking to be eliminated for `getindex(::Memory, ::Int)` (#58754)
Second try for PR #58741.
This moves the `getindex(::Memory, ::Int)` bounds check to Julia, which
is how it's already done for `getindex(::Array, ::Int)`, so I guess it's
correct.
Also deduplicate the bounds checking code while at it.