Commit graph

31 commits

Author SHA1 Message Date
Protobuf Team Bot
83ec410d30 Internal change
PiperOrigin-RevId: 619377672
2024-03-27 05:50:55 -07:00
Jakob Buchgraber
6f1d88107f Support proto_library targets that contain '-'
Crate names must not contain '-'. So we replace any '-' by a '_' to form a valid crate name.

Bazel supports many special characters as target names: !%-@^_"#$&'()*-+,;<=>?[]{|}~/. We don't have to support all of them in Protobuf Rust, but '-' seems used widely enough and is a common alternative to '_' in naming.

PiperOrigin-RevId: 602963206
2024-01-31 00:48:10 -08:00
Jakob Buchgraber
90015d3145 #protobuf #rust Generate fields with imported types
Before this change if a field type was defined in a imported .proto file then our codegen would not generate the field. After this change such fields are correctly generated (see tests). This change is rather trivial as all the supporting infra has been implemented as part of the .proto -> crate mapping CLs.

PiperOrigin-RevId: 601443383
2024-01-25 07:46:38 -08:00
Jakob Buchgraber
4517308a91 Also apply r# prefix to crates whose names are reserved keywords
PiperOrigin-RevId: 601116457
2024-01-24 07:40:46 -08:00
Marcel Hlopko
0f4cf16815 Depend directly on cc_proto_aspect in rust_cc_proto_library
We had to wait for cc_proto_aspect to be exposed in Bazel. Now it is, so we can
simplify our build rules

PiperOrigin-RevId: 600362773
2024-01-21 23:49:11 -08:00
Marcel Hlopko
c1d174f7d2 Stop codegenning module for .proto package
PiperOrigin-RevId: 599856754
2024-01-19 09:35:46 -08:00
Alyssa Haroldsen
1eab5a0237 Generate pub use for imported enums
PiperOrigin-RevId: 596762218
2024-01-08 18:52:27 -08:00
Adam Cozzette
4ec9170bcd Fix layering check for usage of gtest
To satisfy the layering check, we need to depend on :gtest for the headers, in
addition to :gtest_main which provides the main() function.

There are a bunch of formatting changes as a side effect of this, but they
should be harmless.

PiperOrigin-RevId: 594318263
2023-12-28 14:41:48 -08:00
Alyssa Haroldsen
0ce51da377 Implement v0.6 enum definitions
This does not implement accessors, just the enum definitions themselves.

PiperOrigin-RevId: 592591476
2023-12-20 10:10:17 -08:00
Protobuf Team Bot
a47a7bdc8d Internal Code Change
PiperOrigin-RevId: 590074377
2023-12-11 23:04:16 -08:00
Protobuf Team Bot
43f92cb916 Add a trivial 2023 edition test case for Rust.
PiperOrigin-RevId: 588874094
2023-12-07 12:30:52 -08:00
Jakob Buchgraber
001be3c655 Implement Maps for scalar types for the C++ kernel
This CL implements Maps for scalar types for the C++ runtime. It's orthogonal to cl/580453646. This CL is constrained by having to force template instantiation of proto2::Map<K, V>. Put differently, a Rust protobuf::Map<K, V> implementation needs to call 'extern "C"' functions with both key and value type in the function name (e.g. __pb_rust_Map_i32_f64_get()). We use macros to generate a Map implementation for every (K,V)-pair. An alternative would have been to use vtables.

Luckily a key in a protobuf map can only be integer types, bool and string. So the number of key types is bounded by the specification, while the number of value types is not i.e. any protobuf message can be a value in a map. Given these constraints we introduce one 'MapKeyOps' trait per key type e.g. MapKeyBOOLOps or MapKeyI32Ops. These traits need to be implemented for every value type e.g. 'impl MapKeyBOOLOps for i32' will implement 'Map::<bool, i32>'. In particular the MapKeyOps traits can also be implemented for generated messages without violating the orphan rule.

This CL also contains significant changes to the UPB runtime so that both upb.rs and cpp.rs export a similar interface to simplify the implementation in  map.rs and the generated code.

This CL does not yet implement the Proxied trait.

PiperOrigin-RevId: 582951914
2023-11-16 01:38:42 -08:00
Protobuf Team Bot
1388e818a7 Internal Code Change
PiperOrigin-RevId: 582905979
2023-11-15 21:48:40 -08:00
Jakob Buchgraber
ac3f553073 This CL implements msg.<field>() and msg.<field_mut>() accessors for maps with primitive-typed keys and values for the UPB kernel only.
Support for the CPP runtime and non-scalar value types will be implemented in follow up CLs.

PiperOrigin-RevId: 580453646
2023-11-08 02:16:21 -08:00
Hong Shin
80a4df3306 Add support accessing simple scalars (int32s, bools, and floats) in submessages
PiperOrigin-RevId: 566403598
2023-09-18 13:51:49 -07:00
Protobuf Team Bot
a773618181 Internal Code Change
PiperOrigin-RevId: 561592059
2023-08-31 02:14:17 -07:00
Alyssa Haroldsen
9a0bc392b3 Implement v0.6 Optional Bytes
This makes a few changes:
 - It changes generated messages to reference message innards as a type in `__runtime` instead of branching on what fields should be there. That results in much less bifurcation in gencode and lets runtime-agnostic code reference raw message innards.
