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
diff --git a/src/google/protobuf/compiler/rust/context.cc b/src/google/protobuf/compiler/rust/context.cc
index 82ac86a..68d4c9f 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 @@
   return opts;
 }
 
+bool IsInCurrentlyGeneratingCrate(Context<FileDescriptor> file) {
+  return file.generator_context().is_file_in_current_crate(&file.desc());
+}
+
+bool IsInCurrentlyGeneratingCrate(Context<Descriptor> 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 ac31e99..affe8ae 100644
--- a/src/google/protobuf/compiler/rust/context.h
+++ b/src/google/protobuf/compiler/rust/context.h
@@ -132,6 +132,10 @@
   const RustGeneratorContext* rust_generator_context_;
   io::Printer* printer_;
 };
+
+bool IsInCurrentlyGeneratingCrate(Context<FileDescriptor> file);
+bool IsInCurrentlyGeneratingCrate(Context<Descriptor> 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 ecb6d56..3465672 100644
--- a/src/google/protobuf/compiler/rust/generator.cc
+++ b/src/google/protobuf/compiler/rust/generator.cc
@@ -134,9 +134,10 @@
     // 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 9cc3523..a1cfb4e 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 @@
   auto self = is_mut ? "self.inner.msg()" : "self.msg";
   if (fieldType == FieldDescriptor::TYPE_MESSAGE) {
     Context<Descriptor> 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},