From fd47846c69878520eae56ea149ccec76dbc7ac02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= <bkamins@sgh.waw.pl> Date: Tue, 9 Mar 2021 10:46:13 +0100 Subject: [PATCH 1/2] Allow CartesianIndices with Bool argument Follow up to the problem discussed in https://github.com/JuliaLang/julia/pull/31829#issuecomment-793030999. I came to the conclusion that `CartesianIndices((true,))` should be allowed as in this context `true` represents a dimension length not an index. --- base/multidimensional.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/base/multidimensional.jl b/base/multidimensional.jl index 32d66c4c54cda..9acb97a486fbb 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -278,6 +278,7 @@ module IteratorsMD CartesianIndices(A::AbstractArray) = CartesianIndices(axes(A)) + _convert2ind(sz::Bool) = Base.OneTo(Int(sz)) _convert2ind(sz::Integer) = Base.OneTo(sz) _convert2ind(sz::AbstractUnitRange) = first(sz):last(sz) _convert2ind(sz::OrdinalRange) = first(sz):step(sz):last(sz) From 8fec5cecc8ecd3bd8398ad6eb5c5254a994c86db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= <bkamins@sgh.waw.pl> Date: Tue, 9 Mar 2021 10:49:17 +0100 Subject: [PATCH 2/2] add tests --- test/cartesian.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/cartesian.jl b/test/cartesian.jl index af0bc466e3d04..8d2651b6f425f 100644 --- a/test/cartesian.jl +++ b/test/cartesian.jl @@ -409,3 +409,10 @@ end # issue #39705 f39705() = Base.Cartesian.@nany 0 _ -> true @test f39705() === false + +@testset "CartesianIndices with Bool" begin + @test @inferred(CartesianIndices((true,))) == CartesianIndices((1,)) + @test @inferred(CartesianIndices((false,))) == CartesianIndices((0,)) + @test @inferred(CartesianIndices((true, false))) == CartesianIndices((1, 0)) + @test @inferred(CartesianIndices((false, true))) == CartesianIndices((0, 1)) +end