Skip to content

[BUG] Member initialization of @value type not recognized #821

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

Closed
simeonKuran opened this issue Nov 13, 2023 · 4 comments
Closed

[BUG] Member initialization of @value type not recognized #821

simeonKuran opened this issue Nov 13, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@simeonKuran
Copy link

Description
I tried to write a custom constructor for a @value type.
However, the compilation fails because the member initialization is not recognized

To Reproduce

Minimal example:

test: @value type =
{
    val: int;
    operator=:(out this, _val : int) = { val = _val; }
}

main: () -> int =
{
    x : test = (1);
    return 0;
}

Expected Result
A running program.

Actual result
Compiler error:

main.cpp2: error: in operator=, expected 'val = ...' initialization statement (because type scope object 'val' does not have a default initializer)
main.cpp2(3,5): error: see declaration for 'val' here
main.cpp2: error: an operator= body must start with a series of 'member = value;' initialization statements for each of the type-scope objects in the same order they are declared, or the member must have a default initializer (in type 'test')

Adding a default initializer like val: int = 1; works. But according to the diagnostic message the initial code should also work, because of the 'member = value;' initialization statement.

Additional context
I am using g++ 11.4.0 on Linux

@simeonKuran simeonKuran added the bug Something isn't working label Nov 13, 2023
@JohelEGP
Copy link
Contributor

A @value has a default constructor.
So if val doesn't have a NSDMI, it's uninitialized.

@JohelEGP
Copy link
Contributor

See also #453.

@JohelEGP
Copy link
Contributor

Besides the lowered Cpp1, you can use @print to see the generated Cpp2 (https://cpp2.godbolt.org/z/9a9zcjjx9):

test: type =
{
    val: int;

    operator=:(
        out this,
        in _val: int
    ) =
    {
        val = _val;
    }

    operator<=>:(
        in this,
        in that
    ) -> move std::strong_ordering;

    operator=:(
        out this,
        in that
    ) =
    {
    }

    operator=:(out this) =
    {
    }
}

@simeonKuran
Copy link
Author

Ah, of course! you are right.

I think the diagnostic error message for this example is a little bit misleading. However that's also already mentioned in #453 (although due to different reasons). So I will close this ticket.

@simeonKuran simeonKuran closed this as not planned Won't fix, can't repro, duplicate, stale Nov 14, 2023
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

2 participants