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

Use covariant return for synthesized record clone #53404

Merged
merged 16 commits into from
Jul 11, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ static bool modifiersAreValid(DeclarationModifiers modifiers)

protected override (TypeWithAnnotations ReturnType, ImmutableArray<ParameterSymbol> Parameters, bool IsVararg, ImmutableArray<TypeParameterConstraintClause> DeclaredConstraintsForOverrideOrImplementation) MakeParametersAndBindReturnType(BindingDiagnosticBag diagnostics)
{
return (ReturnType: VirtualCloneInBase() is { } baseClone ?
baseClone.ReturnTypeWithAnnotations : // Use covariant returns when available
return (ReturnType: VirtualCloneInBase() is { } baseClone && !ContainingAssembly.RuntimeSupportsCovariantReturnsOfClasses ?
Copy link
Contributor

@AlekseyTs AlekseyTs Jun 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!ContainingAssembly.RuntimeSupportsCovariantReturnsOfClasses

Consider reordering conditions. #Closed

baseClone.ReturnTypeWithAnnotations :
TypeWithAnnotations.Create(isNullableEnabled: true, ContainingType),
Copy link
Member

@cston cston May 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include a test that demonstrates the change. #Closed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was expecting to see CI failures since this code path is already extensively tested. Will look and see why no tests are failing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests for records don't vary the RuntimeSupportsCovariantReturnsOfClasses value from corlib, and I don't know what value is there in the corlib that we reference.

One way to control this is to define your own tiny corlib in a test using CreateEmptyCompilation. See RecordTests.ObjectGetHashCode_15 for example.
Another way is to use an old vs. new target framework (targetFramework in CreateCompilation) assuming that the available options are sufficient to achieve the desired effect.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jcouv I made the assumption that it will work based on another test doing the same. But it looks like the other test has dead code path. But double checking from CI run (#53413)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One way to control this is to define your own tiny corlib in a test using CreateEmptyCompilation

I prefer that we don't do this, we want to test against real frameworks and verify runtime behavior as well. Achieving that with a private tiny corlib is going to be non trivial. I think that testing both modes should be rather simple. It looks like record tests use default TargetFramework. That means that, when tests are run on desktop they are run against desktop (doesn't support covariant returns) and, when run on CoreCLR they are run against CoreCLR (supports covariant returns). We should be able to observe difference between these two modes. However, it looks like default target framework for tests on CoreClr is NetStandard20 (doesn't say that the feature will be supported at runtime), we should be able to explicitly request NetCoreApp framework. I think TargetFramework.StandardLatest would give us what we want for desktop and for CoreCLR. The expectations in the tests will have to be conditional based on the platform

Parameters: ImmutableArray<ParameterSymbol>.Empty,
IsVararg: false,
Expand Down
Loading