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

VB SymbolDisplayVisitor.AddMethodName throws InvalidOperationException when formatting C# destructor ISymbol #77459

Open
Nukepayload2 opened this issue Mar 7, 2025 · 0 comments
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@Nukepayload2
Copy link

I use Microsoft.CodeAnalysis.VisualBasic.SymbolDisplay.ToDisplayString to generate VB declarations from dll files and xml files in my API docs. I found that Microsoft.CodeAnalysis.VisualBasic.SymbolDisplayVisitor.AddMethodName throws System.InvalidOperationException: Unexpected value 'Destructor' of type 'Microsoft.CodeAnalysis.MethodKind' when converting C# destructor ISymbol to string by calling SymbolDisplay.ToDisplayString.

Version Used:
4.13.0

Steps to Reproduce:

Create a VB console project

VbSymbolDisplayCsOptional.vbproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <RootNamespace>VbSymbolDisplayFinalize</RootNamespace>
    <TargetFramework>net9.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.13.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.VisualBasic" Version="4.13.0" />
  </ItemGroup>

</Project>

Edit Program.vb

Program.vb

Imports System.IO
Imports Microsoft.CodeAnalysis
Imports CS = Microsoft.CodeAnalysis.CSharp
Imports VB = Microsoft.CodeAnalysis.VisualBasic

Module Program

    Sub Main()
        ' Create C# code that overrides the Finalize method
        Dim csharpCode As String = "
using System;

public class Example
{
    ~Example()
    {
    }
}"

        ' Compile the C# code
        Dim compilation = CreateCompilationFromCSharpCode(csharpCode, "FinalizeExample")

        ' Get the symbol for the Finalize method
        Dim exampleClass = compilation.GetTypeByMetadataName("Example")
        Dim methodSymbol = exampleClass.GetMembers().OfType(Of IMethodSymbol)().First()

        ' Create a VB symbol display format
        Dim format = SymbolDisplayFormat.FullyQualifiedFormat

        ' Display the symbol using VB SymbolDisplay
        Dim formattedSymbol = VB.SymbolDisplay.ToDisplayString(methodSymbol, format)
        Console.WriteLine("Method signature in VB format:")
        Console.WriteLine(formattedSymbol)
    End Sub

    Public Function CreateCompilationFromCSharpCode(
            code As String,
            name As String,
            ParamArray references As MetadataReference()) As Compilation

        Dim parserOption As New CS.CSharpParseOptions
        Dim syntaxTree = CS.CSharpSyntaxTree.ParseText(code, parserOption)

        Return CS.CSharpCompilation.Create(
            name,
            options:=New CS.CSharpCompilationOptions(
                OutputKind.DynamicallyLinkedLibrary,
                xmlReferenceResolver:=XmlFileResolver.Default),
            syntaxTrees:={syntaxTree},
            references:=GetDefaultMetadataReferences().
            Concat(If(references, Array.Empty(Of MetadataReference))))
    End Function

    Private Function GetDefaultMetadataReferences() As IEnumerable(Of MetadataReference)
        Dim dirPath = Path.GetDirectoryName(GetType(Object).Assembly.Location)

        Return From dll In Directory.EnumerateFiles(dirPath, "*.dll", SearchOption.TopDirectoryOnly)
               Select MetadataReference.CreateFromFile(dll)
    End Function

End Module

Run the project.

Diagnostic Id:

N/A

Expected Behavior:
The program produces the following output:

Method signature in VB format:
Protected Overrides Sub Finalize()

Actual Behavior:
The program produces the following output and crashed:

Unhandled exception. System.InvalidOperationException: Unexpected value 'Destructor' of type 'Microsoft.CodeAnalysis.MethodKind'
   at Microsoft.CodeAnalysis.VisualBasic.SymbolDisplayVisitor.AddMethodName(IMethodSymbol symbol)
   at Microsoft.CodeAnalysis.VisualBasic.SymbolDisplayVisitor.VisitMethod(IMethodSymbol symbol)
   at Microsoft.CodeAnalysis.CSharp.Symbols.PublicModel.MethodSymbol.Accept(SymbolVisitor visitor)
   at Microsoft.CodeAnalysis.CSharp.Symbols.PublicModel.Symbol.Microsoft.CodeAnalysis.ISymbol.Accept(SymbolVisitor visitor)
   at Microsoft.CodeAnalysis.VisualBasic.SymbolDisplay.PopulateDisplayParts(ArrayBuilder`1 builder, ISymbol symbol, SemanticModel semanticModelOpt, Int32 positionOpt, SymbolDisplayFormat format, Boolean minimal)
   at Microsoft.CodeAnalysis.VisualBasic.SymbolDisplay.ToDisplayString(ISymbol symbol, SemanticModel semanticModel, Int32 position, SymbolDisplayFormat format, Boolean minimal)
   at Microsoft.CodeAnalysis.VisualBasic.SymbolDisplay.ToDisplayString(ISymbol symbol, SymbolDisplayFormat format)
   at VbSymbolDisplayFinalize.Program.Main()
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Mar 7, 2025
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

No branches or pull requests

1 participant