From f0ccf26e63a7375414c3a03bf5c9b551e7120803 Mon Sep 17 00:00:00 2001 From: Alyssa Haroldsen Date: Fri, 19 Jan 2024 15:32:19 -0800 Subject: [PATCH] Correct raw identifier terminology in rust_keywords PiperOrigin-RevId: 599954613 --- rust/test/shared/reserved_test.rs | 6 +++--- src/google/protobuf/compiler/rust/naming.cc | 4 ++-- src/google/protobuf/compiler/rust/rust_keywords.cc | 6 +++--- src/google/protobuf/compiler/rust/rust_keywords.h | 11 ++++++----- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/rust/test/shared/reserved_test.rs b/rust/test/shared/reserved_test.rs index 43db655f2a..cbfcdc84ca 100644 --- a/rust/test/shared/reserved_test.rs +++ b/rust/test/shared/reserved_test.rs @@ -8,12 +8,12 @@ /// Test covering proto compilation with reserved words. use googletest::prelude::*; use reserved_proto::r#enum; -use reserved_proto::Self__mangled_because_symbol_is_a_rust_raw_identifier; +use reserved_proto::Self__mangled_because_ident_isnt_a_legal_raw_identifier; #[test] fn test_reserved_keyword_in_accessors() { - let msg = Self__mangled_because_symbol_is_a_rust_raw_identifier::new(); - let res = msg.self__mangled_because_symbol_is_a_rust_raw_identifier().r#for(); + let msg = Self__mangled_because_ident_isnt_a_legal_raw_identifier::new(); + let res = msg.self__mangled_because_ident_isnt_a_legal_raw_identifier().r#for(); assert_that!(res, eq(0)); } diff --git a/src/google/protobuf/compiler/rust/naming.cc b/src/google/protobuf/compiler/rust/naming.cc index bf95ee42cb..02fce2b82e 100644 --- a/src/google/protobuf/compiler/rust/naming.cc +++ b/src/google/protobuf/compiler/rust/naming.cc @@ -246,9 +246,9 @@ std::string FieldInfoComment(Context& ctx, const FieldDescriptor& field) { } std::string RsSafeName(absl::string_view name) { - if (IsNotLegalEvenWithRPoundPrefix(name)) { + if (!IsLegalRawIdentifierName(name)) { return absl::StrCat(name, - "__mangled_because_symbol_is_a_rust_raw_identifier"); + "__mangled_because_ident_isnt_a_legal_raw_identifier"); } if (IsRustKeyword(name)) { return absl::StrCat("r#", name); diff --git a/src/google/protobuf/compiler/rust/rust_keywords.cc b/src/google/protobuf/compiler/rust/rust_keywords.cc index 0e4e4e56b6..b2688f159c 100644 --- a/src/google/protobuf/compiler/rust/rust_keywords.cc +++ b/src/google/protobuf/compiler/rust/rust_keywords.cc @@ -17,12 +17,12 @@ namespace protobuf { namespace compiler { namespace rust { -bool IsNotLegalEvenWithRPoundPrefix(absl::string_view str) { +bool IsLegalRawIdentifierName(absl::string_view str_without_r_prefix) { // These keywords cannot be used even with an r# prefix. // https://doc.rust-lang.org/reference/identifiers.html - static const auto* rust_raw_identifiers = + static const auto* illegal_raw_identifiers = new absl::flat_hash_set{"crate", "self", "super", "Self"}; - return rust_raw_identifiers->contains(str); + return !illegal_raw_identifiers->contains(str_without_r_prefix); } bool IsRustKeyword(absl::string_view str) { diff --git a/src/google/protobuf/compiler/rust/rust_keywords.h b/src/google/protobuf/compiler/rust/rust_keywords.h index ee53465329..8c5815f42c 100644 --- a/src/google/protobuf/compiler/rust/rust_keywords.h +++ b/src/google/protobuf/compiler/rust/rust_keywords.h @@ -15,13 +15,14 @@ namespace protobuf { namespace compiler { namespace rust { -// Returns true if the provided str is a 'Raw Identifier', which is a symbol -// which cannot be used even with r# prefix. -bool IsNotLegalEvenWithRPoundPrefix(absl::string_view str); +// Returns true if the provided name is legal to use as a raw identifier name +// by prefixing with r# +// https://doc.rust-lang.org/reference/identifiers.html#raw-identifiers +bool IsLegalRawIdentifierName(absl::string_view str_without_r_prefix); // Returns true if the provided str is a Rust 2021 Edition keyword and cannot be -// used as a symbol. These symbols can can be used with an r# prefix unless -// IsNotLegalEvenWithRPoundPrefix is true. This function should always match the +// used as an identifier. These symbols can be used with an r# prefix unless +// IsLegalRawIdentifierName returns false. This function should always match the // behavior for the corresponding Edition that our emitted crates use. bool IsRustKeyword(absl::string_view str);