Repository: numba/numba

Issue:
Numba disregards the first element of the output list, using simple python range
This is my first try of using numba. Strange that it has such profound bugs in parsing simple python code.

## Reporting a bug

```python
@numba.njit()
def compute(n=10):
    res = [0.]
    for i in range(n):
        res.append(i)
    return np.array(res, dtype=np.float32)
```

With the `numba.njit` decorator the output is `[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]`.
Without the decorator the (correct) output is `[0. 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]`


- [x] I have tried using the latest released version of Numba (most recent is
 visible in the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
- [x] I have included below a minimal working reproducer (if you are unsure how
 to write one see http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports).

numba ~v0.51.0~ 0.50.1, installed with conda.
Numba disregards the first element of the output list, using simple python range
This is my first try of using numba. Strange that it has such profound bugs in parsing simple python code.

## Reporting a bug

```python
@numba.njit()
def compute(n=10):
    res = [0.]
    for i in range(n):
        res.append(i)
    return np.array(res, dtype=np.float32)
```

With the `numba.njit` decorator the output is `[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]`.
Without the decorator the (correct) output is `[0. 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]`


- [x] I have tried using the latest released version of Numba (most recent is
 visible in the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
- [x] I have included below a minimal working reproducer (if you are unsure how
 to write one see http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports).

numba ~v0.51.0~ 0.50.1, installed with conda.


Discussion:
@dizcza thanks for reporting this, a very interesting bug you found there. I believe it has something to do with the `np.array` constructor. If I run the following (which uses integers however):

```
import numba


@numba.njit()
def compute(n=10):
    res = [0]
    for i in range(n):
        res.append(i)
    return res


print(compute())
print(compute.py_func())
```

I get the  correct output:

```
[0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```
i am testing it with current master (6c01e9ab74efb84094c8a443a8635b696f5b2d88) and it is fine:

```python
import numpy as np
import numba


@numba.njit()
def compute(n=10):
    res = [0.]
    for i in range(n):
        res.append(i)
    return np.array(res, dtype=np.float32)


print(compute())
print(compute.py_func())
```
prints
```
[0. 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
[0. 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
```
@dizcza, you said you installed v0.51.0 but we haven't release that version yet. Did you use one of our dev builds?
0.50.1 tag is also fine.
I ran `conda install numba`
Ah sorry, my version is 0.50.1
Thanks for the clarification.

I am now able to replicate it on master as well. It's a problem specific to Python 3.8. I was using Python 3.7 before and it has no problem.
Marking this bug a highpriority because incorrect result is the worst kind of bug.
Yes, I can confirm that I am unable to reproduce this with Python 3.7, so it seems to be a 3.8 issue only issue.
@dizcza thanks for reporting this, a very interesting bug you found there. I believe it has something to do with the `np.array` constructor. If I run the following (which uses integers however):

```
import numba


@numba.njit()
def compute(n=10):
    res = [0]
    for i in range(n):
        res.append(i)
    return res


print(compute())
print(compute.py_func())
```

I get the  correct output:

```
[0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```
i am testing it with current master (6c01e9ab74efb84094c8a443a8635b696f5b2d88) and it is fine:

```python
import numpy as np
import numba


@numba.njit()
def compute(n=10):
    res = [0.]
    for i in range(n):
        res.append(i)
    return np.array(res, dtype=np.float32)


print(compute())
print(compute.py_func())
```
prints
```
[0. 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
[0. 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
```
@dizcza, you said you installed v0.51.0 but we haven't release that version yet. Did you use one of our dev builds?
0.50.1 tag is also fine.
I ran `conda install numba`
Ah sorry, my version is 0.50.1
Thanks for the clarification.

I am now able to replicate it on master as well. It's a problem specific to Python 3.8. I was using Python 3.7 before and it has no problem.
Marking this bug a highpriority because incorrect result is the worst kind of bug.
Yes, I can confirm that I am unable to reproduce this with Python 3.7, so it seems to be a 3.8 issue only issue.

Resolving patch:
diff --git a/numba/core/inline_closurecall.py b/numba/core/inline_closurecall.py
--- a/numba/core/inline_closurecall.py
+++ b/numba/core/inline_closurecall.py
@@ -939,6 +939,8 @@ def _inline_arraycall(func_ir, cfg, visited, loop, swapped, enable_prange=False,
         list_var_def = get_definition(func_ir, list_var_def.value)
     # Check if the definition is a build_list
     require(isinstance(list_var_def, ir.Expr) and list_var_def.op ==  'build_list')
+    # The build_list must be empty
+    require(len(list_var_def.items) == 0)
 
     # Look for list_append in "last" block in loop body, which should be a block that is
     # a post-dominator of the loop header.


Test patch:
diff --git a/numba/tests/test_comprehension.py b/numba/tests/test_comprehension.py
--- a/numba/tests/test_comprehension.py
+++ b/numba/tests/test_comprehension.py
@@ -282,6 +282,16 @@ def comp_with_array_noinline(n):
         finally:
             ic.enable_inline_arraycall = True
 
+    def test_comp_with_array_noinline_issue_6053(self):
+        def comp_with_array_noinline(n):
+            lst = [0]
+            for i in range(n):
+                lst.append(i)
+            l = np.array(lst)
+            return l
+
+        self.check(comp_with_array_noinline, 5, assert_allocate_list=True)
+
     def test_comp_nest_with_array(self):
         def comp_nest_with_array(n):
             l = np.array([[i * j for j in range(n)] for i in range(n)])