Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Bezarius opened this issue Feb 16, 2025 · 0 comments

Comments

@Bezarius
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant