-
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
Allow users to define EmbeddedAttribute #76523
base: main
Are you sure you want to change the base?
Conversation
…ser-defined-embedded * origin/source-gen-warning: Additional testing Add tests
@dotnet/roslyn-compiler for review. I'll propose a source-generation API to allow authors to create this API easily, but this change can go in separately. |
src/Compilers/CSharp/Portable/Symbols/Source/SourceNamedTypeSymbol.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Test/Emit3/Attributes/AttributeTests_Embedded.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Symbols/Source/SourceNamedTypeSymbol.cs
Outdated
Show resolved
Hide resolved
Done with review pass (commit 8) |
…tself didn't have one applied to it.
@AlekseyTs I believe I've address your feedback, thanks! |
|
||
***Introduced in Visual Studio 2022 version 17.13*** | ||
|
||
The compiler now validates the shape of `Microsoft.CodeAnalysis.EmbeddedAttribute` when declared in source. Previously, the compiler |
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.
Unrelated to the PR: it's not clear to me what the Microsoft.CodeAnalysis.EmbeddedAttribute
does. Is that documented somewhere?
***Introduced in Visual Studio 2022 version 17.13*** | ||
|
||
The compiler now validates the shape of `Microsoft.CodeAnalysis.EmbeddedAttribute` when declared in source. Previously, the compiler | ||
would allow user-defined declarations of this attribute, but only when it didn't need to generate one itself. We now validate that: |
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.
7. It must be allowed on any type declaration (class, struct, interface, enum, or delegate) | ||
|
||
```cs | ||
namespace Microsoft.CodeAnalysis; |
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.
Are we supposed to use VB in this document?
|
||
if (this.IsMicrosoftCodeAnalysisEmbeddedAttribute() && GetEarlyDecodedWellKnownAttributeData() is null or { HasCodeAnalysisEmbeddedAttribute: false }) | ||
{ | ||
// This is Microsoft.CodeAnalysis.EmbeddedAttribute, and the user didn't manually apply this attribute to itself. Grab the parameterless constructor |
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.
// 7. It must be allowed on any type declaration (class, struct, interface, enum, or delegate) | ||
// 8. It must be non-generic (checked as part of IsMicrosoftCodeAnalysisEmbeddedAttribute, we don't error on this because both types can exist) | ||
|
||
const AttributeTargets ExpectedTargets = AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface | AttributeTargets.Enum | AttributeTargets.Delegate; |
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.
{ | ||
public void M(in int p) | ||
{ | ||
// This should trigger generating another EmbeddedAttribute |
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.
@@ -60,6 +60,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols | |||
ReportedVarianceDiagnostics = &H2 ' Set if variance diagnostics have been reported. | |||
ReportedBaseClassConstraintsDiagnostics = &H4 ' Set if base class constraints diagnostics have been reported. | |||
ReportedInterfacesConstraintsDiagnostics = &H8 ' Set if constraints diagnostics for base/implemented interfaces have been reported. | |||
ReportedCodeAnalysisEmbeddedAttributeDiagnostics = &H10 ' Set if |
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.
' 7. It must be allowed on any type declaration (class, struct, interface, enum, Or delegate) | ||
' 8. It must be non-generic | ||
|
||
Dim expectedTargets = AttributeTargets.Class Or AttributeTargets.Struct Or AttributeTargets.Interface Or AttributeTargets.Enum Or AttributeTargets.Delegate |
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.
@@ -20,7 +22,8 @@ public void ReferencingEmbeddedAttributesFromTheSameAssemblySucceeds() | |||
var code = @" | |||
namespace Microsoft.CodeAnalysis | |||
{ | |||
internal class EmbeddedAttribute : System.Attribute { } | |||
[Embedded] |
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.
{ | ||
public void M(in int p) | ||
{ | ||
// This should trigger generating another EmbeddedAttribute |
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.
@@ -1748,6 +1752,21 @@ internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, r | |||
ImmutableArray.Create(new TypedConstant(compilation.GetWellKnownType(WellKnownType.System_Type), TypedConstantKind.Type, originalType)), | |||
isOptionalUse: true)); | |||
} | |||
|
|||
if (this.IsMicrosoftCodeAnalysisEmbeddedAttribute() && GetEarlyDecodedWellKnownAttributeData() is null or { HasCodeAnalysisEmbeddedAttribute: false }) |
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.
If Me.IsMicrosoftCodeAnalysisEmbeddedAttribute() Then | ||
Dim earlyAttributeData = GetEarlyDecodedWellKnownAttributeData() | ||
|
||
If earlyAttributeData Is Nothing OrElse Not earlyAttributeData.HasCodeAnalysisEmbeddedAttribute Then |
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.
Done with review pass (commit 10) |
Alternative approach to #71546. Today, our enforcement of whether to allow users to define
Microsoft.CodeAnalysis.EmbeddedAttribute
is inconsistent; if we need to generate it, we error if the user defines one. But if the we don't need to generate it (as we increasingly do not since .NET 6), we allow the user to define their own version. This PR standardizes our enforcement, and enables the compiler to use the user-declared attribute instead generating one. We also document the breaking change from the standardized enforcement.