Skip to content

Commit

Permalink
Bug repro, compiled iteration over uninitialized ListOf(bool)
Browse files Browse the repository at this point in the history
Strange behavior observed when iterating through a list of bools which
was created with .setSizeUnsafe(N), so full of garbage data.

Outside of the compiler, or when using the builtin sum(), the iteration
gives the expected answer, the number of True values in the garbage data.

Inside a simple Entrypointed function to count Trues, we get a
ludicrously large answer (bigger than N).

Running the same function with some extra added statements for no
purpose (could be almost anything more than "pass"), we instead get 0.
The compiler seems to know to replace uninitialized data with False.
  • Loading branch information
Nat committed Jun 29, 2023
1 parent 8c2c809 commit a6275b6
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions typed_python/uninitialized_list_bug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from typed_python import ListOf, Entrypoint


N = 50
A = ListOf(bool)()
A.reserve(N)
A.setSizeUnsafe(N)


@Entrypoint
def countTrueInUninitializedList(A):
total = 0
for i in range(len(A)):
if A[i]:
total += 1

return total, sum(A)


print(countTrueInUninitializedList(A))


@Entrypoint
def sameButWithAddedStatements(A):
indices = ListOf(int)()
total = 0
for i in range(len(A)):
if A[i]:
indices.append(i)
total += 1

return total, sum(A)


print(sameButWithAddedStatements(A))

0 comments on commit a6275b6

Please sign in to comment.