Also apply r# prefix to crates whose names are reserved keywords

PiperOrigin-RevId: 601116457
pull/15550/head
Jakob Buchgraber 1 year ago committed by Copybara-Service
parent af339fad1e
commit 4517308a91
  1. 31
      rust/test/BUILD
  2. 24
      rust/test/shared/BUILD
  3. 13
      rust/test/shared/reserved_crate_name_test.rs
  4. 18
      rust/test/struct.proto
  5. 2
      src/google/protobuf/compiler/rust/generator.cc
  6. 4
      src/google/protobuf/compiler/rust/naming.cc
  7. 2
      src/google/protobuf/compiler/rust/naming.h

@ -1,3 +1,4 @@
load("@rules_cc//cc:defs.bzl", "cc_proto_library")
load(
"//rust:defs.bzl",
"rust_cc_proto_library",
@ -409,3 +410,33 @@ rust_upb_proto_library(
],
deps = ["//src/google/protobuf:map_unittest_proto"],
)
proto_library(
name = "struct",
testonly = True,
srcs = ["struct.proto"],
)
cc_proto_library(
name = "struct_cc_proto",
testonly = True,
deps = [":struct"],
)
rust_cc_proto_library(
name = "struct_cc_rust_proto",
testonly = True,
visibility = [
"//rust/test/shared:__subpackages__",
],
deps = [":struct_cc_proto"],
)
rust_upb_proto_library(
name = "struct_upb_rust_proto",
testonly = True,
visibility = [
"//rust/test/shared:__subpackages__",
],
deps = [":struct"],
)

@ -457,3 +457,27 @@ rust_test(
"@crate_index//:googletest",
],
)
rust_test(
name = "reserved_crate_name_cc_test",
srcs = ["reserved_crate_name_test.rs"],
tags = [
# TODO: Enable testing on arm once we support sanitizers for Rust on Arm.
"not_build:arm",
],
deps = [
"//rust/test:struct_cc_rust_proto",
],
)
rust_test(
name = "reserved_crate_name_upb_test",
srcs = ["reserved_crate_name_test.rs"],
tags = [
# TODO: Enable testing on arm once we support sanitizers for Rust on Arm.
"not_build:arm",
],
deps = [
"//rust/test:struct_upb_rust_proto",
],
)

@ -0,0 +1,13 @@
// 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
use r#struct::Foo;
#[test]
fn test_new() {
let _ = Foo::new();
}

@ -0,0 +1,18 @@
// 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
// The contents of this file has no purpose. This file is used in a
// proto_library named 'struct' to test that crate names of reserved keywords
// are escaped correctly.
syntax = "proto2";
package main;
message Foo {
optional int32 a = 1;
}

@ -67,7 +67,7 @@ void EmitPubUseOfOwnTypes(Context& ctx, const FileDescriptor& primary_file,
// messages and enums of a `dep`. This should only be
// called for 'import public' deps.
void EmitPublicImportsForDepFile(Context& ctx, const FileDescriptor* dep) {
absl::string_view crate_name = GetCrateName(ctx, *dep);
std::string crate_name = GetCrateName(ctx, *dep);
for (int i = 0; i < dep->message_type_count(); ++i) {
auto* msg = dep->message_type(i);
auto path = GetCrateRelativeQualifiedPath(ctx, *msg);

@ -40,8 +40,8 @@ std::string GetUnderscoreDelimitedFullName(Context& ctx,
}
} // namespace
absl::string_view GetCrateName(Context& ctx, const FileDescriptor& dep) {
return ctx.generator_context().ImportPathToCrateName(dep.name());
std::string GetCrateName(Context& ctx, const FileDescriptor& dep) {
return RsSafeName(ctx.generator_context().ImportPathToCrateName(dep.name()));
}
std::string GetRsFile(Context& ctx, const FileDescriptor& file) {

@ -20,7 +20,7 @@ namespace google {
namespace protobuf {
namespace compiler {
namespace rust {
absl::string_view GetCrateName(Context& ctx, const FileDescriptor& dep);
std::string GetCrateName(Context& ctx, const FileDescriptor& dep);
std::string GetRsFile(Context& ctx, const FileDescriptor& file);
std::string GetThunkCcFile(Context& ctx, const FileDescriptor& file);

Loading…
Cancel
Save