Skip to content

The 'scoped' modifier of parameter 'value' doesn't match overridden or implemented member #378

Description

@Bezarius

Description

When compiling the project, the following error occurs:

MemoryPack.Generator\MemoryPack.Generator.MemoryPackGenerator\dto.MemoryPackFormatter.g.cs(172,30): error CS8987: The 'scoped' modifier of parameter 'value' doesn't match overridden or implemented member.

This error arises because the scoped modifier in the generated CurrencyFormatter class doesn't align with the overridden or implemented member in the base class MemoryPackFormatter. The inconsistency stems from differing checks within the generator for .NET version (NET7_0_OR_GREATER) and language version (C# 11).

Enviroment

Unity6, language version 11

Code

Generated Code:

[global::MemoryPack.Internal.Preserve]
sealed class CurrencyFormatter : MemoryPackFormatter<Currency>
{
    [global::MemoryPack.Internal.Preserve]
    public override void Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref Currency value)
    {
        Currency.Serialize(ref writer, ref value);
    }

    [global::MemoryPack.Internal.Preserve]
    public override void Deserialize(ref MemoryPackReader reader, scoped ref Currency value)
    {
        Currency.Deserialize(ref reader, ref value);
    }
}

Base Class:

[Preserve]
public abstract class MemoryPackFormatter<T> : IMemoryPackFormatter<T>, IMemoryPackFormatter
{
    // ...

    [Preserve]                                                                                                                       
    void IMemoryPackFormatter.Deserialize(ref MemoryPackReader reader, [ScopedRef] ref object? value)
    {
        T obj = value == null ? default (T) : (T) value;
        this.Deserialize(ref reader, ref obj);
        value = (object) obj;
    }
}

Root Cause

The code generator uses inconsistent logic for determining when to apply the scoped modifier. Specifically, it checks for NET7_0_OR_GREATER in one part of the generator, and language version 11 in another part. This discrepancy leads to the scoped modifier being applied in the generated code when it's incompatible with the base class definition.

Proposed Solution

Ensure consistent logic within the code generator for applying the scoped modifier. Unify the checks for .NET version and language version to avoid inconsistencies. Specifically, review the following code in the generator:

var net7 = csOptions.PreprocessorSymbolNames.Contains("NET7_0_OR_GREATER");

and the language version check, and ensure they are aligned. The scoped modifier should only be added if both checks pass, or if a single check correctly determines the need for the modifier.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions