Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

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

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

Choose a reason for hiding this comment

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

compilerFeatureRequiredDiagnostic Is Nothing

Do we have a test observing significance of this condition?

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

If(ObsoleteAttributeData Is Nothing, AttributeDescription.ObsoleteAttribute, Nothing)

Does this filter out explicit [Obsolete] attributes as well (for a constructor on a type with a required member that has been deprecated)?

Copy link
Member Author

Choose a reason for hiding this comment

The 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -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>
Expand All @@ -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)
Copy link
Member Author

Choose a reason for hiding this comment

The 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 SetsRequiredMembers in it, which was then giving missing symbol text in ToTestDisplayString().

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
Expand Down Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -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>
Expand All @@ -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>
Expand Down Expand Up @@ -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)

Expand 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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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