Skip to content

[C#] Compiler warning CS9193 in generated enum serialization #383

@mayfieldiv

Description

@mayfieldiv
enum Status { Unknown = 0; Ok = 1; }
struct Hello { Status status; }

Bebop Playground with this schema

This simple schema generates C# that produces compiler .warnings in .NET 8+.
warning CS9193: Argument 1 should be a variable because it is passed to a 'ref readonly' parameter

Related to this optimization:
#189 - (re-)introduced this optimization
#301 - noticed this compiler warning started in .NET 8 and suppressed it for test package: "For some reason code that worked fine under .net 6.0 is producing warnings in 8.0 and requires a language feature bump; breaking API change in .NET? we just silence them"

I messed around in SharpLab and it looks like the whole unsafe optimization is unnecessary since the JIT compiler optimizes the simple (uint)record.Status without doing any runtime checks.

SharbLab latest compiler - all the methods are equivalent

Image

SharbLab older compiler from C# 9 times - still has equivalent JIT optimizations

Image

SharpLab latest compiler with serialize/deserialization

Also I went ahead and checked both directions of the enum casting to make sure they're all still equivalent.

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions