Skip to content
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

generate assoc. consts in enums representing protobuf enum aliases, resolves #792 #849

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ousado
Copy link

@ousado ousado commented Apr 23, 2023

This PR adds associated constants to enum impls representing enum aliases in protocol buffer enums as shown below. The aliases can be referenced by prost when used as default values, causing compilation to fail, see #792.

#[derive(PartialEq, Eq)]
pub enum X {
    A,
    B,
}

impl X {
    pub const A1: X = X::A;
}

Copy link
Collaborator

@caspermeijn caspermeijn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes makes sense to me.

  • I think the PR title should be something like: Add support for enum aliases.
  • You need to add some tests for the new use cases.
  • Does prost need to check for allow_alias option? If protoc throw an error when allow_alias is not set, then we don't have to.

self.push_indent();
self.buf.push_str("/// Aliases.\n");
}
for variant in &aliases {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I would place the for loop inside the if-block


if aliases.len() > 0 {
self.push_indent();
self.buf.push_str("/// Aliases.\n");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three slashes is for rustdoc comments. This is not a doc comment, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right

prost-build/src/code_generator.rs Show resolved Hide resolved
Comment on lines 1178 to +1180
if !numbers.insert(value.number()) {
continue;
for m in &mappings {
if m.proto_number == value.number() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

genereated_names can be used to do this lookup in one step, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how, that would require a lookup by value instead of name, no?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. The HashMap is the other way around. Please disregard my comment.

panic!("Generated enum variant names overlap: `{}` variant name to be used both by `{}` and `{}` ProtoBuf enum values",
generated_variant_name, old_v, value.name());
// if alias ends up being a duplicate, we don't need it, and can skip it.
// TODO: check if enum values are actually the same
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this TODO needs to be solved

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like duplicates should always be disallowed. Even if they are an alias, then overlap is not disirable.

Comment on lines 1178 to +1180
if !numbers.insert(value.number()) {
continue;
for m in &mappings {
if m.proto_number == value.number() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. The HashMap is the other way around. Please disregard my comment.

panic!("Generated enum variant names overlap: `{}` variant name to be used both by `{}` and `{}` ProtoBuf enum values",
generated_variant_name, old_v, value.name());
// if alias ends up being a duplicate, we don't need it, and can skip it.
// TODO: check if enum values are actually the same
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like duplicates should always be disallowed. Even if they are an alias, then overlap is not disirable.

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

Successfully merging this pull request may close these issues.

2 participants