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

Handle keyword Self after stripping enum type prefix #998

Merged
merged 13 commits into from
Apr 9, 2024

Conversation

MixusMinimax
Copy link
Contributor

Fixes #997

Relates to #179

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.

It seems there are more keywords with the same problem: https://github.com/rust-lang/rust/blob/d7723b21910075a3c38c2b6e64b2467394c93724/library/proc_macro/src/bridge/symbol.rs#L82

I suggest fixing this for all these keywords

prost-build/src/code_generator.rs Outdated Show resolved Hide resolved
@MixusMinimax
Copy link
Contributor Author

The thing is, we know that the result starts with an upper-case letter, as it is already converted to PascalCase, so Self really was the only issue. I can definitely do that though!

Thank you for your consideration!

@MixusMinimax
Copy link
Contributor Author

I have moved the sanitizing of identifiers to a common function that can be used by all the others. What do you think? I think it'd definitely a lot safer this way

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.

Splitting into sanitize_identifier is great! This handles much more cases now.

I have a few small comments left.

@@ -0,0 +1,7 @@
mod enum_keyword_variant {
// #![deny(unused_results)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

This comment can be removed

/// It also tries to handle cases where the stripped name would be
/// invalid - for example, if it were to begin with a number.
///
/// The stripped name can be `Self`, which is sanitized analogously to [to_upper_camel]
Copy link
Collaborator

Choose a reason for hiding this comment

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

This last line doesn´t add information, as to_upper_camel docs doesn't explain how it is handled. Either remove or describe how it is sanitized.

Comment on lines 249 to 262
#[test]
fn test_strip_enum_prefix() {
assert_eq!(strip_enum_prefix("Foo", "FooBar"), "Bar");
assert_eq!(strip_enum_prefix("Foo", "Foobar"), "Foobar");
assert_eq!(strip_enum_prefix("Foo", "Foo"), "Foo");
assert_eq!(strip_enum_prefix("Foo", "Bar"), "Bar");
assert_eq!(strip_enum_prefix("Foo", "Foo1"), "Foo1");
}

#[test]
fn test_strip_enum_prefix_resulting_in_keyword() {
assert_eq!(strip_enum_prefix("Foo", "FooBar"), "Bar");
assert_eq!(strip_enum_prefix("Foo", "FooSelf"), "Self_");
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would combine these two tests

@@ -47,6 +73,67 @@ mod tests {

use super::*;

#[test]
fn test_sanitize_identifier() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for this thorough test.

enum Grooming {
UNSPECIFIED = 0;
ASSISTED = 1;
SELF = 2;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this have a keyword as well? for example: ELSE = 3;

@MixusMinimax
Copy link
Contributor Author

Should be all done now

@caspermeijn caspermeijn added this pull request to the merge queue Apr 9, 2024
@caspermeijn
Copy link
Collaborator

Thanks for your contribution

Merged via the queue into tokio-rs:master with commit 963e942 Apr 9, 2024
12 checks passed
caspermeijn added a commit to caspermeijn/prost that referenced this pull request May 5, 2024
_PROST!_ is a [Protocol Buffers](https://developers.google.com/protocol-buffers/) implementation for the [Rust Language](https://www.rust-lang.org/). `prost` generates simple, idiomatic Rust code from `proto2` and `proto3` files.

This patch update brings new fixes:

- fix: include_file should handle proto without package (tokio-rs#1002)
- Place Config::format behind the format feature flag
- Handle keyword `Self` after stripping enum type prefix (tokio-rs#998)

## Documentation
- fix(readme): fix the link and badge for CI (tokio-rs#1049)

## Internal
- style(codegen): `Syntax` to a separate file (tokio-rs#1029)
- chore(codegen): extract c string escaping to a separate file (tokio-rs#1028)
- style(prost-build): `CodeGenerator::boxed` method (tokio-rs#1019)
- style(prost-build): Consolidate field data into struct (tokio-rs#1017)
- style(prost-build): `BytesType and MapType` into a `collections` module. (tokio-rs#1030)
- style(prost-build): Split `Config` and `Module` into a separate module and files (tokio-rs#1020)
- style(prost-build): prost_path helper (tokio-rs#1018)
- style: Fix toml indent (tokio-rs#1048)
- style: Fix clippy warnings and enable clippy in CI (tokio-rs#1008)
- build: Use git submodule to download protobuf sources (tokio-rs#1014)
- ci: Add TOML validation with `taplo` (tokio-rs#1034)
- tests: Create a separate tempdir for each test (tokio-rs#1044)
- tests: Remove GoogleMessage3 and GoogleMessage4 benchmarks (tokio-rs#1037)
- chore: Update internal crates to Rust edition 2021 (tokio-rs#1039)
- chore: Update crate descriptions (tokio-rs#1038)
- chore: Fix clippy checks in CI (tokio-rs#1032)
- chore: Add Casper Meijn as author (tokio-rs#1025)
github-merge-queue bot pushed a commit that referenced this pull request May 8, 2024
_PROST!_ is a [Protocol Buffers](https://developers.google.com/protocol-buffers/) implementation for the [Rust Language](https://www.rust-lang.org/). `prost` generates simple, idiomatic Rust code from `proto2` and `proto3` files.

This patch update brings new fixes:

- fix: include_file should handle proto without package (#1002)
- Place Config::format behind the format feature flag
- Handle keyword `Self` after stripping enum type prefix (#998)

## Documentation
- fix(readme): fix the link and badge for CI (#1049)

## Internal
- style(codegen): `Syntax` to a separate file (#1029)
- chore(codegen): extract c string escaping to a separate file (#1028)
- style(prost-build): `CodeGenerator::boxed` method (#1019)
- style(prost-build): Consolidate field data into struct (#1017)
- style(prost-build): `BytesType and MapType` into a `collections` module. (#1030)
- style(prost-build): Split `Config` and `Module` into a separate module and files (#1020)
- style(prost-build): prost_path helper (#1018)
- style: Fix toml indent (#1048)
- style: Fix clippy warnings and enable clippy in CI (#1008)
- build: Use git submodule to download protobuf sources (#1014)
- ci: Add TOML validation with `taplo` (#1034)
- tests: Create a separate tempdir for each test (#1044)
- tests: Remove GoogleMessage3 and GoogleMessage4 benchmarks (#1037)
- chore: Update internal crates to Rust edition 2021 (#1039)
- chore: Update crate descriptions (#1038)
- chore: Fix clippy checks in CI (#1032)
- chore: Add Casper Meijn as author (#1025)
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.

Use of "Self" in stripped enum variants
2 participants