Skip to content

Commit

Permalink
Use the more robust check for whether a given type is defined in the …
Browse files Browse the repository at this point in the history
…current crate or not (instead of a hack against the word 'import' being in the package name).

PiperOrigin-RevId: 588868795
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Dec 7, 2023
1 parent 715b543 commit 361faa6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/google/protobuf/compiler/rust/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/google/protobuf/compiler/rust/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/google/protobuf/compiler/rust/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/google/protobuf/compiler/rust/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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},
Expand Down

0 comments on commit 361faa6

Please sign in to comment.