Skip to content

Unable to compile protos with aliased default values #792

Open
@jteplitz

Description

Given this valid protobuf:

syntax = "proto2";

package test;

enum Foo {
  option allow_alias = true;

  BAR = 1;
  ZAA = 2;

  DEFAULT = 1;
}

message MyMessage {
  optional Foo f = 1 [ default = DEFAULT ];
}

prost generates this error

❯ cargo build
   Compiling prost-bug v0.1.0 (/Users/jason/projects/prost-bug)
error[E0599]: no variant or associated item named `Default` found for enum `Foo` in the current scope
 --> /Users/jason/projects/prost-bug/target/debug/build/prost-bug-6fb2c18e0f681f70/out/test.rs:2:28
  |
2 | #[derive(Clone, PartialEq, ::prost::Message)]
  |                            ^^^^^^^^^^^^^^^^ variant or associated item not found in `Foo`
...
9 | pub enum Foo {
  | ------------ variant or associated item `Default` not found for this enum
  |
  = note: this error originates in the derive macro `::prost::Message` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0599`.
error: could not compile `prost-bug` due to previous error

The issue seems to be that prost ignores DEFAULT when generating an enum for Foo since rust enums do not allow aliases like C++. However, it still references Default when generating generating MyMessage.

I'm not sure what the best fix would be here, but since this is explicitly allowed by the protobuf spec it feels like it should compile with prost. Since the f field just becomes an i32 anyways, maybe prost can generate an integer constant for the aliased values?

I put a simple reproduction in a small project here: https://github.com/jteplitz/prost-alias-default-bug

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