-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Avoid generating or emitting NullablePublicOnlyAttribute when no other nullable attributes are emitted #37019
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ public partial class CSharpCompilation | |
/// </summary> | ||
private Symbol[] _lazyWellKnownTypeMembers; | ||
|
||
private bool _usesNullableAttributes; | ||
private int _needsGeneratedAttributes; | ||
private bool _needsGeneratedAttributes_IsFrozen; | ||
|
||
|
@@ -42,12 +43,24 @@ internal EmbeddableAttributes GetNeedsGeneratedAttributes() | |
return (EmbeddableAttributes)_needsGeneratedAttributes; | ||
} | ||
|
||
internal void SetNeedsGeneratedAttributes(EmbeddableAttributes attributes) | ||
private void SetNeedsGeneratedAttributes(EmbeddableAttributes attributes) | ||
{ | ||
Debug.Assert(!_needsGeneratedAttributes_IsFrozen); | ||
ThreadSafeFlagOperations.Set(ref _needsGeneratedAttributes, (int)attributes); | ||
} | ||
|
||
internal bool GetUsesNullableAttributes() | ||
{ | ||
_needsGeneratedAttributes_IsFrozen = true; | ||
return _usesNullableAttributes; | ||
} | ||
|
||
private void SetUsesNullableAttributes() | ||
{ | ||
Debug.Assert(!_needsGeneratedAttributes_IsFrozen); | ||
_usesNullableAttributes = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
just to confirm, we don't need any special operation to ensure this set is atomic, correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
/// <summary> | ||
/// Lookup member declaration in well known type used by this Compilation. | ||
/// </summary> | ||
|
@@ -456,6 +469,12 @@ private void EnsureEmbeddableAttributeExists(EmbeddableAttributes attribute, Dia | |
{ | ||
SetNeedsGeneratedAttributes(attribute); | ||
} | ||
|
||
if ((attribute & (EmbeddableAttributes.NullableAttribute | EmbeddableAttributes.NullableContextAttribute)) != 0 && | ||
modifyCompilation) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so do we expect that in batch compilation, something passes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I'm really wondering when/why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like we pass In reply to: 300809615 [](ancestors = 300809615) |
||
{ | ||
SetUsesNullableAttributes(); | ||
} | ||
} | ||
|
||
internal void EnsureIsReadOnlyAttributeExists(DiagnosticBag diagnostics, Location location, bool modifyCompilation) | ||
|
@@ -483,11 +502,6 @@ internal void EnsureNullableContextAttributeExists(DiagnosticBag diagnostics, Lo | |
EnsureEmbeddableAttributeExists(EmbeddableAttributes.NullableContextAttribute, diagnostics, location, modifyCompilation); | ||
} | ||
|
||
internal void EnsureNullablePublicOnlyAttributeExists(DiagnosticBag diagnostics, Location location, bool modifyCompilation) | ||
{ | ||
EnsureEmbeddableAttributeExists(EmbeddableAttributes.NullablePublicOnlyAttribute, diagnostics, location, modifyCompilation); | ||
} | ||
|
||
internal bool CheckIfAttributeShouldBeEmbedded(EmbeddableAttributes attribute, DiagnosticBag diagnosticsOpt, Location locationOpt) | ||
{ | ||
switch (attribute) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -255,18 +255,6 @@ internal override void ForceComplete(SourceLocation locationOpt, CancellationTok | |
_state.SpinWaitComplete(CompletionPart.FinishValidatingReferencedAssemblies, cancellationToken); | ||
break; | ||
|
||
case CompletionPart.StartMemberChecks: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We were reporting those diagnostics before, but now emitting of this attribute is tied to the other attributes so any diagnostics will be reported from those attributes. In reply to: 301355338 [](ancestors = 301355338) |
||
case CompletionPart.FinishMemberChecks: | ||
if (_state.NotePartComplete(CompletionPart.StartMemberChecks)) | ||
{ | ||
var diagnostics = DiagnosticBag.GetInstance(); | ||
AfterMembersChecks(diagnostics); | ||
AddDeclarationDiagnostics(diagnostics); | ||
diagnostics.Free(); | ||
_state.NotePartComplete(CompletionPart.FinishMemberChecks); | ||
} | ||
break; | ||
|
||
case CompletionPart.MembersCompleted: | ||
this.GlobalNamespace.ForceComplete(locationOpt, cancellationToken); | ||
|
||
|
@@ -533,24 +521,6 @@ internal override void DecodeWellKnownAttribute(ref DecodeWellKnownAttributeArgu | |
} | ||
} | ||
|
||
private bool EmitNullablePublicOnlyAttribute | ||
{ | ||
get | ||
{ | ||
var compilation = DeclaringCompilation; | ||
return compilation.EmitNullablePublicOnly && | ||
compilation.IsFeatureEnabled(MessageID.IDS_FeatureNullableReferenceTypes); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We used to check the feature flag, but I didn't see whether that is still handled somehow. Was that unnecessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We only emit In reply to: 301355808 [](ancestors = 301355808) |
||
} | ||
} | ||
|
||
private void AfterMembersChecks(DiagnosticBag diagnostics) | ||
{ | ||
if (EmitNullablePublicOnlyAttribute) | ||
{ | ||
DeclaringCompilation.EnsureNullablePublicOnlyAttributeExists(diagnostics, location: NoLocation.Singleton, modifyCompilation: true); | ||
} | ||
} | ||
|
||
internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<SynthesizedAttributeData> attributes) | ||
{ | ||
base.AddSynthesizedAttributes(moduleBuilder, ref attributes); | ||
|
@@ -566,7 +536,7 @@ internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, r | |
} | ||
} | ||
|
||
if (EmitNullablePublicOnlyAttribute) | ||
if (moduleBuilder.ShouldEmitNullablePublicOnlyAttribute()) | ||
{ | ||
var includesInternals = ImmutableArray.Create( | ||
new TypedConstant(compilation.GetSpecialType(SpecialType.System_Boolean), TypedConstantKind.Primitive, _assemblySymbol.InternalsAreVisible)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: should this field be renamed, since now used for freezing both "needsGenerated" and "usesNullable"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I considered renaming the field but didn't find a better name.
In reply to: 301354830 [](ancestors = 301354830)