From 0edc5185a65676bfa1048f08a43c552efeb5ca08 Mon Sep 17 00:00:00 2001 From: Neven Sajko Date: Thu, 18 Sep 2025 15:08:17 +0200 Subject: [PATCH 1/2] add a method to `getindex` instead of to `only` and `last` --- src/ZeroDimensionalArrays.jl | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) 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 From e2c7fa14927567c6f64da513fd27f02f906ca5a8 Mon Sep 17 00:00:00 2001 From: Neven Sajko Date: Thu, 18 Sep 2025 20:40:39 +0200 Subject: [PATCH 2/2] add tests for better coverage --- test/runtests.jl | 4 ++++ 1 file changed, 4 insertions(+) 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