-
-
Notifications
You must be signed in to change notification settings - Fork 183
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
Compiler crash trying foreach
on distinct pointers
#1506
Comments
foreach
on ZString
foreach
on distinct pointers
This should now work, please check. |
The crash is fixed. I realized i made a slight mistake in my code, i forgot to add In that circumstance the behavior is correct, but now an error is emitted even with corrected code, which directly contradicts
|
@chri-k I was able to get this to compile: distinct Type = char*;
fn char Type.get(self, usz index) @operator([]) {
return self[index];
}
fn usz Type.len(self) @operator(len)
{
return 0;
}
fn int main() {
Type x = "AAAAA"; // Or ZString
foreach(y : x)
{
}
return 0;
} |
Oh, i tried doing that and got some weird compilation issue so i assumed you can't index I still don't think an explicit " |
@chri-k it seems I can get rid of the get function for it to work, but I feel like this should be an error as I was allowed to implement it. |
Ok, there is a bug, but it's not really in the output @Caleb-o! What would happen would be that the code would ignore the The bug in your code is here: fn char Type.get(self, usz index) @operator([]) {
return self[index];
} Since that one is equal to: fn char Type.get(self, usz index) @operator([]) {
return self.get(index);
} So infinite recursion. The correct way to write it is: fn char Type.get(self, usz index) @operator([]) {
return ((char[])self)[index];
} As this bypasses the The bug with "len" is fixed now though. Try it again. |
Ah okay! That makes sense to why it would just spam index 0 then blow out the stack. I'm glad it still managed to point out a bug though! |
So my assumption that using |
It can't be an error, because then |
One obvious thing one could do was to prohibit overriding |
If the distinct variant itself is iterable, does it make much sense for it to allow an implementation of iterable? |
It could. For example, we could create an "even slice" where |
Hmmm. With my case, how might you ensure that it doesn't get written as I did? My thought was, "I'm passing self, which is just a slice, so I must be able to simply index it". But that wasn't the case and I had to change it so make it work as expected and not recurse. |
Well you DO pass in the self, but the self is a distinct type TypeA. So it will always prefer the override that TypeA has put, right? This is just a pitfall when overriding [] and len by the way. |
Is there a way with contracts or reflection etc, to know whether something is iterable? |
|
I was thinking of a type property as well, but it's not available right now. |
Since it's something more specific, as it's the only thing that allows overloading, it might be nice to have a type property and such. |
...it would not? i think you misunderstood something. I meant, add a warning when |
This is getting a bit derailed. |
Would that not require fairly detailed analysis? For example: fn char Type.get(self, usz index) @operator([]) {
if (index % 2 == 0) return ((char[])self)[index];
return self[index - 1];
} This would not have infinite recursion but is recursive. |
I meant to always warn about |
If you want to add foreach to ZString: macro usz ZString.actual_len(self) @operator(len)
{
return self.len;
}
macro char ZString.get_element(self, usz index) @operator([])
{
return ((char*)self)[index];
} |
BUT I would consider that a bad idea, since |
i guess you could argue that there is no real reason to iterate over a pointer. I mostly-accept that argument ( that section of the docs is still inaccurate though ) In any case, this issue is resolved now. |
Which section @chri-k |
The one that says you need to have |
Is it in some other place than this:
|
I quoted it earlier:
It's the first sentence of the section ( and people often only read that ). |
My quote was from elsewhere. I tried to clarify it somewhat on the page you found, I hope it's better now. |
thank you! |
Bug
If you try to use
foreach
on a distinct pointer implementing.len()
(such asZString
), the compiler crashes withOccurs on latest version (
git c8018c5
)Reproduction
The text was updated successfully, but these errors were encountered: