Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion rust/bazel/aspects.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,15 @@ def _generate_rust_gencode(

additional_args = ctx.actions.args()

annotate_code = False

additional_args.add(
"--rust_opt=experimental-codegen=enabled,kernel={},crate_mapping={},generated_entry_point_rs_file_name={},forced_lite_runtime={}".format(
"--rust_opt=experimental-codegen=enabled,kernel={},crate_mapping={},generated_entry_point_rs_file_name={},forced_lite_runtime={}{}".format(
"upb" if is_upb else "cpp",
crate_mapping.path,
entry_point_rs_output.basename,
"true" if forced_lite_runtime else "false",
",annotate_code=true" if annotate_code else "",
),
)

Expand Down
6 changes: 6 additions & 0 deletions src/google/protobuf/compiler/rust/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ absl::StatusOr<Options> Options::Parse(absl::string_view param) {
opts.force_lite_runtime = lite->second == "true";
}

auto annotate_code = absl::c_find_if(
args, [](auto& arg) { return arg.first == "annotate_code"; });
if (annotate_code != args.end()) {
opts.annotate_code = annotate_code->second == "true";
}

return opts;
}

Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/compiler/rust/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct Options {
std::string mapping_file_path;
bool strip_nonfunctional_codegen = false;
bool force_lite_runtime = false;
bool annotate_code = false;

// The name to use for the generated entry point rs file.
std::string generated_entry_point_rs_file_name = "generated.rs";
Expand Down
15 changes: 14 additions & 1 deletion src/google/protobuf/compiler/rust/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,14 @@ bool RustGenerator::Generate(const FileDescriptor* file,

auto outfile = absl::WrapUnique(
generator_context->Open(GetRsFile(ctx_without_printer, *file)));
io::Printer printer(outfile.get());
GeneratedCodeInfo annotations;
io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector(
&annotations);
io::Printer::Options printer_options{};
if (opts->annotate_code) {
printer_options.annotation_collector = &annotation_collector;
}
io::Printer printer(outfile.get(), printer_options);
Context ctx = ctx_without_printer.WithPrinter(&printer);

// Convenience shorthands for common symbols.
Expand Down Expand Up @@ -327,6 +334,12 @@ bool RustGenerator::Generate(const FileDescriptor* file,
}
}

if (opts->annotate_code) {
ctx.printer().PrintRaw(absl::StrCat(
"// google.protobuf.GeneratedCodeInfo ",
absl::Base64Escape(annotations.SerializeAsString()), "\n"));
}

return true;
}

Expand Down
7 changes: 6 additions & 1 deletion src/google/protobuf/compiler/rust/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ namespace compiler {
namespace rust {
namespace {

using Sub = ::google::protobuf::io::Printer::Sub;

void MessageNew(Context& ctx, const Descriptor& msg) {
switch (ctx.opts().kernel) {
case Kernel::kCpp:
Expand Down Expand Up @@ -409,6 +411,9 @@ void GenerateRs(Context& ctx, const Descriptor& msg, const upb::DefPool& pool) {
upb::MessageDefPtr upb_msg = pool.FindMessageByName(msg.full_name().data());
ctx.Emit(
{
// There's also ${$/$}$-style begin and end tokens, but those might
// be harder to retrofit here because of the giant-template style.
Sub("MsgDefinition", RsSafeName(msg.name())).AnnotatedAs(&msg),
{"Msg", RsSafeName(msg.name())},
{"Msg::new", [&] { MessageNew(ctx, msg); }},
{"Msg::drop", [&] { MessageDrop(ctx, msg); }},
Expand Down Expand Up @@ -520,7 +525,7 @@ void GenerateRs(Context& ctx, const Descriptor& msg, const upb::DefPool& pool) {
},
R"rs(
#[allow(non_camel_case_types)]
pub struct $Msg$ {
pub struct $MsgDefinition$ {
inner: $pbr$::OwnedMessageInner<$Msg$>
}

Expand Down
Loading