- It adds a generic mechanism for creating vtable-based mutators. These vtables point to thunks generated for interacting with C++ or upb fields. Right now, the design results in 2-word (msg+vtable) mutators for C++ and 3-word mutators (msg+arena+vtable) for UPB. See upb.rs for an explanation of the design options. I chose the `RawMessage+&Arena` design for mutator data as opposed to a `&MessageInner` design because it did not result in extra-indirection layout changes for message mutators. We could revisit this in the future with performance data, since this results in all field mutators being 3 words large instead of the register-friendly 2 words.
- And lastly, as a nearby change that touches on many of the same topics, it adds some extra SAFETY comments for Send/Sync in message gencode.

PiperOrigin-RevId: 559483437
2023-08-23 11:08:21 -07:00
Protobuf Team Bot
9a7ca55ca5 Support without-presence scalars better.
Only emit has_field() if the field support presence. Only emit field_opt() getter if the field is both optional and supports presence.

PiperOrigin-RevId: 555133374
2023-08-09 05:48:11 -07:00
Protobuf Team Bot
dd4ffc65cc Internal Code Change
PiperOrigin-RevId: 550163810
2023-07-22 02:36:27 -07:00
Marcel Hlopko
3ced818640 Add support for proto_library with more than one srcs
If a proto_library has more than one srcs, we designate the first one as the primary (that file will be passed to rustc as the crate root). All other files will represent (internal) submodules of the crate.

In general, Rust users won't see which .proto file a message came from, they will only see a crate corresponding to the entire proto_library, and in it public submodules for all `package` statements in all .proto files in the proto_library sources. Therefore in this CL we reexport all messages from non primary sources into their corresponding public modules (= packages declared in their owning .proto files).

Besides the common case this CL also handles .proto files without package statement, and a subset of behaviors needed for public import functionality.

PiperOrigin-RevId: 549543321
2023-07-20 00:35:54 -07:00
Hong Shin
b01794cb3b Add test that ensures rust protos build with reserved keywords
Also confirmed that this fails to build without the `r#` inside singular_scalar.cc

PiperOrigin-RevId: 546906226
2023-07-10 10:15:09 -07:00
Marcel Hlopko
a1ba8d238a Generate bindings under the correct package.
Before this CL all messages were generated in the top-level crate module. With
this change we generate messages under the module specified by the package
declaration in the .proto file.

Dots are interpreted as submodule separator in consistency with how C++
namespaces are handled.

Note that name of the proto_library target still remains to be used as the crate name. This CL only adds crate submodules dependeing on the specified package.

PiperOrigin-RevId: 524235162
2023-04-14 02:24:59 -07:00
Marcel Hlopko
5814f6c977 Generate C++ thunks for Rust protos
In this CL I'd like to call existing C++ Protobuf API from the V0 Rust API. Since parts of the C++ API are defined inline and using (obviously) C++ name mangling, we need to create a "thunks.cc" file that:

1) Generates code for C++ API function we use from Rust
2) Exposes these functions without any name mangling (meaning using `extern "C"`)

In this CL we add Bazel logic to generate "thunks" file, compile it, and propagate its object to linking. We also add logic to protoc to generate this "thunks" file.

The protoc logic is rather rudimentary still. I hope to focus on protoc code quality in my followup work on V0 Rust API using C++ kernel.

PiperOrigin-RevId: 523479839
2023-04-11 12:58:34 -07:00
Marcel Hlopko
f7a2f4acea Add initial end-to-end test directory structure.
This turns out to be quite of a yak shave to be able to perfectly test both kernels without having to pass extra Blaze flags.

PiperOrigin-RevId: 521850709
2023-04-04 13:31:07 -07:00
Jakob Buchgraber
7802b93442 Fix build errors in Protobuf Rust
PiperOrigin-RevId: 518619271
2023-03-22 11:08:58 -07:00
Protobuf Team Bot
f73c7cb74a Add license rules
PiperOrigin-RevId: 518370510
2023-03-21 13:49:49 -07:00
Marcel Hlopko
aaa338b285 Configure build for the C++ backend
PiperOrigin-RevId: 517429571
2023-03-17 09:22:54 -07:00
Marcel Hlopko
ab9f1ab58a Configure the build for the Rust UPB backend
In this CL we're adding the barebones infrastructure to generate Rust proto messages using UPB as a backend. The API is what we call a V0, not yet production-quality, not yet rigorously designed, just something to enable parallel work.

The interesting part of switching backend between UPB and C++ will come in a followup.

PiperOrigin-RevId: 517089760
2023-03-16 04:58:37 -07:00
Marcel Hlopko
5252bc8ec8 Rename a test
PiperOrigin-RevId: 515628124
2023-03-10 07:10:12 -08:00
Marcel Hlopko
26af540a7a Add support for proto dependencies to rust_proto_library
PiperOrigin-RevId: 514719215
2023-03-07 06:51:52 -08:00
Marcel Hlopko
3dc546daff Implement a minimal, internal, experimental rust_proto_library rule.
The internal design is consistent with other <lang>_proto_library rules. rust_proto_library attaches rust_proto_library_aspect on its `deps` attribute. The aspect traverses the dependency, and when it visits proto_library (detected by ProtoInfo provider) it registers 2 actions:

1) to run protoc with Rust backend to emit gencode
2) to compile the gencode using Rustc

Action (2) gets the Rust proto runtime as an input as well.

Coming in a followup is support and test coverage for proto_library.deps.

PiperOrigin-RevId: 514521285
2023-03-06 14:11:18 -08:00