Skip to content

[API Proposal]: SignatureTypeEncoder.TypedReference() #80812

@jcouv

Description

@jcouv

Background and motivation

The roslyn compiler made two changes:

  1. produce a warning instead of an error for pointer to managed type in unsafe context
  2. consider System.TypedReference to be managed

As a result, it is possible to emit reference to this type in new contexts. For example in a pointer type.
The natural way to do it in Microsoft.Cci.MetadataWriter.SerializeTypeReference would be to call a SignatureTypeEncoder.TypedReference() method, but that method doesn't exist.
Note that such an API exists on ReturnTypeEncoder, LocalVariableTypeEncoder, and ParameterTypeEncoder already.

Relates to dotnet/roslyn#66328

API Proposal

namespace System.Reflection.Metadata.Ecma335;
public readonly struct SignatureTypeEncoder
{
    // additional method:
    public void TypedReference()
    {
        Builder.WriteByte((byte)SignatureTypeCode.TypedReference);
    }
}

API Usage

        private void SerializeTypeReference(SignatureTypeEncoder encoder, ITypeReference typeReference)
        {
            while (true)
            {
                if (module.IsPlatformType(typeReference, PlatformType.SystemTypedReference))
                {
                    encoder.TypedReference();
                    return;
                }
                ... handle other types and recursive scenarios
        }

Alternative Designs

It is possible to call Builder.WriteByte((byte)SignatureTypeCode.TypedReference); directly. That's the workaround used by the compiler for now.

Risks

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions