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

Ref fields feature requires ByRefFields runtime flag #62941

Merged
merged 12 commits into from
Aug 5, 2022

Conversation

jcouv
Copy link
Member

@jcouv jcouv commented Jul 26, 2022

Fixes #62919

Relates to test plan #59194

@jcouv jcouv marked this pull request as ready for review August 2, 2022 15:57
@jcouv jcouv requested a review from a team as a code owner August 2, 2022 15:57
@@ -5283,7 +5283,11 @@ ref struct S<T>
ref readonly T F2;
}"
Dim comp = CreateCSharpCompilation(GetUniqueName(), source, parseOptions:=New CSharp.CSharpParseOptions(CSharp.LanguageVersion.Preview))
comp.VerifyDiagnostics()
comp.VerifyDiagnostics(
Copy link
Member

@333fred 333fred Aug 2, 2022

Choose a reason for hiding this comment

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

Please use AssertTheseDiagnostics #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

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

Had tried that, but it fails with System.InvalidCastException : Unable to cast object of type 'Microsoft.CodeAnalysis.CSharp.CSharpCompilation' to type 'Microsoft.CodeAnalysis.VisualBasic.VisualBasicCompilation'.

Copy link
Member Author

Choose a reason for hiding this comment

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

Also, I'd considered getting ride of the diagnostic, by adding a runtime flag, but the C# test hook can't easily be reached from VB tests.
Another option is to skip the test until we have TargetFramework.Net70

Copy link
Member

Choose a reason for hiding this comment

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

Then please at least include what these errors are.

Copy link
Member

@333fred 333fred left a comment

Choose a reason for hiding this comment

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

Done review pass (commit 1)

@jcouv jcouv requested a review from 333fred August 2, 2022 16:57
@@ -30,6 +31,26 @@ private static bool IsNet70OrGreater()

private static string IncludeExpectedOutput(string expectedOutput) => IsNet70OrGreater() ? expectedOutput : null;

private static MetadataReference MscorlibRefWithoutSharingCachedSymbols
Copy link
Member

@cston cston Aug 2, 2022

Choose a reason for hiding this comment

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

private static MetadataReference MscorlibRefWithoutSharingCachedSymbols

Can this be a static readonly field with initializer so we don't need to copy the value to a local in methods that create more than one compilation? #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

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

There's only two tests that use this. What are you trying to achieve?

Copy link
Member

Choose a reason for hiding this comment

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

I was mistaken. I thought this was used in more tests, and that all tests could share a common reference.

@@ -90,7 +111,8 @@ void M5(S<T> s5)
M2(ref s5.F1);
}
}";
var comp = CreateCompilation(sourceA, parseOptions: TestOptions.Regular10);
var mscorlibWithRefFields = MscorlibRefWithoutSharingCachedSymbols;
var comp = CreateByRefFieldsCompilation(sourceA, references: new[] { mscorlibWithRefFields }, parseOptions: TestOptions.Regular10);
Copy link
Member

@cston cston Aug 2, 2022

Choose a reason for hiding this comment

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

CreateByRefFieldsCompilation(sourceA, references: new[] { mscorlibWithRefFields }, parseOptions: TestOptions.Regular10)

Could we hide most of the work in CreateCompilation()?

For instance, add a parameter to CreateCompilation(..., RuntimeFeatures runtimeFeatures, ...) that creates a cloned version of the appropriate corlib with the necessary features enabled, and that cloned version is created at most once and shared with all callers that need the same set of features. #Resolved

@jcouv jcouv requested a review from cston August 3, 2022 04:25
// RuntimeSupportsByRefFields property for it.
var mscorlibRefWithRefFields =
((AssemblyMetadata)((MetadataImageReference)MscorlibRef).GetMetadata()).CopyWithoutSharingCachedSymbols().
GetReference(display: "mscorlib.v4_0_30319.dll");
Copy link
Member

@cston cston Aug 3, 2022

Choose a reason for hiding this comment

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

Can we calculate this once and cache the value, perhaps in CSharpTestBase? #Resolved

Copy link
Member

@cston cston Aug 3, 2022

Choose a reason for hiding this comment

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

Perhaps each caller needs a separate instance in case not all callers set the same runtime features. In that case, consider extracting a helper method in the base class.

@jcouv
Copy link
Member Author

jcouv commented Aug 3, 2022

@333fred for second review. Thanks

@jcouv
Copy link
Member Author

jcouv commented Aug 4, 2022

@333fred for second review. Thanks

Copy link
Member

@333fred 333fred left a comment

Choose a reason for hiding this comment

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

Done review pass (commit 9). @jcouv and I need to sync on the approach taken in the tests, as no previous bypass flag has needed this level of modification before.

=> RuntimeSupportsFeature(SpecialMember.System_Runtime_CompilerServices_RuntimeFeature__ByRefFields);
internal virtual bool RuntimeSupportsByRefFields
{
get
Copy link
Member

@333fred 333fred Aug 4, 2022

Choose a reason for hiding this comment

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

Nit: please use consistent style between these two one-line methods :). #Resolved

var comp = CreateCompilation(sourceA, parseOptions: TestOptions.Regular10);
var mscorlibRefWithRefFields = GetMscorlibRefWithoutSharingCachedSymbols();

// Note: we use skipUsesIsNullable and skipExtraValidation so that nobody pulls
Copy link
Member

@333fred 333fred Aug 4, 2022

Choose a reason for hiding this comment

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

I'm confused why this is necessary. I wouldn't expect anything to be pulling on the compilation or it's references until the compilation is actually used? Can you clarify why we need to do this for this particular flag, but haven't needed to for any of the other RuntimeSupports bypass flags? #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

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

We don't need this for this particular flag, but we need it for certain flags such as numeric IntPtr (pattern that I'm following).
I'll remove but we should have a discussion at some point on a universal test hook, so that we don't have to special-case every new runtime feature flag.

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 opened an issue (#63223) to sit down and design a general/re-usable CreateCompilation API supporting what we need, including a note on skipUsesIsNullable and skipExtraValidation since some features need those but some don't.

@@ -30,6 +31,14 @@ private static bool IsNet70OrGreater()

private static string IncludeExpectedOutput(string expectedOutput) => IsNet70OrGreater() ? expectedOutput : null;

private static IEnumerable<MetadataReference> CopyWithoutSharingCachedSymbols(IEnumerable<MetadataReference> references)
Copy link
Member

@333fred 333fred Aug 4, 2022

Choose a reason for hiding this comment

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

Why is this necessary? It wasn't necessary for any previous feature that set override flags. #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

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

This is used in two tests that need more than mscorlib. For example, one needs dynamic machinery.
Unless those previous features tests such combinations, they would not run into this situation.
I can't say that I understand CopyWithoutSharingCachedSymbols well enough to say there isn't a better way to do this. I'm happy to open a tracking issue to re-discuss when Aleksey is back.

Copy link
Member Author

Choose a reason for hiding this comment

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

Filed #63222 to review usages of CopyWithoutSharingCachedSymbols()

@jcouv jcouv merged commit ab3fe7a into dotnet:main Aug 5, 2022
@jcouv jcouv deleted the runtime-flag branch August 5, 2022 15:01
@ghost ghost added this to the Next milestone Aug 5, 2022
@dibarbet dibarbet modified the milestones: Next, 17.4 P2 Sep 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ref field declarations should require runtime feature
4 participants