Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to index with non-concrete Array{CartesianIndex} #41242

Open
phipsgabler opened this issue Jun 16, 2021 · 1 comment
Open

Unable to index with non-concrete Array{CartesianIndex} #41242

phipsgabler opened this issue Jun 16, 2021 · 1 comment
Labels
docs This change adds or pertains to documentation error messages Better, more actionable error messages

Comments

@phipsgabler
Copy link
Contributor

I noticed a weird error when using abstractly typed arrays of CartesianIndex:

julia> graph = rand(3,3)
3×3 Array{Float64,2}:
 0.596119  0.509642  0.0507867
 0.573233  0.895749  0.972909
 0.410628  0.37905   0.0779637

julia> graph[[CartesianIndex(1,1)]]
1-element Array{Float64,1}:
 0.5961186454040548

julia> graph[CartesianIndex[CartesianIndex(1,1)]]
ERROR: ArgumentError: unable to check bounds for indices of type CartesianIndex{2}
Stacktrace:
 [1] checkindex(::Type{Bool}, ::Base.OneTo{Int64}, ::CartesianIndex{2}) at ./abstractarray.jl:561
 [2] checkindex at ./abstractarray.jl:576 [inlined]
 [3] checkbounds_indices at ./abstractarray.jl:532 [inlined]
 [4] checkbounds at ./multidimensional.jl:544 [inlined]
 [5] checkbounds at ./abstractarray.jl:506 [inlined]
 [6] _getindex at ./multidimensional.jl:742 [inlined]
 [7] getindex(::Array{Float64,2}, ::Array{CartesianIndex,1}) at ./abstractarray.jl:1060
 [8] top-level scope at REPL[155]:1

julia> graph[CartesianIndex{2}[CartesianIndex(1,1)]]
1-element Array{Float64,1}:
 0.5961186454040548

julia> versioninfo()
Julia Version 1.5.4
Commit 69fcb5745b (2021-03-11 19:13 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, skylake)

This has previously been reported on Discourse: https://discourse.julialang.org/t/surprising-type-error/51943.

@mbauman
Copy link
Member

mbauman commented Jun 16, 2021

I fear there's not much we can do here but potentially add this to the https://docs.julialang.org/en/v1/manual/arrays/#Cartesian-indices warning and/or improve the error message. The bounds checking infrastructure requires knowing which dimension(s) a particular index indexes into. We've talked some about it here: #35681 (comment)... and I thought there was a more direct issue about this, but I can't find it.

It's kinda like logical indexing with arrays of Bool, but there we explicitly disallow indexing by scalar Bool to help separate its special functionality... and of course that's not something we can do with scalar CartesianIndex.

@mbauman mbauman added error messages Better, more actionable error messages docs This change adds or pertains to documentation labels Jun 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation error messages Better, more actionable error messages
Projects
None yet
Development

No branches or pull requests

2 participants