-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Filter out CompilerFeatureRequired and Obsolete attribute from method symbols #78313
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
Changes from 1 commit
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 |
|---|---|---|
|
|
@@ -641,7 +641,26 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols.Metadata.PE | |
| If Not _packedFlags.IsCustomAttributesPopulated Then | ||
| Dim attributeData As ImmutableArray(Of VisualBasicAttributeData) = Nothing | ||
| Dim containingPEModuleSymbol = DirectCast(ContainingModule(), PEModuleSymbol) | ||
| containingPEModuleSymbol.LoadCustomAttributes(Me.Handle, attributeData) | ||
| Dim checkForRequiredMembers = MethodKind = MethodKind.Constructor AndAlso | ||
| HasSetsRequiredMembers = False AndAlso | ||
| (Me.ContainingType.HasAnyDeclaredRequiredMembers OrElse Not Me.ContainingType.AllRequiredMembers.IsEmpty) | ||
|
|
||
| If checkForRequiredMembers Then | ||
| Dim compilerFeatureRequiredDiagnostic As DiagnosticInfo = Nothing | ||
| DeriveCompilerFeatureRequiredUseSiteInfo(compilerFeatureRequiredDiagnostic) | ||
|
|
||
| Dim discard1 As CustomAttributeHandle = Nothing | ||
| Dim discard2 As CustomAttributeHandle = Nothing | ||
| attributeData = containingPEModuleSymbol.GetCustomAttributesForToken( | ||
| Me.Handle, | ||
| filteredOutAttribute1:=discard1, | ||
| filterOut1:=If(compilerFeatureRequiredDiagnostic Is Nothing, AttributeDescription.CompilerFeatureRequiredAttribute, Nothing), | ||
|
Contributor
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.
Member
Author
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. No, I didn't cook up some invalid IL test here. If you'd prefer that I do so, I can.
Contributor
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 assume we have a test for C#, it feels like it shouldn't be very hard to port it.
Member
Author
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 didn't, but now we do. |
||
| filteredOutAttribute2:=discard2, | ||
| filterOut2:=If(ObsoleteAttributeData Is Nothing, AttributeDescription.ObsoleteAttribute, Nothing)) | ||
|
Contributor
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.
Member
Author
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. |
||
|
|
||
| Else | ||
| containingPEModuleSymbol.LoadCustomAttributes(Me.Handle, attributeData) | ||
| End If | ||
| Debug.Assert(Not attributeData.IsDefault) | ||
| If Not attributeData.IsEmpty Then | ||
| attributeData = InterlockedOperations.Initialize(AccessUncommonFields()._lazyCustomAttributes, attributeData) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -255,6 +255,10 @@ BC37321: Required member 'Public Overloads Property Prop As Integer' must be set | |
| Dim t <%= constructor %> | ||
| ~ | ||
| </expected>) | ||
|
|
||
| Dim c = comp.GetTypeByMetadataName("C") | ||
| Dim ctor = c.Constructors.Single() | ||
| Assert.Empty(ctor.GetAttributes()) | ||
| End Sub | ||
|
|
||
| <Theory> | ||
|
|
@@ -275,6 +279,10 @@ BC37321: Required member 'Public Field As Integer' must be set in the object ini | |
| Dim t <%= constructor %> With { .Prop = 1 } | ||
| ~ | ||
| </expected>) | ||
|
|
||
| Dim c = comp.GetTypeByMetadataName("C") | ||
| Dim ctor = c.Constructors.Single() | ||
| Assert.Empty(ctor.GetAttributes()) | ||
| End Sub | ||
|
|
||
| <Theory> | ||
|
|
@@ -292,6 +300,10 @@ End Module" | |
|
|
||
| Dim comp = CreateCompilation(vbCode, {cComp.EmitToImageReference()}) | ||
| comp.AssertNoDiagnostics() | ||
|
|
||
| Dim c = comp.GetTypeByMetadataName("C") | ||
| Dim ctor = c.Constructors.Single() | ||
| Assert.Empty(ctor.GetAttributes()) | ||
| End Sub | ||
|
|
||
| <Theory> | ||
|
|
@@ -306,8 +318,12 @@ Module M | |
| End Sub | ||
| End Module" | ||
|
|
||
| Dim comp = CreateCompilation(vbCode, {cComp.EmitToImageReference()}) | ||
| comp.AssertNoDiagnostics | ||
| Dim comp = CreateCompilation(vbCode, {cComp.EmitToImageReference()}, targetFramework:=TargetFramework.Net70) | ||
|
Member
Author
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. Turns out some of these compilations were not targeting a version of .NET with |
||
| comp.AssertNoDiagnostics() | ||
|
|
||
| Dim c = comp.GetTypeByMetadataName("C") | ||
| Dim ctor = c.Constructors.Single() | ||
| AssertEx.Equal("System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute", ctor.GetAttributes().Single().AttributeClass.ToTestDisplayString()) | ||
| End Sub | ||
|
|
||
| Private Shared Function GetBaseDerivedDefinition(hasSetsRequiredMembers As Boolean) As String | ||
|
|
@@ -363,6 +379,10 @@ BC37321: Required member 'Public Overloads Property Prop2 As Integer' must be se | |
| Dim t <%= constructor %> | ||
| ~~~~~~~~~~~~~~ | ||
| </expected>) | ||
|
|
||
| Dim dd = comp.GetTypeByMetadataName("DerivedDerived") | ||
| Dim ctor = dd.Constructors.Single() | ||
| Assert.Empty(ctor.GetAttributes()) | ||
| End Sub | ||
|
|
||
| <Theory> | ||
|
|
@@ -386,6 +406,10 @@ BC37321: Required member 'Public Overloads Property Prop2 As Integer' must be se | |
| Dim t <%= constructor %> With { .Prop1 = 1, .Field2 = 2 } | ||
| ~~~~~~~~~~~~~~ | ||
| </expected>) | ||
|
|
||
| Dim dd = comp.GetTypeByMetadataName("DerivedDerived") | ||
| Dim ctor = dd.Constructors.Single() | ||
| Assert.Empty(ctor.GetAttributes()) | ||
| End Sub | ||
|
|
||
| <Theory> | ||
|
|
@@ -402,6 +426,10 @@ End Module" | |
|
|
||
| Dim comp = CreateCompilation(vbCode, {cComp.EmitToImageReference()}) | ||
| comp.AssertNoDiagnostics() | ||
|
|
||
| Dim dd = comp.GetTypeByMetadataName("DerivedDerived") | ||
| Dim ctor = dd.Constructors.Single() | ||
| Assert.Empty(ctor.GetAttributes()) | ||
| End Sub | ||
|
|
||
| <Theory> | ||
|
|
@@ -416,8 +444,12 @@ Module M | |
| End Sub | ||
| End Module" | ||
|
|
||
| Dim comp = CreateCompilation(vbCode, {cComp.EmitToImageReference()}) | ||
| Dim comp = CreateCompilation(vbCode, {cComp.EmitToImageReference()}, targetFramework:=TargetFramework.Net70) | ||
| comp.AssertNoDiagnostics() | ||
|
|
||
| Dim dd = comp.GetTypeByMetadataName("DerivedDerived") | ||
| Dim ctor = dd.Constructors.Single() | ||
| AssertEx.Equal("System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute", ctor.GetAttributes().Single().AttributeClass.ToTestDisplayString()) | ||
| End Sub | ||
|
|
||
| <Fact> | ||
|
|
@@ -485,7 +517,7 @@ End Module", {originalBasic.ToMetadataReference(), retargetedC.EmitToImageRefere | |
| Dim originalBasic = CreateCompilation(" | ||
| Public Class Base | ||
| Public Property C As C | ||
| End Class", {originalC.EmitToImageReference()}) | ||
| End Class", {originalC.EmitToImageReference()}, targetFramework:=TargetFramework.Net70) | ||
|
|
||
| Dim retargetedC = CreateCSharpCompilation(New AssemblyIdentity("Ret", New Version(2, 0, 0, 0), isRetargetable:=True), retargetedCode, referencedAssemblies:=Basic.Reference.Assemblies.Net70.References.All) | ||
|
|
||
|
|
@@ -494,7 +526,7 @@ Module M | |
| Public Sub Main() | ||
| Dim b As New Base() With { .C = New C() } | ||
| End Sub | ||
| End Module", {originalBasic.ToMetadataReference(), retargetedC.EmitToImageReference()}) | ||
| End Module", {originalBasic.ToMetadataReference(), retargetedC.EmitToImageReference()}, targetFramework:=TargetFramework.Net70) | ||
|
|
||
| comp.AssertNoDiagnostics() | ||
| End Sub | ||
|
|
@@ -670,7 +702,7 @@ Module M | |
| End Sub | ||
| End Module" | ||
|
|
||
| Dim comp = CreateCompilation(vbCode, {cComp.EmitToImageReference()}) | ||
| Dim comp = CreateCompilation(vbCode, {cComp.EmitToImageReference()}, targetFramework:=TargetFramework.Net70) | ||
| comp.AssertNoDiagnostics() | ||
| End Sub | ||
|
|
||
|
|
@@ -1598,7 +1630,7 @@ Module M | |
| End Sub | ||
| End Module" | ||
|
|
||
| Dim comp = CreateCompilation(vbCode, {cComp.EmitToImageReference()}) | ||
| Dim comp = CreateCompilation(vbCode, {cComp.EmitToImageReference()}, targetFramework:=TargetFramework.Net70) | ||
| comp.AssertNoDiagnostics() | ||
| End Sub | ||
|
|
||
|
|
@@ -2475,5 +2507,23 @@ BC37321: Required member 'Public Overloads Property P1 As Integer' must be set i | |
| ~~ | ||
| </expected>) | ||
| End Sub | ||
|
|
||
| <Fact> | ||
| Public Sub ObsoleteConstructorNotFilteredOut() | ||
| Dim csharpComp = CreateCSharpCompilationWithRequiredMembers(" | ||
| public class C1 | ||
| { | ||
| public required int P1 {get;set;} | ||
| [System.Obsolete(""Really obsolete"", true)] | ||
| public C1() { } | ||
| } | ||
| ") | ||
|
|
||
| Dim comp = CreateCompilation("", references:={csharpComp.EmitToImageReference()}, targetFramework:=TargetFramework.Net70) | ||
|
|
||
| Dim c1 = comp.GetTypeByMetadataName("C1") | ||
| Dim c1Constructor = c1.InstanceConstructors.Single() | ||
| AssertEx.Equal("System.ObsoleteAttribute", c1Constructor.GetAttributes().Single().AttributeClass.ToTestDisplayString()) | ||
| End Sub | ||
| End Class | ||
| End Namespace | ||
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.
See https://github.com/dotnet/roslyn/blob/main/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEMethodSymbol.cs#L1012-L1030 for the equivalent C# version of this. I based this logic on the current C# code.