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

Copy constructor should not be allowed to omit this initializer call in a type that is not a record #68345

Closed
tmat opened this issue May 27, 2023 · 0 comments · Fixed by #68442
Assignees
Labels
Milestone

Comments

@tmat
Copy link
Member

tmat commented May 27, 2023

According to https://github.com/dotnet/csharplang/blob/main/proposals/primary-constructors.md#primary-constructors-on-records:

If an explicit constructor declaration is a "copy constructor" - a constructor that takes a single parameter of the enclosing type - it is not required to call a this initializer, and will not execute the member initializers present in the record declaration.

A copy-constructor in a record is allowed to not call this initializer. However, the compiler also allows it for non-record type with a primary constructor:

    public class D(int X)
    {
        protected D(D other)
        {
            Console.Write("copy ctor");
        }

        protected D(string other) // error CS8862: A constructor declared in a type with parameter list must have 'this' constructor initializer.
        {
            Console.Write("not a copy ctor");
        }
    }

CS8862 should be reported for the copy constructor as well in this case.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels May 27, 2023
@jcouv jcouv added Bug and removed untriaged Issues and PRs which have not yet been triaged by a lead labels May 30, 2023
@jcouv jcouv added this to the 17.7 milestone May 30, 2023
AlekseyTs added a commit to AlekseyTs/roslyn that referenced this issue Jun 5, 2023
@AlekseyTs AlekseyTs added the 4 - In Review A fix for the issue is submitted for review. label Jun 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants