Skip to content

Commit

Permalink
Allow usage of ref structs in VB despite CompilerFeatureRequired (#65431
Browse files Browse the repository at this point in the history
)

Fixes #65392
  • Loading branch information
jcouv authored Nov 17, 2022
1 parent c72ce32 commit dcd1063
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols.Metadata.PE
Private Function DeriveCompilerFeatureRequiredDiagnostic() As DiagnosticInfo
Dim decoder = New MetadataDecoder(ContainingPEModule, Me)

Dim diagnostic = DeriveCompilerFeatureRequiredAttributeDiagnostic(Me, ContainingPEModule, Handle, CompilerFeatureRequiredFeatures.None, decoder)
Dim diagnostic = DeriveCompilerFeatureRequiredAttributeDiagnostic(Me, ContainingPEModule, Handle, CompilerFeatureRequiredFeatures.RefStructs, decoder)

If diagnostic IsNot Nothing Then
Return diagnostic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<ProjectReference Include="..\..\..\Test\Resources\Core\Microsoft.CodeAnalysis.Compiler.Test.Resources.csproj" />
<ProjectReference Include="..\..\..\Test\Utilities\VisualBasic\Microsoft.CodeAnalysis.VisualBasic.Test.Utilities.vbproj" />
<ProjectReference Include="..\..\Portable\Microsoft.CodeAnalysis.VisualBasic.vbproj" />
<PackageReference Include="Basic.Reference.Assemblies.Net60" Version="$(BasicReferenceAssembliesNet60Version)" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="$(SystemDrawingCommonVersion)" Condition="$(TargetFramework) == 'net7.0'" />
Expand Down
32 changes: 28 additions & 4 deletions src/Compilers/VisualBasic/Test/Semantic/Semantics/RefFieldTests.vb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests
''' <summary>
''' Ref field in ref struct.
''' </summary>
<Fact>
<Fact, WorkItem(65392, "https://github.com/dotnet/roslyn/issues/65392")>
Public Sub RefField_01()
Dim sourceA =
"public ref struct S<T>
Expand All @@ -42,9 +42,6 @@ End Module"
BC30668: 'S(Of Integer)' is obsolete: 'Types with embedded references are not supported in this version of your compiler.'.
Dim s = New S(Of Integer)()
~~~~~~~~~~~~~
BC37319: 'S(Of T)' requires compiler feature 'RefStructs', which is not supported by this version of the Visual Basic compiler.
Dim s = New S(Of Integer)()
~~~~~~~~~~~~~
BC30656: Field 'F' is of an unsupported type.
Console.WriteLine(s.F)
~~~
Expand All @@ -56,6 +53,33 @@ BC30656: Field 'F' is of an unsupported type.
Assert.True(field.HasUnsupportedMetadata)
End Sub

<Fact, WorkItem(65392, "https://github.com/dotnet/roslyn/issues/65392")>
Public Sub CanUsePassThroughRefStructInstances()
Dim source =
"Imports System
Module Program
Sub Main()
Dim s = ""123"".AsSpan()
Dim s2 As ReadOnlySpan(Of Char) = ""123"".AsSpan()
Dim s3 = MemoryExtensions.AsSpan(""123"")
End Sub
End Module"

Dim comp = CreateCompilation(source, targetFramework:=TargetFramework.Net60)
comp.AssertTheseDiagnostics(<expected>
BC30668: 'ReadOnlySpan(Of Char)' is obsolete: 'Types with embedded references are not supported in this version of your compiler.'.
Dim s2 As ReadOnlySpan(Of Char) = "123".AsSpan()
~~~~~~~~~~~~~~~~~~~~~
</expected>)

comp = CreateCompilation(source, targetFramework:=TargetFramework.Net70)
comp.AssertTheseDiagnostics(<expected>
BC30668: 'ReadOnlySpan(Of Char)' is obsolete: 'Types with embedded references are not supported in this version of your compiler.'.
Dim s2 As ReadOnlySpan(Of Char) = "123".AsSpan()
~~~~~~~~~~~~~~~~~~~~~
</expected>)
End Sub

''' <summary>
''' Ref field in class.
''' </summary>
Expand Down

0 comments on commit dcd1063

Please sign in to comment.