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

Emit CompilerFeaturesRequiredAttribute #61388

Merged
merged 2 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -952,14 +952,26 @@ public override ImmutableArray<CSharpAttributeData> GetAttributes()
? _packedFlags.IsReadOnly
: IsValidReadOnlyTarget;

bool checkForRequiredMembers = this.ShouldCheckRequiredMembers() && this.ContainingType.HasAnyRequiredMembers;

bool isExtensionMethod = false;
bool isReadOnly = false;
if (checkForExtension || checkForIsReadOnly)
if (checkForExtension || checkForIsReadOnly || checkForRequiredMembers)
{
containingPEModuleSymbol.LoadCustomAttributesFilterCompilerAttributes(_handle,
ref attributeData,
out isExtensionMethod,
out isReadOnly);
attributeData = containingPEModuleSymbol.GetCustomAttributesForToken(_handle,
filteredOutAttribute1: out CustomAttributeHandle extensionAttribute,
filterOut1: AttributeDescription.CaseSensitiveExtensionAttribute,
filteredOutAttribute2: out CustomAttributeHandle isReadOnlyAttribute,
filterOut2: AttributeDescription.IsReadOnlyAttribute,
filteredOutAttribute3: out _,
filterOut3: (checkForRequiredMembers && DeriveCompilerFeatureRequiredDiagnostic() is null) ? AttributeDescription.CompilerFeatureRequiredAttribute : default,
filteredOutAttribute4: out _,
filterOut4: (checkForRequiredMembers && ObsoleteAttributeData is null) ? AttributeDescription.ObsoleteAttribute : default,
filteredOutAttribute5: out _,
filterOut5: default);

isExtensionMethod = !extensionAttribute.IsNil;
isReadOnly = !isReadOnlyAttribute.IsNil;
}
else
{
Expand Down
25 changes: 13 additions & 12 deletions src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEModuleSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,15 +283,6 @@ internal void LoadCustomAttributes(EntityHandle token, ref ImmutableArray<CSharp
ImmutableInterlocked.InterlockedInitialize(ref customAttributes, loaded);
}

internal void LoadCustomAttributesFilterCompilerAttributes(EntityHandle token,
ref ImmutableArray<CSharpAttributeData> customAttributes,
out bool foundExtension,
out bool foundReadOnly)
{
var loadedCustomAttributes = GetCustomAttributesFilterCompilerAttributes(token, out foundExtension, out foundReadOnly);
ImmutableInterlocked.InterlockedInitialize(ref customAttributes, loadedCustomAttributes);
}

internal void LoadCustomAttributesFilterExtensions(EntityHandle token,
ref ImmutableArray<CSharpAttributeData> customAttributes)
{
Expand All @@ -303,7 +294,7 @@ internal ImmutableArray<CSharpAttributeData> GetCustomAttributesForToken(EntityH
out CustomAttributeHandle filteredOutAttribute1,
AttributeDescription filterOut1)
{
return GetCustomAttributesForToken(token, out filteredOutAttribute1, filterOut1, out _, default, out _, default, out _, default);
return GetCustomAttributesForToken(token, out filteredOutAttribute1, filterOut1, out _, default, out _, default, out _, default, out _, default);
}

/// <summary>
Expand All @@ -318,12 +309,15 @@ internal ImmutableArray<CSharpAttributeData> GetCustomAttributesForToken(EntityH
out CustomAttributeHandle filteredOutAttribute3,
AttributeDescription filterOut3,
out CustomAttributeHandle filteredOutAttribute4,
AttributeDescription filterOut4)
AttributeDescription filterOut4,
out CustomAttributeHandle filteredOutAttribute5,
AttributeDescription filterOut5)
{
filteredOutAttribute1 = default;
filteredOutAttribute2 = default;
filteredOutAttribute3 = default;
filteredOutAttribute4 = default;
filteredOutAttribute5 = default;
ArrayBuilder<CSharpAttributeData> customAttributesBuilder = null;

try
Expand Down Expand Up @@ -357,6 +351,12 @@ internal ImmutableArray<CSharpAttributeData> GetCustomAttributesForToken(EntityH
continue;
}

if (matchesFilter(customAttributeHandle, filterOut5))
{
filteredOutAttribute5 = customAttributeHandle;
continue;
}

if (customAttributesBuilder == null)
{
customAttributesBuilder = ArrayBuilder<CSharpAttributeData>.GetInstance();
Expand Down Expand Up @@ -438,7 +438,8 @@ internal ImmutableArray<CSharpAttributeData> GetCustomAttributesFilterCompilerAt
filteredOutAttribute2: out CustomAttributeHandle isReadOnlyAttribute,
filterOut2: AttributeDescription.IsReadOnlyAttribute,
filteredOutAttribute3: out _, filterOut3: default,
filteredOutAttribute4: out _, filterOut4: default);
filteredOutAttribute4: out _, filterOut4: default,
filteredOutAttribute5: out _, filterOut5: default);

foundExtension = !extensionAttribute.IsNil;
foundReadOnly = !isReadOnlyAttribute.IsNil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,10 @@ public override ImmutableArray<CSharpAttributeData> GetAttributes()
IsReadOnly ? AttributeDescription.IsReadOnlyAttribute : default,
out _,
// Filter out [IsByRefLike]
IsRefLikeType ? AttributeDescription.IsByRefLikeAttribute : default);
IsRefLikeType ? AttributeDescription.IsByRefLikeAttribute : default,
out _,
// Filter out [CompilerFeatureRequired]
(IsRefLikeType && DeriveCompilerFeatureRequiredDiagnostic() is null) ? AttributeDescription.CompilerFeatureRequiredAttribute : default);

ImmutableInterlocked.InterlockedInitialize(ref uncommon.lazyCustomAttributes, loadedCustomAttributes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,8 @@ public override ImmutableArray<CSharpAttributeData> GetAttributes()
out isReadOnlyAttribute,
filterIsReadOnlyAttribute ? AttributeDescription.IsReadOnlyAttribute : default,
out _,
default,
out _,
default);

if (!paramArrayAttribute.IsNil || !constantAttribute.IsNil)
Expand Down
6 changes: 5 additions & 1 deletion src/Compilers/CSharp/Portable/Symbols/MethodSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1219,15 +1219,19 @@ protected static void AddRequiredMembersMarkerAttributes(ref ArrayBuilder<Synthe
var obsoleteData = methodToAttribute.ObsoleteAttributeData;
Debug.Assert(obsoleteData != ObsoleteAttributeData.Uninitialized, "getting synthesized attributes before attributes are decoded");

CSharpCompilation declaringCompilation = methodToAttribute.DeclaringCompilation;
if (obsoleteData == null)
{
CSharpCompilation declaringCompilation = methodToAttribute.DeclaringCompilation;
AddSynthesizedAttribute(ref attributes, declaringCompilation.TrySynthesizeAttribute(WellKnownMember.System_ObsoleteAttribute__ctor,
ImmutableArray.Create(
new TypedConstant(declaringCompilation.GetSpecialType(SpecialType.System_String), TypedConstantKind.Primitive, PEModule.RequiredMembersMarker), // message
new TypedConstant(declaringCompilation.GetSpecialType(SpecialType.System_Boolean), TypedConstantKind.Primitive, true)) // error
));
}

AddSynthesizedAttribute(ref attributes, declaringCompilation.TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerFeatureRequiredAttribute__ctor,
ImmutableArray.Create(new TypedConstant(declaringCompilation.GetSpecialType(SpecialType.System_String), TypedConstantKind.Primitive, nameof(CompilerFeatureRequiredFeatures.RequiredMembers)))
));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@

namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
// PROTOTYPE(req): Add obsolete marker to constructors if required members and Obsolete hasn't already been emitted
// PROTOTYPE(req): Add poison type marker to constructors if required members and Obsolete hasn't already been emitted,
// pending framework design review
internal sealed class SourceConstructorSymbol : SourceConstructorSymbolBase
{
private readonly bool _isExpressionBodied;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2428,6 +2428,12 @@ private void CheckForRequiredMemberAttribute(BindingDiagnosticBag diagnostics)
{
// Ensure that an error is reported if the required constructor isn't present.
_ = Binder.GetWellKnownTypeMember(DeclaringCompilation, WellKnownMember.System_Runtime_CompilerServices_RequiredMemberAttribute__ctor, diagnostics, Locations[0]);
}

if (HasAnyRequiredMembers)
{
_ = Binder.GetWellKnownTypeMember(DeclaringCompilation, WellKnownMember.System_Runtime_CompilerServices_CompilerFeatureRequiredAttribute__ctor, diagnostics, Locations[0]);

if (this.IsRecord)
{
// Copy constructors need to emit SetsRequiredMembers on the ctor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1557,17 +1557,24 @@ internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, r
var obsoleteData = ObsoleteAttributeData;
Debug.Assert(obsoleteData != ObsoleteAttributeData.Uninitialized, "getting synthesized attributes before attributes are decoded");

// If user specified an Obsolete attribute, we cannot emit ours.
// NB: we do not check the kind of deprecation.
// we will not emit Obsolete even if Deprecated or Experimental was used.
// we do not want to get into a scenario where different kinds of deprecation are combined together.
//
if (obsoleteData == null && !this.IsRestrictedType(ignoreSpanLikeTypes: true))
if (!this.IsRestrictedType(ignoreSpanLikeTypes: true))
{
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(WellKnownMember.System_ObsoleteAttribute__ctor,
ImmutableArray.Create(
new TypedConstant(compilation.GetSpecialType(SpecialType.System_String), TypedConstantKind.Primitive, PEModule.ByRefLikeMarker), // message
new TypedConstant(compilation.GetSpecialType(SpecialType.System_Boolean), TypedConstantKind.Primitive, true)), // error=true
// If user specified an Obsolete attribute, we cannot emit ours.
// NB: we do not check the kind of deprecation.
// we will not emit Obsolete even if Deprecated or Experimental was used.
// we do not want to get into a scenario where different kinds of deprecation are combined together.
//
if (obsoleteData == null)
{
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(WellKnownMember.System_ObsoleteAttribute__ctor,
ImmutableArray.Create(
new TypedConstant(compilation.GetSpecialType(SpecialType.System_String), TypedConstantKind.Primitive, PEModule.ByRefLikeMarker), // message
new TypedConstant(compilation.GetSpecialType(SpecialType.System_Boolean), TypedConstantKind.Primitive, true)), // error=true
isOptionalUse: true));
}

AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerFeatureRequiredAttribute__ctor,
ImmutableArray.Create(new TypedConstant(compilation.GetSpecialType(SpecialType.System_String), TypedConstantKind.Primitive, nameof(CompilerFeatureRequiredFeatures.RefStructs))),
isOptionalUse: true));
}
}
Expand Down
Loading