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
pull/15003/head
Protobuf Team Bot 12 months ago committed by Copybara-Service
parent 715b543e04
commit 361faa658c
  1. 10
      src/google/protobuf/compiler/rust/context.cc
  2. 4
      src/google/protobuf/compiler/rust/context.h
  3. 5
      src/google/protobuf/compiler/rust/generator.cc
  4. 7
      src/google/protobuf/compiler/rust/message.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> Options::Parse(absl::string_view param) {
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

@ -132,6 +132,10 @@ class Context {
const RustGeneratorContext* rust_generator_context_;
io::Printer* printer_;
};
bool IsInCurrentlyGeneratingCrate(Context<FileDescriptor> file);
bool IsInCurrentlyGeneratingCrate(Context<Descriptor> message);
} // namespace rust
} // namespace compiler
} // namespace protobuf

@ -134,9 +134,10 @@ void EmitPublicImports(Context<FileDescriptor>& 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);
}
}

@ -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<FieldDescriptor> field, bool is_mut) {
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},

Loading…
Cancel
Save