roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

Interface-Method: IsSealed does not work as expected

Open bernd5 opened this issue 1 year ago • 1 comments

Version Used:

Steps to Reproduce:

Execute:

using System;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;


var tree = ParseSyntaxTree("""
using System;
						   
interface IFoo
{
    sealed void SealedMethod()
    {
        Console.WriteLine("IFoo.SealedMethod");
    }    
}					  
""");

var refApis = AppDomain.CurrentDomain.GetAssemblies()
    .Where(a => !a.IsDynamic)
    .Select(a => MetadataReference.CreateFromFile(a.Location));

var compilation = CSharpCompilation.Create("something", new[] { tree }, refApis);
compilation = compilation.WithOptions(compilation.Options.WithOutputKind(OutputKind.DynamicallyLinkedLibrary));
var method = compilation.SyntaxTrees.First().GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().Single();

var symbol = compilation.GetSemanticModel(method.SyntaxTree).GetDeclaredSymbol(method);
Console.WriteLine(symbol.IsSealed); //<-- returns false

Expected Behavior: Expected output: True

Actual Behavior: Prints: False

Is there any API to get the "correct" IsSealed value? The "Sealed" value seems to be encoded with the newslot flag - but that detail should not be part of the public API, or?

bernd5 avatar May 12 '24 22:05 bernd5

Interesting. This might be a bug. Compiler team would have to check. In the meantime, you can check for this syntactically on the method decl node you searched for.

CyrusNajmabadi avatar May 12 '24 23:05 CyrusNajmabadi

@333fred can you confirm whether or not this behavior is intended?

jaredpar avatar May 20 '24 21:05 jaredpar

@AlekseyTs would be better to comment here.

333fred avatar May 20 '24 21:05 333fred

However, I'm guessing this is intentional, as we don't emit final into the IL here.

333fred avatar May 20 '24 21:05 333fred

This is by design. For interface members, sealed modifier means not-virtual.

AlekseyTs avatar May 20 '24 22:05 AlekseyTs