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

[BUG] Initializing an enum array does not compile #1316

Open
MatthieuHernandez opened this issue Oct 12, 2024 · 10 comments
Open

[BUG] Initializing an enum array does not compile #1316

MatthieuHernandez opened this issue Oct 12, 2024 · 10 comments
Labels
bug Something isn't working

Comments

@MatthieuHernandez
Copy link
Contributor

I try to compile this simple code:

Color: @enum<u8> type = {
    Black: = 0;
    White: = 1;
}
main: () = {
    colors: std::array<Color, 3> = (Color::Black);
}

The C++ code is generated correctly but I get an error when compiling.

main.cpp2:6:40: error: chosen constructor is explicit in copy-initialization
    6 |     std::array<Color,3> colors {Color::Black}; 
      |                                        ^~~~~
main.cpp2:12:18: note: explicit constructor declared here
   12 | constexpr Color::Color()
      |                  ^
main.cpp2:6:40: note: in implicit initialization of array element 1 with omitted initializer
    6 |     std::array<Color,3> colors {Color::Black}; 
      |                                        ^
1 error generated.

Here is the code I ran:
https://cpp2.godbolt.org/z/MnrPqT4fK

@MatthieuHernandez MatthieuHernandez added the bug Something isn't working label Oct 12, 2024
@gregerolsson
Copy link

I'm wondering if something really breaking has happened lately. I can't even get the hello world example to compile either.

https://cpp2.godbolt.org/z/3n3YW5rdW

@JohelEGP

This comment was marked as off-topic.

@MatthieuHernandez
Copy link
Contributor Author

@JohelEGP Yes, but what's the solution to my issue? It doesn't seem to be related to my problem.

@JohelEGP
Copy link
Contributor

If you add @print after, you'll see that it has this (https://cpp2.godbolt.org/z/P6TYxEasK):

    operator=:(out this, ) == 
    {
        _value = Black._value;
    }

If you attempt to add an implicit default constructor, e.g. (https://cpp2.godbolt.org/z/hqonP3dbG):

    operator=: (implicit out this) = {
         this = Black;
    }

It'll fail with:

main.cpp2(4,5): error: while applying @enum - in a 'enum' type, the name 'operator=' is reserved for use by the 'enum' implementation

If you want to use @enum with std::array like that,
then @enum will need to emit an implicit default constructor,
or allow the user to write one.

@MatthieuHernandez
Copy link
Contributor Author

Yes, cpp2 enum should have a default constructor.
I'd like to contribute to the cppfront project but I'm not sure where to start, the code is a little difficult to grasp.

@JohelEGP
Copy link
Contributor

Oh, but it does have a default constructor, it's just explicit.
So this works: colors: std::array<Color, 3> = (Color(), Color(), Color()); (https://cpp2.godbolt.org/z/9Wrj8qKd8).

@MatthieuHernandez
Copy link
Contributor Author

Ah yes indeed, thank you 😊

@MatthieuHernandez
Copy link
Contributor Author

@JohelEGP In fact, I have the same problem with any objects, if I redefine the constructor.
I feel like there's no way to write this or an equivalent:

Color: type = {
    value: i32 = 0;

    operator=:(out this) = {
        value++;
    }
}
main: () = {
    colors: std::array<Color, 1000> = ();
    std::cout << Color[999].value << std::endl;
}

This colors: std::array<Color, 1000> = (); required an implicit constructor on Color that isn't possible for now.
And this colors: std::array<Color, 1000>; prodce this error program violates initialization safety guarantee which seems normal.

But my conclusion is that for the moment it's impossible to declare an array of 1000 objects without writing them all one by one. So to keep on progressing on my project I'll have to use std::vector instead of std::array.

Please feel free to correct me if I'm talking nonsense 😅

@JohelEGP
Copy link
Contributor

That out this parameter is missing that implicit keyword above.

@MatthieuHernandez
Copy link
Contributor Author

Oh yes, sorry, I didn't realize that implicit keyword existed in cpp2. I totally missed it when I looked at the documentation.
Now everything is working properly, I spent so much time pulling my hair out for nothing 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants