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

Selfhost Compiler Crashed with Segmentation Fault printing a LinkedList #973

Open
horenso opened this issue Jul 25, 2022 · 6 comments
Open
Labels

Comments

@horenso
Copy link
Contributor

horenso commented Jul 25, 2022

WHF :) On the most recent main branch (commit 9c52bee6) I tried to compile the following progam:

struct LinkedList {
    value: i64
    next: LinkedList?
}

function main() {
    mut node1 = LinkedList(value: 1, next: None)
}

and the compiler (not the resulting program) crashed with a Segmentation Fault.|

Steps I took:

cargo install --path .
jakt ./selfhost/main.jakt && mv ./build/main ./jakt_selfhost
./jakt_selfhost test.jakt # test.jakt is the program shown above

The Rust-based compiler doesn't crash and produces some Cpp which clang then rejects with
error: invalid application of 'sizeof' to an incomplete type 'Jakt::LinkedList'

@robryanx
Copy link
Contributor

It is basically this line: if struct_.record_type is Class and not top_level { in codegen.jakt
As the record type is Struct it ends up with infinite recursion.
If the code is changed from struct to class it works, I am not sure if there should be an error about recursion in structs or that the codegen should be changed to handle it.

@horenso
Copy link
Contributor Author

horenso commented Jul 26, 2022

Ah interesting, since Struct is a value type it makes sense that it cannot contain itself as a value. I think the typechecker should handle that case.

@maddanio
Copy link
Contributor

maddanio commented Aug 6, 2022

Basically the same as with recursive enums which have to be boxed (aka class)

@7w17chy
Copy link

7w17chy commented Sep 18, 2022

So the compiler should reject code in which a value type references itself directly, as in:

struct S {
    field: S
}

and indirectly through other value types such as tuples, arrays or other structs (e. g. Optional):

struct S {
    field: S?
}

whilst allowing non-value types such as pointers to reference it safely?

@maddanio
Copy link
Contributor

only if it is a struct, it should work for classes.

@maddanio
Copy link
Contributor

i.e. this should be ok:

class S {
    field: S
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants