Skip to content

Commit

Permalink
Add CompilerFeatureRequiredAttribute (#67924)
Browse files Browse the repository at this point in the history
Closes #66167.

Co-authored-by: Stephen Toub <stoub@microsoft.com>
  • Loading branch information
333fred and stephentoub committed Apr 14, 2022
1 parent e5eee9a commit fdd104e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/libraries/System.Private.CoreLib/src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The goal is to have the majority of code located in this folder, as that code is

### File Naming Convention

Any runtime-specific `partial` part which also has a shared part sholud use a runtime-specific file name suffix to ease the navigation.
Any runtime-specific `partial` part which also has a shared part should use a runtime-specific file name suffix to ease the navigation.

* `*.CoreCLR.cs` for CoreCLR runtime
* `*.Mono.cs` for Mono runtime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\CallingConventions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\CompilationRelaxations.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\CompilationRelaxationsAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\CompilerFeatureRequiredAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\CompilerGeneratedAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\CompilerGlobalScopeAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\ConditionalWeakTable.cs" />
Expand Down Expand Up @@ -2411,4 +2412,4 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IUnaryPlusOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IUnsignedNumber.cs" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Runtime.CompilerServices
{
/// <summary>
/// Indicates that compiler support for a particular feature is required for the location where this attribute is applied.
/// </summary>
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)]
public sealed class CompilerFeatureRequiredAttribute : Attribute
{
public CompilerFeatureRequiredAttribute(string featureName)
{
FeatureName = featureName;
}

/// <summary>
/// The name of the compiler feature.
/// </summary>
public string FeatureName { get; }

/// <summary>
/// If true, the compiler can choose to allow access to the location where this attribute is applied if it does not understand <see cref="FeatureName"/>.
/// </summary>
public bool IsOptional { get; init; }

/// <summary>
/// The <see cref="FeatureName"/> used for the ref structs C# feature.
/// </summary>
public const string RefStructs = nameof(RefStructs);

/// <summary>
/// The <see cref="FeatureName"/> used for the required members C# feature.
/// </summary>
public const string RequiredMembers = nameof(RequiredMembers);
}
}
9 changes: 9 additions & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11275,6 +11275,15 @@ public CompilationRelaxationsAttribute(int relaxations) { }
public CompilationRelaxationsAttribute(System.Runtime.CompilerServices.CompilationRelaxations relaxations) { }
public int CompilationRelaxations { get { throw null; } }
}
[System.AttributeUsageAttribute(System.AttributeTargets.All, AllowMultiple=true, Inherited=false)]
public sealed partial class CompilerFeatureRequiredAttribute : System.Attribute
{
public const string RefStructs = "RefStructs";
public const string RequiredMembers = "RequiredMembers";
public CompilerFeatureRequiredAttribute(string featureName) { }
public string FeatureName { get { throw null; } }
public bool IsOptional { get { throw null; } init { } }
}
[System.AttributeUsageAttribute(System.AttributeTargets.All, Inherited=true)]
public sealed partial class CompilerGeneratedAttribute : System.Attribute
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,5 +326,17 @@ public static void RequiredMemberAttributeTests()
{
new RequiredMemberAttribute();
}

[Fact]
public static void CompilerFeatureRequiredTests()
{
var attr1 = new CompilerFeatureRequiredAttribute("feature1");
Assert.Equal("feature1", attr1.FeatureName);
Assert.False(attr1.IsOptional);

var attr2 = new CompilerFeatureRequiredAttribute("feature2") { IsOptional = true };
Assert.Equal("feature2", attr2.FeatureName);
Assert.True(attr2.IsOptional);
}
}
}

0 comments on commit fdd104e

Please sign in to comment.