Skip to content

Commit

Permalink
Correct raw identifier terminology in rust_keywords
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 599954613
  • Loading branch information
kupiakos authored and copybara-github committed Jan 19, 2024
1 parent f6812b7 commit f0ccf26
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
6 changes: 3 additions & 3 deletions rust/test/shared/reserved_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/compiler/rust/naming.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/google/protobuf/compiler/rust/rust_keywords.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>{"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) {
Expand Down
11 changes: 6 additions & 5 deletions src/google/protobuf/compiler/rust/rust_keywords.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit f0ccf26

Please sign in to comment.