protocolbuffers / protobuf

Protocol Buffers - Google's data interchange format
http://protobuf.dev
Other
65.65k stars 15.5k forks source link

[c#] Generated code for non-alphanumeric enum names is invalid #19134

Open saquibmian opened 5 hours ago

saquibmian commented 5 hours ago

What version of protobuf and what language are you using? Version: v28.3 Language: C#

What operating system (Linux, Windows, ...) and version? macOS

What runtime / compiler are you using (e.g., python version or gcc version) protoc

What did you do? Steps to reproduce the behavior:

  1. Write foo.proto with the following content.

    syntax = "proto3";
    
    package foo.v1;
    
    enum Foo {
      _ = 0;
    }
  2. Run protoc -I . --csharp_out=. foo.proto

What did you expect to see Foo.cs should contain the following definition:

public enum Foo {
  [pbr::OriginalName("_")]  _ = 0,
}

What did you see instead? Foo.cs contains the following definition, which does not compile (missing enum name):

public enum Foo {
  [pbr::OriginalName("_")]  = 0,
}

Anything else we should know about your project / environment Nothing else.

jskeet commented 5 hours ago

I agree we shouldn't generate invalid code. Whether just _ is the right name to generate is up for discussion, given that _ has a "somewhat" special meaning in C#. (For an enum value that make be okay - for a field it could be pretty confusing. It's worth thinking about how it might be used in all contexts rather than just enums.)

I think it's unlikely that we're going to get to this corner case any time soon - but if you have the time to create a PR to change the C# generator code to fix this, I'd happily review it.