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

Give a better error than BoundsError in multiple assignment #7978

Closed
quinnj opened this issue Aug 12, 2014 · 4 comments
Closed

Give a better error than BoundsError in multiple assignment #7978

quinnj opened this issue Aug 12, 2014 · 4 comments
Labels
error handling Handling of exceptions by Julia or the user

Comments

@quinnj
Copy link
Member

quinnj commented Aug 12, 2014

Seems like we could give a better error here:

In  [65]: a,b = 1,2

Out [65]: (1,2)

In  [66]: a,b = 1,2,3

Out [66]: (1,2,3)

In  [67]: a,b,c = 1,2

Out [67]: ERROR: BoundsError()
while loading In[67], in expression starting on line 1

This example is trivial, but a use case that was much more frustrating was along the lines of

julia> a,b,c = arrayA[x], methodB(y)

Out [67]: ERROR: BoundsError()
while loading In[67], in expression starting on line 1

In which case you spend too much time checking if x is inbounds, and then if methodB is somehow out of bounds, etc.

@tknopp
Copy link
Contributor

tknopp commented Aug 12, 2014

see #4744 and #2144

I would love if in the case of an BoundsError it would be reported:

  • The size of the underlying array / tuple ...
  • The index at which the access has failed.

Debugging BoundsErrors is really tricky at the moment.

@JeffBezanson JeffBezanson changed the title Give a better error than BoundsError Give a better error than BoundsError in multiple assignment Aug 14, 2014
@quinnj
Copy link
Member Author

quinnj commented Feb 8, 2015

The output for this is now

In  [3]: a,b,c = 1,2

Out [3]: ERROR: LoadError: BoundsError: attempt to access (1,2)
  at index [3]
while loading In[19], in expression starting on line 1

 in indexed_next at no file

which is much more helpful. Thanks @vtjnash!

@quinnj quinnj closed this as completed Feb 8, 2015
@ssfrr
Copy link
Contributor

ssfrr commented Sep 26, 2017

This has maybe regressed? In 0.6:

julia> foo() = 1
foo (generic function with 1 method)

julia> x, y = foo()
ERROR: BoundsError
Stacktrace:
 [1] indexed_next(::Int64, ::Int64, ::Bool) at ./tuple.jl:56

@vtjnash
Copy link
Member

vtjnash commented Sep 26, 2017

Doesn't look like that function was ever fixed. This patch fixes that case, but needs tests.

diff --git a/base/tuple.jl b/base/tuple.jl
index 1ffc85f9c4..b08da0ca75 100644
--- a/base/tuple.jl
+++ b/base/tuple.jl
@@ -57,7 +57,7 @@ end
 # while reducing to plain next() for arbitrary iterables.
 indexed_next(t::Tuple, i::Int, state) = (t[i], i+1)
 indexed_next(a::Array, i::Int, state) = (a[i], i+1)
-indexed_next(I, i, state) = done(I,state) ? throw(BoundsError()) : next(I, state)
+indexed_next(I, i, state) = done(I, state) ? throw(BoundsError(I, state)) : next(I, state)
 
 # Use dispatch to avoid a branch in first
 first(::Tuple{}) = throw(ArgumentError("tuple must be non-empty"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error handling Handling of exceptions by Julia or the user
Projects
None yet
Development

No branches or pull requests

5 participants