Repository: numba/numba

Issue:
LoweringError iterating over 2D array @numba.jit(nopython=True)
#### # FAILS: **This should not have happened**, a problem has occurred in Numba's internals.  You are currently using Numba version 0.45.1...LoweringError: Failed in nopython mode pipeline (step: nopython mode backend) iterating over 2D array...**Please report**...
a=numpy.array([[0, 1],[1, 3],[2, 2]])
@numba.jit(nopython=True)
def f(a):
..for i in a:
....print(i)
f(a)

#### # RUNS WITH A WARNING: Compilation is falling back to object mode WITH looplifting enabled because Function f failed at nopython mode lowering due to: iterating over 2D array
a=numpy.array([[0, 1],[1, 3],[2, 2]])
@numba.jit()
def f(a):
.. for i in a:
....print(i)
f(a)

### I figured out that the way to do what I need:
a=numpy.array([[0, 1],[1, 3],[2, 2]])
@numba.jit(nopython=True, nogil=True, cache=True)
def f(a):
..lst=[]
..for i in range(0,len(a)):
....lst.append(a[i,1])
..return lst(a)
f(a)
### filing the bug because is was requested by @numba.jit error message


Discussion:

Resolving patch:
diff --git a/numba/typing/builtins.py b/numba/typing/builtins.py
--- a/numba/typing/builtins.py
+++ b/numba/typing/builtins.py
@@ -88,7 +88,16 @@ def generic(self, args, kws):
         assert not kws
         [obj] = args
         if isinstance(obj, types.IterableType):
-            return signature(obj.iterator_type, obj)
+            # Raise this here to provide a very specific message about this
+            # common issue, delaying the error until later leads to something
+            # less specific being noted as the problem (e.g. no support for
+            # getiter on array(<>, 2, 'C')).
+            if isinstance(obj, types.Array) and obj.ndim > 1:
+                msg = ("Direct iteration is not supported for arrays with "
+                       "dimension > 1. Try using indexing instead.")
+                raise errors.TypingError(msg)
+            else:
+                return signature(obj.iterator_type, obj)
 
 
 @infer


Test patch:
diff --git a/numba/tests/test_iteration.py b/numba/tests/test_iteration.py
--- a/numba/tests/test_iteration.py
+++ b/numba/tests/test_iteration.py
@@ -4,7 +4,7 @@
 
 import numba.unittest_support as unittest
 from numba.compiler import compile_isolated, Flags
-from numba import numpy_support, types
+from numba import numpy_support, types, errors
 from .support import TestCase, MemoryLeakMixin, tag
 
 enable_pyobj_flags = Flags()
@@ -193,6 +193,24 @@ def test_array_1d_record_mutate_npm(self, flags=no_pyobj_flags):
     def test_array_1d_record_mutate(self):
         self.test_array_1d_record_mutate_npm(flags=force_pyobj_flags)
 
+    def test_array_2d_raises(self):
+
+        def foo(x):
+            for i in x:
+                pass
+
+        # 1D is fine
+        aryty = types.Array(types.int32, 1, 'C')
+        compile_isolated(foo, (aryty,))
+
+        # 2d is typing error
+        with self.assertRaises(errors.TypingError) as raises:
+            aryty = types.Array(types.int32, 2, 'C')
+            compile_isolated(foo, (aryty,))
+
+        self.assertIn("Direct iteration is not supported",
+                      str(raises.exception))
+
     def test_tuple_iter_issue1504(self):
         # The issue is due to `row` being typed as heterogeneous tuple.
         def bar(x, y):