Skip to content

Invalid IL is produced for ref returning static auto property in an interface #49341

@AlekseyTs

Description

@AlekseyTs
interface IA
{
    static ref int P { get;}
    
    static void Main()
    {}
}

IL:

.class interface private auto ansi abstract IA
{
    // Fields
    .field private static initonly int32 '<P>k__BackingField'
    .custom instance void [System.Private.CoreLib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
        01 00 00 00
    )
    .custom instance void [System.Private.CoreLib]System.Diagnostics.DebuggerBrowsableAttribute::.ctor(valuetype [System.Private.CoreLib]System.Diagnostics.DebuggerBrowsableState) = (
        01 00 00 00 00 00 00 00
    )

    // Methods
    .method public hidebysig specialname static 
        int32& get_P () cil managed 
    {
        .custom instance void [System.Private.CoreLib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
            01 00 00 00
        )
        // Method begins at RVA 0x2050
        // Code size 6 (0x6)
        .maxstack 8

        IL_0000: ldsfld int32 IA::'<P>k__BackingField'
        IL_0005: ret
    } // end of method IA::get_P

    .method public hidebysig static 
        void Main () cil managed 
    {
        // Method begins at RVA 0x2057
        // Code size 2 (0x2)
        .maxstack 8

        IL_0000: nop
        IL_0001: ret
    } // end of method IA::Main

    // Properties
    .property int32& P()
    {
        .get int32& IA::get_P()
    }

} // end of class IA

Expected an error similar to the one in class: "error CS8145: Auto-implemented properties cannot return by reference"

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions