-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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] Not able to iterate over a String
and print items (V24.6)
#3890
Comments
Reproducible with @ConnorGray pulling you in here, because I think we need to merge #3823 to solve this. Then add @forFudan meanwhile you can use fn main() raises:
var a: String = "abcdefg"
for i in a.as_string_slice():
print(i) |
Hey Martin, thanks for the analysis. And thank you @forFudan for bringing this to our attention! 🙂 I've been investigating this as well. I don't have any conclusions yet, and I'm still digesting your suggestion Martin. I just wanted to share this "minimal" self-contained repro I found that illustrates the problem: from os import abort
trait MyWritable:
fn my_write_to(self):
pass
@register_passable("trivial")
struct MyStringSlice[mut: Bool, //, origin: Origin[mut]](
MyWritable,
):
fn my_write_to(self):
pass
struct MySliceIter[mut: Bool, //, origin: Origin[mut]]:
fn __next__(mut self) -> MyStringSlice[origin]:
return abort[MyStringSlice[origin]]()
fn __has_next__(self) -> Bool:
return abort[Bool]()
@value
struct MyString:
fn __iter__(self) -> MySliceIter[__origin_of(self)]:
return abort[MySliceIter[__origin_of(self)]]()
fn main() raises:
var a: MyString = MyString()
# for i in a:
# # print(i)
# foo(i)
alias a_imm_origin = ImmutableOrigin.cast_from[__origin_of(a)].result
var iter = a.__iter__() # MySliceIter[a_imm_origin]
var i = iter.__next__() # MyStringSlice[a_imm_origin]
foo(i)
fn foo[*Args: MyWritable](*args: *Args):
pass One minor comment: The error seems to go away if you change |
@ConnorGray nice repro
I tried setting it up with your repro trait MyWritable:
alias ImmutSelf: MyWritable
fn my_write_to(self: Self.ImmutSelf):
pass
@register_passable("trivial")
struct MyStringSlice[mut: Bool, //, origin: Origin[mut]](
MyWritable,
):
alias ImmutSelf = MyStringSlice[ImmutableOrigin.cast_from[origin].result]
fn my_write_to(self: Self.ImmutSelf):
pass but the compiler doesn't like the trait
So we come again into the problem of traits for non-owning types :( (those with parametric origins). Because for owning types it would just be a matter of making the trait signature I also tried doing struct MyStringSlice[mut: Bool, //, origin: Origin[mut]](
MyWritable,
):
alias ImmutSelf = MyStringSlice[ImmutableOrigin.cast_from[origin].result]
fn immut(self) -> Self.ImmutSelf:
return rebind[Self.ImmutSelf](self)
...
foo(i.immut())
so it's definitely a problem of the
I actually get
|
Bug description
I am not able to iterate over a string and print the items in Mojo V24.6. For example, the following code is no longer working.
It prints following error message:
Steps to reproduce
System information
The text was updated successfully, but these errors were encountered: