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] Fail to interpret x:y:z as Slice in __getitem__ and in Variant #3930

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

Comments

@forFudan
Copy link

forFudan commented Jan 7, 2025

Bug description

I am working on indexing and slicing by means of the __getitem__ method of structs. I noticed that when the arguments are Variant[Slice, AnyType), the syntax x[Slice(0,10,1)] works but x[0:10:1] fails.

It seems that x:y:z cannot be interpretted by compiler as Slice in square brackets.

Steps to reproduce

Here is an example that prints either an Int or the start of a Slice.

The error message is also provided.

We can see that Slice(5, 2, 1) works but foo[5:2:1] fails.

from utils import Variant


fn main():
    var foo = Foo(List[Int](0, 1, 2, 3, 4, 5, 6, 7))
    foo[5]  # works
    foo[Slice(5, 2, 1)]  # works
    foo[5:2:1]  # fails!
# error: invalid call to '__getitem__': index  cannot be converted from initializer list to 'Variant[Int, Slice]'
#     foo[5:2:1]
    ~~~^~~~~~~


struct Foo:
    var buf: List[Int]

    fn __init__(out self, data: List[Int]):
        self.buf = data

    fn __getitem__(self, i: Variant[Int, Slice]):
        if i.isa[Int]():
            print(self.buf[i._get_ptr[Int]()[0]])
        else:
            var s = i._get_ptr[Slice]()[0]
            print(self.buf[s.start.or_else(1)])

Error message:

error: invalid call to '__getitem__': index cannot be converted from initializer list to 'Variant[Int, Slice]'
    foo[5:2:1]
    ~~~^~~~~~~
note: try resolving the overloaded function first
    foo[5:2:1]
         ^
note: function declared here
    fn __getitem__(self, i: Variant[Int, Slice]):
       ^

System information

- What OS did you do install Mojo on?
MacOS 15.2
- Provide version information for Mojo by pasting the output of `mojo -v`
mojo 24.6.0 (4487cd6e)
- Provide Magic CLI version by pasting the output of `magic -V` or `magic --version` 
magic 0.5.1 - (based on pixi 0.37.0)
- Optionally, provide more information with `magic info`.

     Magic version: 0.5.1
System
------------
      Pixi version: 0.37.0
          Platform: osx-arm64
  Virtual packages: __unix=0=0
                  : __osx=15.2=0
                  : __archspec=1=m2
         Cache dir: /Users/ZHU/Library/Caches/rattler/cache
      Auth storage: /Users/ZHU/.rattler/credentials.json
  Config locations: No config files found

Global
------------
           Bin dir: /Users/ZHU/.modular/bin
   Environment dir: /Users/ZHU/.modular/envs
      Manifest dir: /Users/ZHU/.modular/manifests/pixi-global.toml

Project
------------
              Name: NuMojo
           Version: 0.4.0
     Manifest file: /Users/ZHU/Programs/NuMojo/mojoproject.toml
      Last updated: 22-12-2024 19:54:34

Environments
------------
       Environment: default
          Features: default
          Channels: conda-forge, https://conda.modular.com/max
  Dependency count: 4
      Dependencies: max, python, numpy, scipy
  Target platforms: osx-arm64, linux-64
             Tasks: p, package, final, test, t, f, format
@forFudan forFudan added bug Something isn't working mojo-repo Tag all issues with this label labels Jan 7, 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