Skip to content

Consider hiding nullability differences in PEModuleBuilder.Translate()  #45758

@cston

Description

@cston

A couple of recent PRs have changed MetadataWriter to accommodate type references that differ only by nullability: #38249 and #45714.

Since nullability is not persisted as part of the type reference, an alternative suggested by @AlekseyTs to changing MetadataWriter might be to hide nullability differences in PEModuleBuilder.Translate().

For instance, the following change to PEModuleBuilder.Translate() seems to work as an alternative to #45714.

internal Cci.INamedTypeReference Translate(
    NamedTypeSymbol namedTypeSymbol,
    SyntaxNode syntaxNodeOpt,
    DiagnosticBag diagnostics,
    bool fromImplements = false,
    bool needDeclaration = false)
{
    Debug.Assert(namedTypeSymbol.IsDefinitionOrDistinct());
    Debug.Assert(diagnostics != null);

    // If the only differences between the type and OriginalDefinition is nullability, use
    // OriginalDefinition because nullability is not persisted as part of the type reference.
    if (!namedTypeSymbol.IsDefinition)
    {
        var definition = namedTypeSymbol.OriginalDefinition;
        if (namedTypeSymbol.Equals(definition, TypeCompareKind.AllNullableIgnoreOptions))
        {
            namedTypeSymbol = definition;
        }
    }

    // ...

See also related issue #40271 translating native integers.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions