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] Mutating list while holding immutable reference and mutating immutable references compile successfully. #3937

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

Comments

@Deftioon
Copy link

Bug description

Mutating a list while holding an immutable reference to an item in a list does not error, and mutating this immutable reference also does not error.

Steps to reproduce

struct MyList:
    var list: List[Int]
    
    fn __init__(out self):
        self.list = List[Int]()
    
    fn __getitem__(self, index: Int) -> ref[self.list] Int:
        return self.list[index]
    
    fn append(mut self, value: Int) -> None:
        self.list.append(value)
    
    fn do_stuff(mut self, index: Int) -> None:
        self.list[index] = 1

fn main() raises -> None:
    var mylist = MyList()
    mylist.append(0)
    var q1 = mylist[0]
    print(q1)
    mylist.do_stuff(0)
    print(q1)
    print(mylist[0])

This code snippet creates an immutable reference q1 and then mutates the list, and compiles successfully. Outputs:

0
0
1

Running this function:

fn main() raises -> None:
    var mylist = MyList()
    mylist.append(0)
    var q1 = mylist[0]
    print(q1)
    q1 = 1
    print(q1)
    print(mylist[0])

Also compiles successfully. Outputs:

0
1
0

System information

- What OS did you do install Mojo on ?
- Provide version information for Mojo by pasting the output of `mojo -v`
- Provide Magic CLI version by pasting the output of `magic -V` or `magic --version` 
- Optionally, provide more information with `magic info`.

OS: `Ubuntu 24.04.1 LTS`
Mojo Version: magic 0.4.0 - (based on pixi 0.33.0)
Magic CLI version: `magic 0.4.0 - (based on pixi 0.33.0)`
A bit of `magic info` output:

      Pixi version: 0.33.0
          Platform: linux-64
  Virtual packages: __unix=0=0
                  : __linux=6.8.0=0
                  : __glibc=2.39=0
                  : __cuda=12.4=0
                  : __archspec=1=icelake
         Cache dir: /home/deftioon/.cache/rattler/cache
      Auth storage: /home/deftioon/.rattler/credentials.json
  Config locations: No config files found
@Deftioon Deftioon added bug Something isn't working mojo-repo Tag all issues with this label labels Jan 11, 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

2 participants