- Only prepend r# to fields when needed instead of always - Append '__mangled_because_symbol_is_a_rust_raw_identifier' to names like 'Self' which can't be used legally even with an r# prefix Also use the same check to prepend r# on: - Message names (eg `message Self {}`) - oneof names - oneof case names - enum names - enum case names - module names (e.g. 'package google.type') PiperOrigin-RevId: 599153141pull/15454/head
parent
85972e505a
commit
cd575718be
15 changed files with 183 additions and 59 deletions
@ -0,0 +1,46 @@ |
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2024 Google LLC. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file or at
|
||||
// https://developers.google.com/open-source/licenses/bsd
|
||||
|
||||
#include "google/protobuf/compiler/rust/rust_keywords.h" |
||||
|
||||
#include <string> |
||||
|
||||
#include "absl/container/flat_hash_set.h" |
||||
#include "absl/strings/string_view.h" |
||||
|
||||
namespace google { |
||||
namespace protobuf { |
||||
namespace compiler { |
||||
namespace rust { |
||||
|
||||
bool IsNotLegalEvenWithRPoundPrefix(absl::string_view str) { |
||||
// These keywords cannot be used even with an r# prefix.
|
||||
// https://doc.rust-lang.org/reference/identifiers.html
|
||||
static const auto* rust_raw_identifiers = |
||||
new absl::flat_hash_set<std::string>{"crate", "self", "super", "Self"}; |
||||
return rust_raw_identifiers->contains(str); |
||||
} |
||||
|
||||
bool IsRustKeyword(absl::string_view str) { |
||||
// https://doc.rust-lang.org/reference/keywords.html#reserved-keywords
|
||||
static const auto* rust_keywords = new absl::flat_hash_set<std::string>{ |
||||
"as", "async", "await", "break", "const", "continue", |
||||
"crate", "dyn", "else", "enum", "extern", "false", |
||||
"fn", "for", "if", "impl", "in", "let", |
||||
"loop", "match", "mod", "move", "mut", "pub", |
||||
"ref", "return", "Self", "self", "static", "struct", |
||||
"super", "trait", "true", "type", "union", "unsafe", |
||||
"use", "where", "while", "abstract", "become", "box", |
||||
"do", "final", "macro", "override", "priv", "try", |
||||
"typeof", "unsized", "virtual", "yield"}; |
||||
return rust_keywords->contains(str); |
||||
} |
||||
|
||||
} // namespace rust
|
||||
} // namespace compiler
|
||||
} // namespace protobuf
|
||||
} // namespace google
|
@ -0,0 +1,33 @@ |
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2024 Google LLC. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file or at
|
||||
// https://developers.google.com/open-source/licenses/bsd
|
||||
|
||||
#ifndef GOOGLE_PROTOBUF_COMPILER_RUST_RUST_KEYWORDS_H__ |
||||
#define GOOGLE_PROTOBUF_COMPILER_RUST_RUST_KEYWORDS_H__ |
||||
|
||||
#include "absl/strings/string_view.h" |
||||
|
||||
namespace google { |
||||
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 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
|
||||
// behavior for the corresponding Edition that our emitted crates use.
|
||||
bool IsRustKeyword(absl::string_view str); |
||||
|
||||
} // namespace rust
|
||||
} // namespace compiler
|
||||
} // namespace protobuf
|
||||
} // namespace google
|
||||
|
||||
#endif // GOOGLE_PROTOBUF_COMPILER_RUST_RUST_KEYWORDS_H__
|
Loading…
Reference in new issue