Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Unexpected copy-on-write behaviour with for loops #3955

Open
hypno2000 opened this issue Jan 17, 2025 · 0 comments
Open

[BUG] Unexpected copy-on-write behaviour with for loops #3955

hypno2000 opened this issue Jan 17, 2025 · 0 comments
Labels
bug Something isn't working mojo-repo Tag all issues with this label

Comments

@hypno2000
Copy link

hypno2000 commented Jan 17, 2025

Bug description

this code:

from collections import InlineArray

def main():
    bits = byte_as_bits(137)
    print("Bits: ", bits)

def byte_as_bits(byte: UInt8) -> String:
    var bits = InlineArray[UInt8, 8](0, 0, 0, 0, 0, 0, 0, 0, 0)
    for i in range(8):
        print("Before: ", byte)
        bits[i] = byte & 1
        byte >>= 1
        print("After:  ", byte)

    var bits_string = String()
    for i in range(8).__reversed__():
        bits_string += str(bits[i])

    return bits_string

outputs:

Before:  137
After:   68
Before:  137
After:   34
Before:  137
After:   17
Before:  137
After:   8
Before:  137
After:   4
Before:  137
After:   2
Before:  137
After:   1
Before:  137
After:   0
Bits:  11111111

It is strange how the byte variable on the "After" print is changing but it keeps its original value in the "Before" print. On every iteration cycle it takes a copy from the original argument but the second print it seems to be a value that was copied only once and is changed with every cycle.

When we assign byte to our own variable before the loop then everything looks good and the return value is also correct:

from collections import InlineArray

def main():
    bits = byte_as_bits(137)
    print("Bits: ", bits)

def byte_as_bits(byte: UInt8) -> String:
    var bits = InlineArray[UInt8, 8](0, 0, 0, 0, 0, 0, 0, 0, 0)
    var my_byte = byte
    for i in range(8):
        print("Before: ", my_byte)
        bits[i] = my_byte & 1
        my_byte >>= 1
        print("After:  ", my_byte)

    var bits_string = String()
    for i in range(8).__reversed__():
        bits_string += str(bits[i])

    return bits_string
Before:  137
After:   68
Before:  68
After:   34
Before:  34
After:   17
Before:  17
After:   8
Before:  8
After:   4
Before:  4
After:   2
Before:  2
After:   1
Before:  1
After:   0
Bits:  10001001

System information

- MacOS 15.2 (24C101)
- mojo 25.1.0.dev2025011705 (92b12ebe)
- magic 0.6.2 - (based on pixi 0.40.0)
@hypno2000 hypno2000 added bug Something isn't working mojo-repo Tag all issues with this label labels Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

1 participant