Commit graph

749 commits

Author SHA1 Message Date
Jason Lunn
3df019d491 Forward-port Rust crate swap (#29397)
Cherrypick crate fixes from 36.x to main

Closes #29397

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/29397 from protocolbuffers:forward_port_crate_swap 2b5bb9fe65
PiperOrigin-RevId: 970589358
2026-08-25 09:08:44 -07:00
Protobuf Team Bot
e1c5f3ac47 rust protobuf matchers: Add a partially matcher to mirror C++
It is common to want to match only a subset of fields in large protobufs, and doing that in rust right now requires manually clearing out the fields in the `actual` message that we don't care about. This is a pain, and a worse experience when compared to C++.

Like in C++, when doing partial matching, only fields set in the expected protobuf message are compared, while extra fields set only in the actual message are ignored.

When using the C++ kernel we leverage `proto2::util::MessageDifferencer`, and for the UPB kernel we use the `kUpb_CompareOption_Partial` option.

PiperOrigin-RevId: 964125682
2026-08-13 09:24:36 -07:00
Runze Wang
97fcd43558 Re-export to google-protobuf crate in 0.x.y for 36.0 and above
New crates are in https://crates.io/search?q=google-protobuf

PiperOrigin-RevId: 963684439
2026-08-12 15:12:23 -07:00
Runze Wang
e8bd2b5dcc Remove iterator trait implementations for RepeatedMutIter
Use inherent next()/next_back()/len() API instead.

PiperOrigin-RevId: 963506746
2026-08-12 09:57:23 -07:00
Runze Wang
b1b5f993e8 Enable inherent next() and next_back() API for RepeatedMutIter besides trait implementation.
This is in preparation for removing the impl Iterator on RepeatedMutIter, since we intend to make it a lending iterator which the std Iterator trait does not support.

PiperOrigin-RevId: 962265415
2026-08-10 11:25:24 -07:00
Runze Wang
8c137c4e09 internal change
PiperOrigin-RevId: 959737239
2026-08-05 10:29:52 -07:00
Protobuf Team Bot
8acef15ff3 gtest_matchers_impl.rs: Reorder the file a bit and add more comments
This makes it easier to find the thing people are actually interested in - `proto_eq`.

PiperOrigin-RevId: 957308502
2026-07-31 13:35:21 -07:00
Krasimir Georgiev
aed6a821d1 Add required_providers to proto aspect
PiperOrigin-RevId: 955838256
2026-07-29 06:10:06 -07:00
Protobuf Team Bot
a105c7ef15 Close risk of RustProto UB in the face of a set Cord-typed field.
In Rust, enums are always closed, such that its UB to construct one that isn't the corresponding name. This is true even for #[non_exhaustive] #[repr(C)] enums.

If FFI provides such an enum, its always immediate UB in Rust if it reaches a case with no name.

PiperOrigin-RevId: 954588991
2026-07-27 06:26:08 -07:00
Clayton Knittel
c4be7483cb Use ClassData instead of a prototype throughout lazy field / extensions when possible.
`New` is more efficient on ClassData than on a prototype, as `New` on the prototype has to first load the class data through the prototype.

There's also a small optimization in `LazyField::MergeFromMessage` which avoids loading the class data twice. The redundancy was made more obvious by this refactor.

PiperOrigin-RevId: 953889072
2026-07-25 09:22:11 -07:00
Runze Wang
e0337ad29e Inline hot methods on Map and in generated code
These methods are one-to-three line wrappers delegating to underlying map implementations (upb/cpp_kernel). Without `#[inline]`, the compiler will not cross crate boundaries, resulting in many physical function call overheads for otherwise fully optimal code in hot loops on Map values.

PiperOrigin-RevId: 953537646
2026-07-24 13:50:52 -07:00
Protobuf Team Bot
4bb2c61094 Internal change.
PiperOrigin-RevId: 951011798
2026-07-20 13:13:44 -07:00
Protobuf Team Bot
ad1064c841 Fix the behavior of GetOrCreateMutableMessage on fields in oneofs
Before this, for a field in a oneof, GetOrCreateMutableMessage would read the union value without making sure it was the right case first, effectively casting blindly.

This would result in a crash in RustProto if you set any field in a oneof and then read the mutable-getter of a message-typed field in the same oneof.

PiperOrigin-RevId: 950998569
2026-07-20 12:50:45 -07:00
Protobuf Team Bot
e7b3391b43 Remove -release suffix from Rust Crates.io release if minor version >= 36
This change signals that we actually will subscribe the library semver going forward.

PiperOrigin-RevId: 940534757
2026-06-30 10:46:57 -07:00
Protobuf Team Bot
142d3928b2 Internal change
PiperOrigin-RevId: 939972433
2026-06-29 12:35:21 -07:00
Protobuf Team Bot
796f71274c Set Rust edition 2024 for all protobuf and upb rust targets.
PiperOrigin-RevId: 936789455
2026-06-23 11:29:41 -07:00
Protobuf Team Bot
6abea2a805 RustProto: Remove redundant length checks and casts in message.cc
PiperOrigin-RevId: 932445168
2026-06-15 07:21:11 -07:00
Protobuf Team Bot
4184df38a3 Automated Code Change
PiperOrigin-RevId: 932193885
2026-06-14 20:35:06 -07:00
Protobuf Team Bot
16c1367c7b Label rust:use_upb_kernel to resolve this label against protobufs' //rust, instead of a local repository's
PiperOrigin-RevId: 928527845
2026-06-08 06:33:05 -07:00
Abhinav Tripathi
f5e654b534 Add treeshaking tests for protobuf rust on android and wasm
Added tests:
* One test to check that unused symbols will not be present in the final binary. This is now supported on all platforms.
* Another test to ensure the number of exception tables remains exactly same, even after adding more unused symbols in proto file. You will need to compile rust with panic=abort for this test.

For this test, we have kept proto's crate name to be identical, hence we need them in their own subdirectories.

PiperOrigin-RevId: 928340707
2026-06-07 22:48:57 -07:00
Protobuf Team Bot
54b62411d7 Remove unused Matcher_Eq import in rust
PiperOrigin-RevId: 927449688
2026-06-05 13:17:35 -07:00
Protobuf Team Bot
c99b4fbc40 Replace transmute on arena with pointer cast
This is just a slightly more Rust idiomatic way of being clear that you are knowingly using unsafe to soundly extend a lifetime.

PiperOrigin-RevId: 926763735
2026-06-04 10:36:14 -07:00
Ada Zhang
8ceca4cb58 upb: Adds a new error code kUpb_ErrorCode_MaxDepthExceeded.
PiperOrigin-RevId: 923465700
2026-05-29 09:59:31 -07:00
Fabian Wolff
c616b4becd Pass allowed_unstable_rust_features to rustc_compile_action.
PiperOrigin-RevId: 914211132
2026-05-12 04:40:50 -07:00
Protobuf Team Bot
f472fe1724 Add parity methods to Protobuf Repeated and Map
Added fns like first(), last(), contains_key() and implement
standard traits that are supported by Vec (DoubleEndedIterator, FromIterator).

PiperOrigin-RevId: 910689041
2026-05-05 08:00:20 -07:00
Protobuf Team Bot
59aa92b4fa Automated Code Change
PiperOrigin-RevId: 907335237
2026-04-28 21:15:09 -07:00
Protobuf Team Bot
06afcbd65b RustProto: add serialized_len()->usize
PiperOrigin-RevId: 904638364
2026-04-23 14:34:21 -07:00
Protobuf Team Bot
70590a527e RustProto: Add the ability to iterate over the muts of a repeated message-typed field.
PiperOrigin-RevId: 904479108
2026-04-23 08:53:33 -07:00
Protobuf Team Bot
0be05fd8f9 Add Private passkey to extension api that is only intended to be available for internal and gencode use.
PiperOrigin-RevId: 899554889
2026-04-14 06:48:28 -07:00
Joshua Haberman
7c958618b6 Added infrastructure for Rust Extensions support, but left it disabled for now.
This code makes use of some Rust features that are not supported in Rust 1.85 (our currently supported version), so we cannot enable the codegen for this feature yet.

This CL introduces support for [Protobuf Extensions](https://engdoc.corp.google.com/eng/doc/devguide/proto/programming-guides/proto2.html#extensions) to the Rust Protobuf library, a key feature for full Proto2 API parity.

Key changes include:

1.  **Code Generation:**
    *   Added `compiler/rust/extension.cc` and `extension.h` to handle the generation of Rust code for extension definitions.
    *   Updated `generator.cc` and `message.cc` to invoke extension generation for both top-level and nested extensions.
    *   Generates `ExtensionId` constants for each extension.

2.  **Runtime API (`rust/extension.rs`):**
    *   Introduced the generic `ExtensionId<Extendee, T>` type to identify and type-check extensions.
    *   Defined traits for extension operations: `ExtHas`, `ExtGet`, `ExtSet`, `ExtClear`, and `ExtGetMut`.
    *   Implemented methods on `ExtensionId` that dispatch to the trait implementations provided by the active kernel.

3.  **Kernel Implementations:**
    *   **C++ Kernel (`rust/cpp_kernel/extension.rs`):** Implemented extension traits by calling into new C++ FFI functions (defined in `rust/cpp_kernel/extension.cc`) that interact with the C++ `ExtensionSet`.
    *   **UPB Kernel (`rust/upb_kernel/extension.rs`):** Implemented extension traits using UPB's MiniTable-based extension API. Extension MiniTables are registered at static initialization time using the `linkme` crate.

4.  **Build System:**
    *   Updated `protobuf/rust/BUILD` and `protobuf/compiler/rust/BUILD` to include new files.
    *   Added a dependency on `//third_party/rust/linkme/v0_3` for the UPB kernel's extension registration.

5.  **Testing:**
    *   Added `test/extensions.proto` and `test/extensions_separate_file.proto` to define various extension types.
    *   Added `test/shared/extensions_test.rs` with comprehensive tests covering presence, getters, setters, defaults, nested extensions, submessage extensions, repeated extensions, serialization, and `MergeFrom`.

This change enables Rust users to define and interact with Protobuf extensions in a type-safe manner, compatible with both the C++ and UPB runtimes.

PiperOrigin-RevId: 896506594
2026-04-08 08:08:46 -07:00
Joshua Haberman
f54e9a1eaf Refactor: Move interop traits to kernel-specific modules.
This change moves the `OwnedMessageInterop`, `MessageViewInterop`, and `MessageMutInterop` traits from `codegen_traits.rs` into new `interop.rs` files within both the `cpp_kernel` and `upb_kernel` directories.

The `KernelMessage`, `KernelMessageView`, and `KernelMessageMut` traits in each kernel's `message.rs` are updated to include the respective kernel-specific interop traits. This ensures that the interop functionality is tied to the specific kernel implementation.

The `shared.rs` module now re-exports the interop traits from `crate::__internal::runtime::interop`, which will resolve to the correct kernel's interop module.

It would be possible to take this farther by making the interop traits return strongly typed pointers to `upb_Message*` and `proto2::MessageLite*` instead of `c_void`, but that would be a breaking change.

PiperOrigin-RevId: 892470084
2026-03-31 12:04:34 -07:00
Protobuf Team Bot
63d6ef01b0 Mangle types named XyzView if there is any direct sibling named Xyz
We had already done this for one special case of an enum named XyzView with a sibling message named Xyz, but this change extends that to apply to all pairwise combination of message-or-enum XyzView and message Xyz.

Unfortunately protobuf has no holistic strategy for avoiding collisions in gencode of pairwise names, and unfortunately Rust is maximally hostile situation to collision problems. This is an incomplete fix for potential collisions of this shape, but covers a significant portion of realistic/organic collisions.

Fixes https://github.com/protocolbuffers/protobuf/issues/25736

PiperOrigin-RevId: 890070424
2026-03-26 15:15:22 -07:00
Protobuf Team Bot
002f830c46 Remove Copy+Clone from MessageMutInner.
MessageMutInner was Copy+Clone, which had been convenient for some internal use. But, with them being .into() SomeMessageMut this was a potential footgun, since it was very easy to into() the same one twice which would be formally unsound if it was reachable (including that you could send each of them to a different thread and concurrently modify them).

As these were implementation detail types this is only a defensive fix and no actual soundness issues were identified.

PiperOrigin-RevId: 888615057
2026-03-24 05:52:58 -07:00
Joshua Haberman
1c478d5923 Added appropriate super-traits to Enum.
The `Enum` trait now requires several other traits, including `Singular`, `Into<i32>`, `Copy`, `Proxied`, `EntityType`, and `SealedInternal`. This simplifies bounds in functions that operate on types implementing `Enum`, as they no longer need to explicitly list `+ Singular` and other common requirements.

PiperOrigin-RevId: 888286136
2026-03-23 14:40:03 -07:00
Joshua Haberman
2b3058dcff Refactor: Split upb and cpp backends into submodules
This CL contains no functional change, it is a pure splitting of the existing files.

I elected to move the C++ kernel files to `cpp_kernel/` which already contained the C++ API for the C++ kernel.  Unfortunately this required removing the existing `third_party/protobuf/rust/cpp_kernel/BUILD` file, because, the Rust parts of the cpp kernel have circular dependencies with the shared code in `third_party/protobuf/rust`.

PiperOrigin-RevId: 886884124
2026-03-20 11:13:43 -07:00
Protobuf Team Bot
7f7b974fce Allow ProtoStr to be used in const contexts.
PiperOrigin-RevId: 886834080
2026-03-20 09:34:26 -07:00
Luke Zarko
d568c6e142 Internal-only change.
PiperOrigin-RevId: 885875178
2026-03-18 17:36:10 -07:00
Joshua Haberman
fabd38def4 Moved EntityType tag to shared runtime.
This will allow us to use blanket implementations of messages/enums for both backends, not just upb.

PiperOrigin-RevId: 885697415
2026-03-18 11:24:45 -07:00
Joshua Haberman
f0aa515b21 Give Message backend-specific supertraits.
This simplifies the trait bounds in the implementation (`upb.rs` and `cpp.rs`), and solves some upcoming cases with extensions.

More cleanups can probably be performed, but this is quick initial pass.

PiperOrigin-RevId: 885668189
2026-03-18 10:31:14 -07:00
Protobuf Team Bot
d787869082 Make any &T impl AsView if T impl AsView
And same for &mut T if T impl AsMut

This matches the behavior of std::convert::AsRef which has similar blanket impls to this.

PiperOrigin-RevId: 883287790
2026-03-13 12:35:19 -07:00
Samuel Benzaquen
6ced6127bc Change _opt() accessors to return an std::Option instead of protobuf::Optional
protob::Optional was designed to be able to carry a default value in the Unset case, as in:

```message M { int32 x = 1 [default=42]; }```

Before this change: m.x_opt() would return Unset(42) if the field was unset.

After this change: m.x_opt() will return None instead. If they want to know the default value they will need to look at the accessor again, like:

if !msg.x_opt().is_none() {   // or !msg.has_x()
  let default_val = msg.x();
}

PiperOrigin-RevId: 883178303
2026-03-13 08:31:00 -07:00
Protobuf Team Bot
665c1a3445 Deny all warnings in our cargo test setup.
Some suppressions to make our current code warning-clean. A number of these are slightly odd, because in blazel upb/rust is a separate crate and so has sensible pub fns that we don't happen to use (which is no-warning), but in Cargo where we embed it as a module in our crate without re-exporting it, it becomes a warning if we don't use those fns/constants/etc (because an unused fn on a crate-visibility struct is considered an unused fn warning).

Rust is set up so it won't show warnings from your cratesio deps, but as our
gencode is embedded in the callers crate warnings will annoy them. Tightening
this will help reduce (but not totally prevent)  cases where warnings in our
code will show up to users.

PiperOrigin-RevId: 882686850
2026-03-12 11:18:41 -07:00
Protobuf Team Bot
f8daf2fabc Add some common methods to ProtoStr to make it more ergonomic to use without dropping down to &[u8]
Also make ProtoString deref to &ProtoStr to get the same methods there.

PiperOrigin-RevId: 882142314
2026-03-11 12:36:35 -07:00
Protobuf Team Bot
fd819467f0 Add flag_if_supported("-flto=thin") to our upb cargo compilation
PiperOrigin-RevId: 880863939
2026-03-09 08:11:35 -07:00
Taylor Cramer
74b6f3fe0b ffi_11: Define basic arithmetic operations and comparison with underlying type
PiperOrigin-RevId: 879684052
2026-03-06 09:55:42 -08:00
Protobuf Team Bot
b3cd288ce4 Have from_utf8_unchecked on both ProtoString and &ProtoStr not be considered 'unsafe' since we guarantee it won't reach UB if it is used.
In both cases, support only try_into() not into() when going from [u8]: its intended that `string` fields maintain utf8ness invariant, so even though it is not UB if the invariant is violated, people should avoid it where feasible.

PiperOrigin-RevId: 878704268
2026-03-04 15:51:59 -08:00
Protobuf Team Bot
1a7f883dc0 Remove rerun-if-changed on generated artifacts.
The idea of this was to ensure that the gencode is refreshed if the generated output is modified out-of-band. However, it causes protoc to run on every single build because every time it regenerates the outputs, which in turn updates the timestamps, which causes the next build to regenerate it again.

Fixes https://github.com/protocolbuffers/protobuf/issues/26190

PiperOrigin-RevId: 878120672
2026-03-03 14:17:47 -08:00
Protobuf Team Bot
127b34f445 Automated Code Change
PiperOrigin-RevId: 877789010
2026-03-03 00:27:52 -08:00
Krasimir Georgiev
228aa1663a rust: clear the collect coverage attribute
Collecting coverage for generated sources is generally not intended.

PiperOrigin-RevId: 877328988
2026-03-02 04:19:09 -08:00
Protobuf Team Bot
b1c987696e Make the alias produced by the rust_proto_library macro adopt the specified tags.
PiperOrigin-RevId: 875683533
2026-02-26 06:12:17 -08:00