dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.83k stars 4k forks source link

A delegate type synthesized by compiler can refer to an inaccessible type #74312

Open AlekseyTs opened 1 month ago

AlekseyTs commented 1 month ago
unsafe class C
{
    static void Main()
    {
        var d = (S* x) => x;
    }

    private struct S
    {}
}

class B {}

class D : B, System.IDisposable
{
    public void Dispose() {}
}

IL

.class private auto ansi sealed '<>f__AnonymousDelegate0'
    extends [System.Runtime]System.MulticastDelegate
{
    .custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
        01 00 00 00
    )
    // Methods
    .method public hidebysig specialname rtspecialname 
        instance void .ctor (
            object 'object',
            native int 'method'
        ) runtime managed 
    {
    } // end of method '<>f__AnonymousDelegate0'::.ctor

    .method public hidebysig newslot virtual 
        instance valuetype C/S* Invoke (
            valuetype C/S* arg
        ) runtime managed 
    {
    } // end of method '<>f__AnonymousDelegate0'::Invoke

} // end of class <>f__AnonymousDelegate0

Note that signature of Invoke refers to a private type S.

AlekseyTs commented 1 month ago

@cston Is the current behavior a result of an explicit decision?

cston commented 1 month ago

@cston Is the current behavior a result of an explicit decision?

No, this is a bug.

jaredpar commented 1 month ago

How serious is this bug? Does the code still execute but is just unveriable?

AlekseyTs commented 1 month ago

How serious is this bug? Does the code still execute but is just unveriable?

I think our runtime doesn't enforce accessibility during execution. The delegate can be invoked.

jaredpar commented 1 month ago

Okay. Putting this on the MQ Candidate list then.