From 361faa658cd77aeb00d0e7d9cb94177a7218dec0 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Thu, 7 Dec 2023 12:08:53 -0800 Subject: [PATCH] Use the more robust check for whether a given type is defined in the current crate or not (instead of a hack against the word 'import' being in the package name). PiperOrigin-RevId: 588868795 --- src/google/protobuf/compiler/rust/context.cc | 10 ++++++++++ src/google/protobuf/compiler/rust/context.h | 4 ++++ src/google/protobuf/compiler/rust/generator.cc | 5 +++-- src/google/protobuf/compiler/rust/message.cc | 7 +++---- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/google/protobuf/compiler/rust/context.cc b/src/google/protobuf/compiler/rust/context.cc index 82ac86a0c0..68d4c9fb2a 100644 --- a/src/google/protobuf/compiler/rust/context.cc +++ b/src/google/protobuf/compiler/rust/context.cc @@ -17,6 +17,7 @@ #include "absl/strings/string_view.h" #include "absl/strings/substitute.h" #include "google/protobuf/compiler/code_generator.h" +#include "google/protobuf/descriptor.h" namespace google { namespace protobuf { @@ -67,6 +68,15 @@ absl::StatusOr Options::Parse(absl::string_view param) { return opts; } +bool IsInCurrentlyGeneratingCrate(Context file) { + return file.generator_context().is_file_in_current_crate(&file.desc()); +} + +bool IsInCurrentlyGeneratingCrate(Context message) { + return message.generator_context().is_file_in_current_crate( + message.desc().file()); +} + } // namespace rust } // namespace compiler } // namespace protobuf diff --git a/src/google/protobuf/compiler/rust/context.h b/src/google/protobuf/compiler/rust/context.h index ac31e99d68..affe8aee1b 100644 --- a/src/google/protobuf/compiler/rust/context.h +++ b/src/google/protobuf/compiler/rust/context.h @@ -132,6 +132,10 @@ class Context { const RustGeneratorContext* rust_generator_context_; io::Printer* printer_; }; + +bool IsInCurrentlyGeneratingCrate(Context file); +bool IsInCurrentlyGeneratingCrate(Context message); + } // namespace rust } // namespace compiler } // namespace protobuf diff --git a/src/google/protobuf/compiler/rust/generator.cc b/src/google/protobuf/compiler/rust/generator.cc index ecb6d56f4a..3465672232 100644 --- a/src/google/protobuf/compiler/rust/generator.cc +++ b/src/google/protobuf/compiler/rust/generator.cc @@ -134,9 +134,10 @@ void EmitPublicImports(Context& primary_file) { // TODO: Handle the case where a non-primary src with the same // declared package as the primary src publicly imports a file that the // primary doesn't. - if (primary_file.generator_context().is_file_in_current_crate(dep_file)) - continue; auto dep = primary_file.WithDesc(dep_file); + if (IsInCurrentlyGeneratingCrate(dep)) { + return; + } EmitPubUseForImportedMessages(primary_file, dep); } } diff --git a/src/google/protobuf/compiler/rust/message.cc b/src/google/protobuf/compiler/rust/message.cc index 9cc35239c5..a1cfb4e8c4 100644 --- a/src/google/protobuf/compiler/rust/message.cc +++ b/src/google/protobuf/compiler/rust/message.cc @@ -9,7 +9,6 @@ #include "absl/log/absl_check.h" #include "absl/log/absl_log.h" -#include "absl/strings/match.h" #include "absl/strings/string_view.h" #include "google/protobuf/compiler/cpp/helpers.h" #include "google/protobuf/compiler/cpp/names.h" @@ -175,11 +174,11 @@ void GetterForViewOrMut(Context field, bool is_mut) { auto self = is_mut ? "self.inner.msg()" : "self.msg"; if (fieldType == FieldDescriptor::TYPE_MESSAGE) { Context d = field.WithDesc(field.desc().message_type()); - auto prefix = "crate::" + GetCrateRelativeQualifiedPath(d); - // TODO: investigate imports breaking submsg accessors - if (absl::StrContains(prefix, "import")) { + // TODO: support messages which are defined in other crates. + if (!IsInCurrentlyGeneratingCrate(d)) { return; } + auto prefix = "crate::" + GetCrateRelativeQualifiedPath(d); field.Emit( { {"prefix", prefix},