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

Recursive C structures blow the stack when compiling #760

Open
yorickpeterse opened this issue Sep 5, 2024 · 0 comments
Open

Recursive C structures blow the stack when compiling #760

yorickpeterse opened this issue Sep 5, 2024 · 0 comments
Labels
accepting contributions Issues that are suitable to be worked on by anybody, not just maintainers bug Defects, unintended behaviour, etc compiler Changes related to the compiler

Comments

@yorickpeterse
Copy link
Collaborator

yorickpeterse commented Sep 5, 2024

Please describe the bug

The compiler doesn't handle recursive C structures gracefully, i.e. the following code crashes it:

class extern Foo {
  let @a: Int32
  let @b: Foo
}

class async Main {
  fn async main {
    Foo(a: 0 as Int32, b: Foo())
  }
}

The problem here is that when we try to calculate the struct size for LLVM, we encounter an infinite loop and blow the stack. This problem would also be something one could encounter using user-defined value types (per #750), when/if we support those.

To handle this, we need a way to detect if a class extern class is recursive and if so produce a compile-time error. At least for class extern this isn't too difficult and comes down to the following:

  • For each field, check if its type is the same as the surrounding class or is another class extern that contains the surrounding class
  • Make that recursive by checking each field of the sub class

For custom value types it's more tricky as they can be generic. This means we don't know if a type is recursive until its specialized. At that point, producing a meaningful error may prove difficult.

We could instead check at the initialization site, but then we pay an extra compile-time cost for every initialization of a value type, which I'd like to avoid.

Operating system

Fedora

Inko version

main

@yorickpeterse yorickpeterse added accepting contributions Issues that are suitable to be worked on by anybody, not just maintainers bug Defects, unintended behaviour, etc compiler Changes related to the compiler labels Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting contributions Issues that are suitable to be worked on by anybody, not just maintainers bug Defects, unintended behaviour, etc compiler Changes related to the compiler
Projects
None yet
Development

No branches or pull requests

1 participant