Repository: numba/numba

Issue:
writing to a list
```
Traceback (most recent call last):
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/targets/base.py", line 499, in get_constant_generic
    impl = self._get_constants.find((ty,))
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/targets/base.py", line 50, in find
    out = self._find(sig)
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/targets/base.py", line 59, in _find
    raise NotImplementedError(self, sig)
NotImplementedError: (<numba.targets.base.OverloadSelector object at 0x116394c50>, (reflected list(int64),))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/errors.py", line 627, in new_error_context
    yield
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/lowering.py", line 258, in lower_block
    self.lower_inst(inst)
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/lowering.py", line 301, in lower_inst
    val = self.lower_assign(ty, inst)
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/lowering.py", line 449, in lower_assign
    value.value)
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/targets/base.py", line 502, in get_constant_generic
    raise NotImplementedError("Cannot lower constant of type '%s'" % (ty,))
NotImplementedError: Cannot lower constant of type 'reflected list(int64)'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/david/work/Datascience/bestQuestion/scratch.py", line 11, in <module>
    main()
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/dispatcher.py", line 369, in _compile_for_args
    raise e
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/dispatcher.py", line 326, in _compile_for_args
    return self.compile(tuple(argtypes))
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
    return func(*args, **kwargs)
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/dispatcher.py", line 658, in compile
    cres = self._compiler.compile(args, return_type)
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/dispatcher.py", line 82, in compile
    pipeline_class=self.pipeline_class)
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/compiler.py", line 941, in compile_extra
    return pipeline.compile_extra(func)
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/compiler.py", line 372, in compile_extra
    return self._compile_bytecode()
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/compiler.py", line 872, in _compile_bytecode
    return self._compile_core()
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/compiler.py", line 859, in _compile_core
    res = pm.run(self.status)
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
    return func(*args, **kwargs)
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/compiler.py", line 253, in run
    raise patched_exception
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/compiler.py", line 244, in run
    stage()
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/compiler.py", line 731, in stage_nopython_backend
    self._backend(lowerfn, objectmode=False)
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/compiler.py", line 681, in _backend
    lowered = lowerfn()
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/compiler.py", line 668, in backend_nopython_mode
    self.metadata)
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/compiler.py", line 1063, in native_lowering_stage
    lower.lower()
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/lowering.py", line 177, in lower
    self.lower_normal_function(self.fndesc)
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/lowering.py", line 218, in lower_normal_function
    entry_block_tail = self.lower_function_body()
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/lowering.py", line 243, in lower_function_body
    self.lower_block(block)
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/lowering.py", line 258, in lower_block
    self.lower_inst(inst)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/errors.py", line 635, in new_error_context
    six.reraise(type(newerr), newerr, tb)
  File "/Users/david/work/Datascience/venv/lib/python3.7/site-packages/numba/six.py", line 659, in reraise
    raise value
numba.errors.LoweringError: Failed in nopython mode pipeline (step: nopython mode backend)
Cannot lower constant of type 'reflected list(int64)'

File "scratch.py", line 8:
def main():
    <source elided>
    for i in range(5):
        a[i]=i
        ^

[1] During: lowering "$16.3 = global(a: [0, 0, 0, 0, 0])" at /Users/david/work/Datascience/bestQuestion/scratch.py (8)
-------------------------------------------------------------------------------
This should not have happened, a problem has occurred in Numba's internals.
```

reproducer
```
from numba import jit
a = [0]*5
@jit(nopython=True)
def main():
    total = 0
    for i in range(5):
        a[i]=i
if __name__ == '__main__':
    main()
    print(a)
```





Discussion:
@DigiKaschub thanks for using Numba and reporting this error. I have updated your comment with syntax highlighting to increase legibility.
This currently doesn't work as the list `a` is defined in the modules global context. Here is a workaround:

```
from numba import jit


@jit(nopython=True)
def main():
    a = [0] * 5
    for i in range(5):
        a[i] = i
    return a


if __name__ == '__main__':
    a = main()
    print(a)
```

And here is another, in case you need to define and populate the list before the function call:

```
from numba import jit


@jit(nopython=True)
def main(a):
    for i in range(5):
        a[i] = i


if __name__ == '__main__':
    a = [0] * 5
    main(a)
    print(a)
```
Thanks for the help!

Resolving patch:
diff --git a/numba/ir_utils.py b/numba/ir_utils.py
--- a/numba/ir_utils.py
+++ b/numba/ir_utils.py
@@ -1891,6 +1891,15 @@ def raise_on_unsupported_feature(func_ir, typemap):
                         "try wrapping the variable {}with 'np.<dtype>()'".
                         format(vardescr), loc=stmt.loc)
 
+            # checks for globals that are also reflected
+            if isinstance(stmt.value, ir.Global):
+                ty = typemap[stmt.target.name]
+                if getattr(ty, 'reflected', False):
+                    msg = ("Writing to a %s defined in globals is not "
+                           "supported as globals are considered compile-time "
+                           "constants.")
+                    raise TypingError(msg % ty, loc=stmt.loc)
+
     # There is more than one call to function gdb/gdb_init
     if len(gdb_calls) > 1:
         msg = ("Calling either numba.gdb() or numba.gdb_init() more than once "


Test patch:
diff --git a/numba/tests/test_errorhandling.py b/numba/tests/test_errorhandling.py
--- a/numba/tests/test_errorhandling.py
+++ b/numba/tests/test_errorhandling.py
@@ -8,6 +8,8 @@
 from numba import errors, utils
 import numpy as np
 
+# used in TestMiscErrorHandling::test_handling_of_write_to_global
+_global_list = [1, 2, 3, 4]
 
 class TestErrorHandlingBeforeLowering(unittest.TestCase):
 
@@ -106,6 +108,18 @@ def f(a):
         expected = 'File "unknown location", line 0:'
         self.assertIn(expected, str(raises.exception))
 
+    def test_handling_of_write_to_global(self):
+        @njit
+        def foo():
+            _global_list[0] = 10
+
+        with self.assertRaises(errors.TypingError) as raises:
+            foo()
+
+        expected = ["Writing to a", "defined in globals is not supported"]
+        for ex in expected:
+            self.assertIn(ex, str(raises.exception))
+
 
 class TestConstantInferenceErrorHandling(unittest.TestCase):