Cysharp / MemoryPack

Zero encoding extreme performance binary serializer for C# and Unity.
MIT License
3.25k stars 190 forks source link

Regression with base classes #265

Closed Joy-less closed 6 months ago

Joy-less commented 6 months ago

The following code works in MemoryPack 1.10.0:

using System;
using System.Threading;
using MemoryPack;

Console.WriteLine(MemoryPackSerializer.Serialize(new Test()).Length);

public abstract class TestBase {
    public int Number = Interlocked.Increment(ref LastNumber);

    private static int LastNumber;
}
[MemoryPackable]
public partial class Test : TestBase {
}

However, it fails in 1.20.0 - 1.20.4 with the following errors:

CS0122: 'TestBase.LastNumber' is inaccessible due to its protection level
MemoryPack.Generator\MemoryPack.Generator.MemoryPackGenerator\Test.MemoryPackFormatter.g.cs 117
CS0103: The name 'Interlocked' does not exist in the current context
MemoryPack.Generator\MemoryPack.Generator.MemoryPackGenerator\Test.MemoryPackFormatter.g.cs 117

The first error can be removed by making LastNumber public, and the second error can be removed by enabling global usings.

The errors only occur when inheriting from a class (abstract or not).

hadashiA commented 6 months ago

Thanks for the sample code.

Since 1.20.0, I was trying to detect the default value from the source code, but it didn't take into account the case of using the static method you presented.

We decided to revert this feature. I believe the compile error has been eliminated in 1.20.5.