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

Records (C# reference)|Inheritance|with expressions in derived records – Clone does not use *covariant return type* #24881

Closed
bravequickcleverfibreyarn opened this issue Jun 27, 2021 · 4 comments · Fixed by #25045
Assignees
Labels
doc-bug Problem with the content; needs to be fixed [org][type][category] dotnet-csharp/svc lang-reference/subsvc Pri1 High priority, do before Pri2 and Pri3

Comments

@bravequickcleverfibreyarn
Copy link
Contributor

bravequickcleverfibreyarn commented Jun 27, 2021

From Records (C# reference)/Inheritance/with expressions in derived records

Because the synthesized clone method uses a covariant return type, the result of a with expression has the same run-time type as the expression's operand.

This seems rendered as a lie.

Decompilation
private void Test()
{
  DerivedRec dr = (DerivedRec)new DerivedRec().<Clone>$();
}

public override BaseRec <Clone>$()
{
  return new DerivedRec(this);
}
Definition
class CovariantReturnType
{
    record BaseRec () { }
    record DerivedRec : BaseRec { }

    void Test ()
    {       
      var dr = new DerivedRec () with { };
    }
}

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@dotnet-bot dotnet-bot added the ⌚ Not Triaged Not triaged label Jun 27, 2021
@PRMerger16 PRMerger16 added lang-reference/subsvc dotnet-csharp/svc Pri1 High priority, do before Pri2 and Pri3 labels Jun 27, 2021
@Youssef1313
Copy link
Member

This is being worked on currently in Roslyn. See dotnet/roslyn#53404.

@bravequickcleverfibreyarn
Copy link
Contributor Author

bravequickcleverfibreyarn commented Jun 28, 2021

This is being worked on currently in Roslyn. See dotnet/roslyn#53404.

Ok, I see. Documentation antedates implementation. Not big problem.

@Youssef1313
Copy link
Member

@BillWagner Do you think it's worth specifying the compiler version which have this update when it gets merged?

@BillWagner
Copy link
Member

@Youssef1313 @Uzivatel919

As a fix, I think I'd prefer removing that implementation detail from the description of records. The important concept is that with expressions don't accidentally slice a record if the source argument has a compile time type that is a base class of the runtime type.

@BillWagner BillWagner added the doc-bug Problem with the content; needs to be fixed [org][type][category] label Jun 28, 2021
@dotnet-bot dotnet-bot removed the ⌚ Not Triaged Not triaged label Jun 28, 2021
BillWagner added a commit to BillWagner/docs that referenced this issue Jul 8, 2021
Fixes dotnet#24880

The calling sequence between `Clone` and the copy constructor was reversed.

Fixes dotnet#24881

Remove the implementation detail of covariant returns.
@BillWagner BillWagner self-assigned this Jul 8, 2021
BillWagner added a commit that referenced this issue Jul 8, 2021
Fixes #24880

The calling sequence between `Clone` and the copy constructor was reversed.

Fixes #24881

Remove the implementation detail of covariant returns.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-bug Problem with the content; needs to be fixed [org][type][category] dotnet-csharp/svc lang-reference/subsvc Pri1 High priority, do before Pri2 and Pri3
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants