From 6a0ff9d203c07fb4aa4759fe126ffdc21c1776aa Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Wed, 4 Dec 2024 14:17:17 -0800 Subject: [PATCH] Rust: remove extern crate declaration for `std` from generated code Starting from the 2018 edition of Rust, paths starting with `::` must refer to a crate, so when we refer to `::std` there is no risk of accidental collision with another identifier. Using `::std` instead of `::__std` is useful because it works even when the generated code is in a module rather than directly in the crate root. This allows us to remove one of the post-processing steps from the codegen crate. PiperOrigin-RevId: 702858213 --- rust/release_crates/protobuf_codegen/src/lib.rs | 3 +-- src/google/protobuf/compiler/rust/generator.cc | 11 +++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/rust/release_crates/protobuf_codegen/src/lib.rs b/rust/release_crates/protobuf_codegen/src/lib.rs index 15a9cf1aa761..c251f293bffc 100644 --- a/rust/release_crates/protobuf_codegen/src/lib.rs +++ b/rust/release_crates/protobuf_codegen/src/lib.rs @@ -144,8 +144,7 @@ impl CodeGen { .unwrap() .replace("crate::", "") .replace("protobuf_upb", "protobuf") - .replace("::__pb", "__pb") - .replace("::__std", "__std"); + .replace("::__pb", "__pb"); let mut file = OpenOptions::new().write(true).truncate(true).open(path).unwrap(); file.write(contents.as_bytes()).unwrap(); } diff --git a/src/google/protobuf/compiler/rust/generator.cc b/src/google/protobuf/compiler/rust/generator.cc index 576d65e711ee..5c06f9b6b62c 100644 --- a/src/google/protobuf/compiler/rust/generator.cc +++ b/src/google/protobuf/compiler/rust/generator.cc @@ -147,19 +147,18 @@ bool RustGenerator::Generate(const FileDescriptor* file, // Convenience shorthands for common symbols. auto v = ctx.printer().WithVars({ - {"std", "::__std"}, + {"std", "::std"}, {"pb", "::__pb"}, {"pbi", "::__pb::__internal"}, {"pbr", "::__pb::__runtime"}, - {"NonNull", "::__std::ptr::NonNull"}, - {"Phantom", "::__std::marker::PhantomData"}, - {"Result", "::__std::result::Result"}, - {"Option", "::__std::option::Option"}, + {"NonNull", "::std::ptr::NonNull"}, + {"Phantom", "::std::marker::PhantomData"}, + {"Result", "::std::result::Result"}, + {"Option", "::std::option::Option"}, }); ctx.Emit({{"kernel", KernelRsName(ctx.opts().kernel)}}, R"rs( extern crate protobuf_$kernel$ as __pb; - extern crate std as __std; )rs");