Compiler crash trying `foreach` on distinct pointers
Bug
If you try to use foreach on a distinct pointer implementing .len() (such as ZString), the compiler crashes with
FATAL ERROR Should be unreachable -> in llvm_emit_len_for_expr @ in /usr/src/debug/c3c-git/c3c/src/compiler/llvm_codegen_expr.c:2834
Occurs on latest version (git c8018c5)
Reproduction
distinct Type = char*;
fn usz Type.len(str)
{
return 0;
}
fn int main(String[] args)
{
Type x = "AAAAA"; // Or ZString
foreach(y : x)
{
}
return 0;
}
This should now work, please check.
The crash is fixed.
I realized i made a slight mistake in my code, i forgot to add @operator(len)
In that circumstance the behavior is correct, but now an error is emitted even with corrected code, which directly contradicts
In order to use a type with
foreach, e.g.foreach(d : foo), at a minimum[]andlenneed to be implemented.
@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;
}
I have found an odd issue after playing with this, using this as an example:
distinct TypeA = char[];
fn char TypeA.get(self, usz i) @operator([]) {
io::printfn("index = %s", i);
return self[i];
}
fn usz TypeA.len(self) @operator(len)
{
return self.len;
}
fn int main() {
TypeA x = "AAAAA"; // Or ZString
foreach(y : x)
{
io::printfn("%s", y);
}
return 0;
}
Which then results in a stack overflow:
Did I not do this right?
@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 self inside of []. Apparently i was accidentally using &self, so that failing makes perfect sense.
I still don't think an explicit "[] should do self[]" should be needed
@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 operator(len) code, which was a bug, but then it would correctly call operator([])
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 distinct (and its operator overload).
The bug with "len" is fixed now though. Try it again.
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
operator(len)code, which was a bug, but then it would correctly calloperator([])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
distinct(and its operator overload).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 self[] in [] is wrong was correct, but it should also be a warning or error since it's a really easy mistake (and if you want recursion you can call the method by name). But that's a separate issue.
It can't be an error, because then String foo; char x = foo[2]; would stop working.
One obvious thing one could do was to prohibit overriding get for distinct variants of types that in themselves may be iterable, but I don't know if that is ideal.
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 get maps to i * 2 (and len is half of the wrapped type).
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?
$defined(foo[0]) &&& ($defined(foo.len) ||| $defined(foo.len())) should work well.
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 can't be an error, because then
String foo; char x = foo[2];would stop working.
...it would not? i think you misunderstood something.
I meant, add a warning when self[] would cause recursion. But still, separate issue.