Inbounds still gives me a bounds error

indexing

#1

Why does this code:

T = [1,2,3]
@inbounds x = T[4] 

gives me an error:

ERROR: LoadError: BoundsError: attempt to access 3-element Array{Int64,1} at index [4]
Stacktrace:
 [1] getindex(::Array{Int64,1}, ::Int64) at ./array.jl:554
 [2] include_string(::String, ::String) at ./loading.jl:522
 [3] include_string(::String, ::String, ::Int64) at /home/gluon/.julia/v0.6/CodeTools/src/eval.jl:30
 [4] include_string(::Module, ::String, ::String, ::Int64, ::Vararg{Int64,N} where N) at /home/gluon/.julia/v0.6/CodeTools/src/eval.jl:34
 [5] (::Atom.##92#97{String,Int64,String})() at /home/gluon/.julia/v0.6/Atom/src/eval.jl:52
 [6] withpath(::Atom.##92#97{String,Int64,String}, ::String) at /home/gluon/.julia/v0.6/CodeTools/src/utils.jl:30
 [7] withpath(::Function, ::String) at /home/gluon/.julia/v0.6/Atom/src/eval.jl:38
 [8] hideprompt(::Atom.##91#96{String,Int64,String}) at /home/gluon/.julia/v0.6/Atom/src/repl.jl:67
 [9] macro expansion at /home/gluon/.julia/v0.6/Atom/src/eval.jl:49 [inlined]
 [10] (::Atom.##90#95{Dict{String,Any}})() at ./task.jl:80
while loading /media/DATA/PhD/code/ising/ising_julia_code/Lattice2.jl, in expression starting on line 24

And just in case, is it possible to use @inboudns and @view in a sum call, like e. g.:

sum(@inbounds @view A[1:end, 2])

#2

Looks like this only works inside a function (should be documented though, can you make a PR?):

julia> function f()
       T= [1,2,3]
       @inbounds x = T[4]
       x
       end                                                                                                                                                              
f (generic function with 1 method)                                                                                                                                      
                                                                                                                                                                        
julia> f()                                                                                                                                                              
140194507264240                 # garbage                                                                                                                                        

#3

@mbauman touched upon this here https://stackoverflow.com/questions/38901275/inbounds-propagation-rules-in-julia .


#4

Do you mean on GitHub?


#5

This

T = [1,2,3]
function f()
	@inbounds x = T[4] 
end
f()

gives the same error.


#6

Doing const T=[1,2,3] works.


#7

Read the last paragraph of the stackoverflow post linked above.


#8

Oh yeah, thanks. Was too sloppy in reading the article.


#9

Please note that accessing an out-of-bounds index with an @inbounds declaration is undefined behavior and may cause any arbitrary behavior, including launching nukes, throwing arbitrary non-sensical errors, returning garbage, crashing or calling your parents to complain that you’ve been bad.


#10

It should all be documented here: https://docs.julialang.org/en/latest/devdocs/boundscheck/

As usual, though, those reading the docs for the first time are often the ones in the best place to suggest improvements!


#11

Maybe a qualification in the doc-string would be good. Currently it says

help?> @inbounds                                                                                                                                                         
  @inbounds(blk)                                                                                                                                                         

  Eliminates array bounds checking within expressions.

which suggests to me that the bounds checks are indeed always eliminated.

Something like:

help?> @inbounds                                                                                                                                                         
  @inbounds(blk)                                                                                                                                                         

  Eliminates array bounds checking within expressions if possible.

#12

That’s a great idea — the docs I linked are indeed devdocs. We could definitely do for a bit more information in the user-facing docstring.


#13

Suggested wording:

Grant the compiler permission to skip bounds checks within the given expression by simply assuming that all indexing is in bounds. If the given index is not actually in bounds your program may crash or produce complete garbage. Elimination of bounds checks is not guaranteed by the @inbounds annotation, merely permitted.


#14

Let’s throw a breadcrumb in there to --check-bounds=yes. Otherwise that looks great.


#15

And an example! The docstring for @boundscheck have a good one that should be applicable here too.