Skip to content

Commit

Permalink
Rust: ensure we can handle name collision between local module and cr…
Browse files Browse the repository at this point in the history
…ate dependency

This change updates the generated code to always prefix the names of crates
with `::` so that they can't conflict with local modules. I'm not aware of this
being an issue for anyone yet, but figured we might as well fix it proactively.

PiperOrigin-RevId: 703122266
  • Loading branch information
acozzette authored and copybara-github committed Dec 5, 2024
1 parent 482752a commit 738ffe2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 6 additions & 0 deletions rust/test/fields_with_imported_types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ message MsgWithFieldsWithImportedTypes {
imported_types.ImportedEnum imported_enum_oneof = 4;
}
}

// This will produce a module called imported_types_proto. Let's verify that we
// can do this without conflicting with the crate of the same name.
message ImportedTypesProto {
message Nested {}
}
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/rust/naming.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace compiler {
namespace rust {

std::string GetCrateName(Context& ctx, const FileDescriptor& dep) {
return RsSafeName(ctx.ImportPathToCrateName(dep.name()));
return absl::StrCat("::", RsSafeName(ctx.ImportPathToCrateName(dep.name())));
}

std::string GetRsFile(Context& ctx, const FileDescriptor& file) {
Expand Down

0 comments on commit 738ffe2

Please sign in to comment.