c3c icon indicating copy to clipboard operation
c3c copied to clipboard

Compiler crash trying `foreach` on distinct pointers

Open 31a05b9c opened this issue 2 years ago • 32 comments

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;
}

31a05b9c avatar Sep 30 '24 15:09 31a05b9c

This should now work, please check.

lerno avatar Sep 30 '24 19:09 lerno

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 [] and len need to be implemented.

31a05b9c avatar Oct 01 '24 04:10 31a05b9c

@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;
}

Caleb-o avatar Oct 01 '24 07:10 Caleb-o

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: image

Did I not do this right?

Caleb-o avatar Oct 01 '24 07:10 Caleb-o

@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

31a05b9c avatar Oct 01 '24 07:10 31a05b9c

@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.

Caleb-o avatar Oct 01 '24 07:10 Caleb-o

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.

lerno avatar Oct 01 '24 11:10 lerno

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.

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!

Caleb-o avatar Oct 01 '24 11:10 Caleb-o

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.

31a05b9c avatar Oct 01 '24 11:10 31a05b9c

It can't be an error, because then String foo; char x = foo[2]; would stop working.

lerno avatar Oct 01 '24 11:10 lerno

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.

lerno avatar Oct 01 '24 11:10 lerno

If the distinct variant itself is iterable, does it make much sense for it to allow an implementation of iterable?

Caleb-o avatar Oct 01 '24 11:10 Caleb-o

It could. For example, we could create an "even slice" where get maps to i * 2 (and len is half of the wrapped type).

lerno avatar Oct 01 '24 11:10 lerno

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.

Caleb-o avatar Oct 01 '24 11:10 Caleb-o

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.

lerno avatar Oct 01 '24 11:10 lerno

Is there a way with contracts or reflection etc, to know whether something is iterable?

Caleb-o avatar Oct 01 '24 11:10 Caleb-o

$defined(foo[0]) &&& ($defined(foo.len) ||| $defined(foo.len())) should work well.

lerno avatar Oct 01 '24 11:10 lerno

I was thinking of a type property as well, but it's not available right now.

lerno avatar Oct 01 '24 12:10 lerno

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.

Caleb-o avatar Oct 01 '24 12:10 Caleb-o

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.

31a05b9c avatar Oct 01 '24 12:10 31a05b9c