Skip to content

Commit ca2a3c3

Browse files
Jakob Nybo Andersenjakobnissen
authored andcommitted
Update to LightBoundsError
1 parent eed6315 commit ca2a3c3

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ not be mentioned here, because they do not impact how the package is to be used.
1515
* `Matrix` and other `Array` types with a different dimensionality than 1 is now
1616
`NotMemory`, since it is not equal to its own memory view, due to shape mismatch.
1717

18+
* Out of bounds access now throws a `LightBoundsError` from the LightBoundsErrors
19+
package, instead of `Base.BoundsError`.
20+
This improves codegen slightly, as it enables escape analysis of the array,
21+
and outlines error paths slightly more aggressively.
22+
1823
* `MemoryView(::SubArray)` now accepts fewer subarray types. However, it is unlikely
1924
that any instance that is now no longer accepted worked previously, so it is
2025
unlikely to be breaking in practice.

src/MemoryViews.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Create a mutable memory view from its parts.
110110
```jldoctest
111111
julia> v = [1,2,3,4];
112112
113-
julia> ref = Base.memoryref(v);
113+
julia> ref = Base.cconvert(Ptr, v);
114114
115115
julia> view = unsafe_from_parts(ref, 3)
116116
3-element MutableMemoryView{Int64}:

src/basic.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ end
410410
411411
Return the first element of `v` and all other elements as a new memory view.
412412
413-
This function will throw a `BoundsError` if `v` is empty.
413+
This function will throw a `LightBoundsError` if `v` is empty.
414414
415415
See also: [`split_last`](@ref)
416416
@@ -425,7 +425,7 @@ julia> split_first(v[1:1])
425425
(0x01, UInt8[])
426426
427427
julia> split_first(v[1:0])
428-
ERROR: BoundsError: attempt to access 0-element MutableMemoryView{UInt8} at index [1]
428+
ERROR: LightBoundsErrors.LightBoundsError: out-of-bounds indexing: `collection[1]`, where:
429429
[...]
430430
```
431431
"""
@@ -439,7 +439,7 @@ end
439439
440440
Return the last element of `v` and all other elements as a new memory view.
441441
442-
This function will throw a `BoundsError` if `v` is empty.
442+
This function will throw a `LightBoundsError` if `v` is empty.
443443
444444
See also: [`split_first`](@ref)
445445
@@ -454,7 +454,7 @@ julia> split_last(v[1:1])
454454
(0x01, UInt8[])
455455
456456
julia> split_last(v[1:0])
457-
ERROR: BoundsError: attempt to access 0-element MutableMemoryView{UInt8} at index [1]
457+
ERROR: LightBoundsErrors.LightBoundsError: out-of-bounds indexing: `collection[1]`, where:
458458
[...]
459459
```
460460
"""
@@ -469,10 +469,10 @@ end
469469
Split a memory view into two at an index.
470470
471471
The first will contain all indices in `1:i-1`, the second `i:end`.
472-
This function will throw a `BoundsError` if `i` is not in `1:end+1`.
472+
This function will throw a `LightBoundsError` if `i` is not in `1:end+1`.
473473
474474
# Examples
475-
```jldocstest
475+
```jldoctest
476476
julia> split_at(MemoryView([1,2,3,4,5]), 2)
477477
([1], [2, 3, 4, 5])
478478
@@ -481,7 +481,9 @@ julia> split_at(MemoryView(Int8[1, 2, 3]), 4)
481481
```
482482
"""
483483
function split_at(v::MemoryView, i::Int)
484-
@boundscheck checkbounds(1:(lastindex(v) + 1), i)
484+
@boundscheck if i 1:(lastindex(v) + 1)
485+
throw_lightboundserror(v, i)
486+
end
485487
return (@inbounds(truncate(v, i - 1)), @inbounds(truncate_start(v, i)))
486488
end
487489

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,8 @@ end
485485
@test split_at(mem, 2) == (mem[1:1], mem[2:end])
486486
@test split_at(mem, lastindex(mem)) == (mem[1:(end - 1)], mem[end:end])
487487
@test split_at(mem, lastindex(mem) + 1) == (mem[1:end], mem[1:0])
488+
@test_throws LightBoundsError split_at(mem, 0)
489+
@test_throws LightBoundsError split_at(mem, lastindex(mem) + 2)
488490
mem = mem[2:2]
489491
@test split_first(mem) == (mem[1], mem[2:end])
490492
@test split_last(mem) == (mem[end], mem[1:(end - 1)])
@@ -498,6 +500,8 @@ end
498500
(v1, v2) = split_at(mem, 1)
499501
@test v1 == v2
500502
@test isempty(v1)
503+
@test_throws LightBoundsError split_at(mem, 0)
504+
@test_throws LightBoundsError split_at(mem, 2)
501505
end
502506

503507
@testset "Split unaligned" begin

0 commit comments

Comments
 (0)