diff --git a/src/ZeroDimensionalArrays.jl b/src/ZeroDimensionalArrays.jl index cd7a3f1..0741d5c 100644 --- a/src/ZeroDimensionalArrays.jl +++ b/src/ZeroDimensionalArrays.jl @@ -63,6 +63,15 @@ function Base.getindex(a::ZeroDimensionalArray) a.v end +# This method is redundant for correctness, but adding it helps achieve constprop, which +# helps `only(::ZeroDimensionalArray)` and `last(::ZeroDimensionalArray)`, for example. +Base.@constprop :aggressive function Base.getindex(a::ZeroDimensionalArray, i::Int) + if !isone(i) + throw(BoundsError()) + end + a[] +end + function Base.setindex!(a::Box, x) a.v = x end @@ -71,14 +80,6 @@ Base.@nospecializeinfer function Base.isassigned((@nospecialize unused::ZeroDime all(isone, i) end -function Base.only(a::ZeroDimensionalArray) - a[] -end - -function Base.last(a::ZeroDimensionalArray) - a[] -end - function Base.iterate(a::ZeroDimensionalArray) (a[], nothing) end diff --git a/test/runtests.jl b/test/runtests.jl index 31485f8..4e3911b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -45,6 +45,10 @@ using Aqua: Aqua @test Arr(x) == Arr(x) @test all(@inferred Arr(x) .== Arr(x)) @test (@inferred Arr(x) .+ [10, 20]) isa AbstractVector + let oob_exception_type = Exception + @test_throws oob_exception_type Arr(x)[0] + @test_throws oob_exception_type Arr(x)[2] + end end end