Repository: numba/numba

Issue:
Error message needs improvement
The error message when running this code:
``` python
from numba import jit
import numba
import numpy

f = numpy.zeros(shape=(3,3,3))

@jit(nopython=True)
def explorethisx(inar, x):
    places = []
    places.append((x,0))
    x1,y1 = x,0
    s = numba.int64(0)
    co = 0
    #for _ in range( 799):

    while co != numba.int16(799):
        co += 1
        a1,a2,a3 = 999,999,999
        a1 = inar[y1 + 1][x1-1][1]
        a2 = inar[y1 + 1][x1][1]
        a3 = inar[y1 + 1][x1 + 1][1]
        m = a1
        ch = -1
        if m > a2:
            m = a2
            ch = 0
        if m > a3:
            m = a3
            ch = 1
        x1 = x1 + ch
        y1 = y1 + 1
        s += inar[y1][x1][1]
        places.append((x1,y1))
    return [s, places]

print explorethisx(f,3)
```
does not indicate what line number the error is happening on, or what the problem is:
```
Traceback (most recent call last):
  File "test.py", line 35, in <module>
    print explorethisx(f,3)
  File "/Users/stan/anaconda/lib/python2.7/site-packages/numba/dispatcher.py", line 309, in _compile_for_args
    raise e
numba.errors.TypingError: Caused By:
Traceback (most recent call last):
  File "/Users/stan/anaconda/lib/python2.7/site-packages/numba/compiler.py", line 228, in run
    stage()
  File "/Users/stan/anaconda/lib/python2.7/site-packages/numba/compiler.py", line 442, in stage_nopython_frontend
    self.locals)
  File "/Users/stan/anaconda/lib/python2.7/site-packages/numba/compiler.py", line 795, in type_inference_stage
    typemap, restype, calltypes = infer.unify()
  File "/Users/stan/anaconda/lib/python2.7/site-packages/numba/typeinfer.py", line 819, in unify
    check_var(var)
  File "/Users/stan/anaconda/lib/python2.7/site-packages/numba/typeinfer.py", line 806, in check_var
    raise TypingError("Undefined variable '%s'" % (var,))
TypingError: Undefined variable '$313.3'

Failed at nopython (nopython frontend)
Undefined variable '$313.3'
```


Discussion:
An identical issue has been posted as [question on StackOverflow](http://stackoverflow.com/q/41558683/5393381) today.

Just in case you're wondering what caused this: It's because of the shape-mismatch inside the `return [s, places]`.

A minimal example to reproduce the issue is

```
from numba import jit

@jit(nopython=True)
def test():
    return [1, [1,2]]

test()
```

Resolving patch:
diff --git a/numba/ir.py b/numba/ir.py
--- a/numba/ir.py
+++ b/numba/ir.py
@@ -754,6 +754,16 @@ def find_insts(self, cls=None):
             if isinstance(inst, cls):
                 yield inst
 
+    def find_variable_assignment(self, name):
+        """
+        Returns the assignment inst associated with variable "name", None if
+        it cannot be found.
+        """
+        for x in self.find_insts(cls=Assign):
+            if x.target.name == name:
+                return x
+        return None
+
     def prepend(self, inst):
         assert isinstance(inst, Stmt)
         self.body.insert(0, inst)
diff --git a/numba/typeinfer.py b/numba/typeinfer.py
--- a/numba/typeinfer.py
+++ b/numba/typeinfer.py
@@ -882,7 +882,15 @@ def unify(self):
         def check_var(name):
             tv = self.typevars[name]
             if not tv.defined:
-                raise TypingError("Undefined variable '%s'" % (var,))
+                offender = None
+                for block in self.func_ir.blocks.values():
+                    offender = block.find_variable_assignment(name)
+                    if offender is not None:
+                        break
+                val = getattr(offender, 'value', 'unknown operation')
+                loc = getattr(offender, 'loc', 'unknown location')
+                msg = "Undefined variable '%s', operation: %s, location: %s"
+                raise TypingError(msg % (var, val, loc))
             tp = tv.getone()
             if not tp.is_precise():
                 raise TypingError("Can't infer type of variable '%s': %s" % (var, tp))


Test patch: