Skip to content

"return" attributes on extension property accessors not emitted on implementation #80017

@sbomer

Description

@sbomer

When applying attributes to extension properties, I expect them to show up in the implementation method. If not for all attributes, this should at least happen for attributes marked CompilerLoweringPreserve:

using System.Diagnostics.CodeAnalysis;

_ = new C().Property;

static class Extensions {
    extension(C c) {
        [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]
        public Type Property
        {
            [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]
            
            get => typeof(int);
        }
    }
}

class C { }

The metadata only shows up on the generated extension metadata type:

    .method public hidebysig specialname 
            instance [System.Runtime]System.Type 
            get_Property() cil managed
    {
      .custom instance void [System.Runtime]System.Runtime.CompilerServices.NullableContextAttribute::.ctor(uint8) = ( 01 00 01 00 00 ) 
      .custom instance void [System.Runtime]System.Runtime.CompilerServices.ExtensionMarkerAttribute::.ctor(string) = ( 01 00 24 3C 4D 3E 24 41 35 32 37 42 44 34 42 37   // 
..$<M>$A527BD4B7
                                                                                                                        41 34 35 33 39 33 32 30 36 46 34 35 38 44 42 45   // 
A45393206F458DBE
                                                                                                                        30 43 45 35 36 46 46 00 00 )                      // 
0CE56FF..
      .param [0]
      .custom instance void [System.Runtime]System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute::.ctor([System.Runtime]System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes) = ( 01 00 00 02 00 00 00 00 ) 
      // Code size       2 (0x2)
      .maxstack  8
      IL_0000:  ldnull
      IL_0001:  throw
    } // end of method '<G>$9794DAFCCB9E752B29BFD6350ADA77F2'::get_Property

    .property instance [System.Runtime]System.Type
            Property()
    {
      .custom instance void [System.Runtime]System.Runtime.CompilerServices.NullableAttribute::.ctor(uint8) = ( 01 00 01 00 00 ) 
      .custom instance void [System.Runtime]System.Runtime.CompilerServices.ExtensionMarkerAttribute::.ctor(string) = ( 01 00 24 3C 4D 3E 24 41 35 32 37 42 44 34 42 37   // ..$<M>$A527BD4B7
                                                                                                                        41 34 35 33 39 33 32 30 36 46 34 35 38 44 42 45   // A45393206F458DBE
                                                                                                                        30 43 45 35 36 46 46 00 00 )                      // 0CE56FF..
      .custom instance void [System.Runtime]System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute::.ctor([System.Runtime]System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes) = ( 01 00 00 02 00 00 00 00 ) 
      .get instance [System.Runtime]System.Type Extensions/'<G>$9794DAFCCB9E752B29BFD6350ADA77F2'::get_Property()
    } // end of property '<G>$9794DAFCCB9E752B29BFD6350ADA77F2'::Property
  } // end of class '<G>$9794DAFCCB9E752B29BFD6350ADA77F2'

But not on the implementation:

  .method public hidebysig static [System.Runtime]System.Type 
          get_Property(class C c) cil managed
  {
    .custom instance void [System.Runtime]System.Runtime.CompilerServices.NullableContextAttribute::.ctor(uint8) = ( 01 00 01 00 00 ) 
    // Code size       11 (0xb)
    .maxstack  8
    IL_0000:  ldtoken    [System.Runtime]System.Int32
    IL_0005:  call       [System.Runtime]System.Type [System.Runtime]System.Type::GetTypeFromHandle([System.Runtime]System.RuntimeTypeHandle)
    IL_000a:  ret
  } // end of method Extensions::get_Property

} // end of class Extensions

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions