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

Allow users to define EmbeddedAttribute #76523

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

333fred
Copy link
Member

@333fred 333fred commented Dec 19, 2024

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.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Dec 19, 2024
@333fred 333fred marked this pull request as ready for review December 19, 2024 20:18
@333fred 333fred requested a review from a team as a code owner December 19, 2024 20:18
@333fred
Copy link
Member Author

333fred commented Dec 19, 2024

@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. For VB, there are existing tests verifying that it respects the EmbeddedAttribute definition, and allows defining in the same assembly. I opted not to enforce any shape on the VB as it doesn't do any form of validation in any scenario for the attribute definition today. We now validate the VB side, because we will synthesize an EmbeddedAttribute application for the type. This has also been documented as a breaking change.

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 8)

@333fred
Copy link
Member Author

333fred commented Dec 21, 2024

@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
Copy link
Member

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:
Copy link
Contributor

Choose a reason for hiding this comment

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

, but only when it didn't need to generate one itself

", but only when it didn't need to generate one itself" Is this part accurate for VB?

7. It must be allowed on any type declaration (class, struct, interface, enum, or delegate)

```cs
namespace Microsoft.CodeAnalysis;
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

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

// This is Microsoft.CodeAnalysis.EmbeddedAttribute

Consider adding an assert to verify the fact that "This is Microsoft.CodeAnalysis.EmbeddedAttribute"

// 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;
Copy link
Contributor

Choose a reason for hiding this comment

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

ExpectedTargets

It looks like the code is not following naming conventions for locals. Despite the const modifier, this is a local.

{
public void M(in int p)
{
// This should trigger generating another EmbeddedAttribute
Copy link
Contributor

Choose a reason for hiding this comment

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

// This should trigger generating another EmbeddedAttribute

Is this accurate?

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

' Set if

It looks like the comment is not finished

' 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
Copy link
Contributor

Choose a reason for hiding this comment

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

Dim

Const?

@@ -20,7 +22,8 @@ public void ReferencingEmbeddedAttributesFromTheSameAssemblySucceeds()
var code = @"
namespace Microsoft.CodeAnalysis
{
internal class EmbeddedAttribute : System.Attribute { }
[Embedded]
Copy link
Contributor

Choose a reason for hiding this comment

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

[Embedded]

It looks like this change (the attribute application) is no longer necessary here and in other similar places.

{
public void M(in int p)
{
// This should trigger generating another EmbeddedAttribute
Copy link
Contributor

Choose a reason for hiding this comment

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

// This should trigger generating another EmbeddedAttribute

Is this accurate?

@@ -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 })
Copy link
Contributor

Choose a reason for hiding this comment

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

&& GetEarlyDecodedWellKnownAttributeData() is null or { HasCodeAnalysisEmbeddedAttribute: false }

Are we testing positive and negative effect of this condition?

If Me.IsMicrosoftCodeAnalysisEmbeddedAttribute() Then
Dim earlyAttributeData = GetEarlyDecodedWellKnownAttributeData()

If earlyAttributeData Is Nothing OrElse Not earlyAttributeData.HasCodeAnalysisEmbeddedAttribute Then
Copy link
Contributor

Choose a reason for hiding this comment

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

earlyAttributeData Is Nothing OrElse Not earlyAttributeData.HasCodeAnalysisEmbeddedAttribute

Are we testing positive and negative effect of this condition?

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 10)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants