Stop using upb C accessor codegen for submsg accessors. PiperOrigin-RevId: 668009483
diff --git a/rust/upb/message.rs b/rust/upb/message.rs index caaa80a..4532c56 100644 --- a/rust/upb/message.rs +++ b/rust/upb/message.rs
@@ -24,7 +24,7 @@ pub fn upb_Message_Clear(m: RawMessage, mini_table: *const upb_MiniTable); /// # Safety - /// - `m` and `mini_table` must be valid to deref + /// - `m` and `f` must be valid to deref /// - `f` must be a field associated with `f` pub fn upb_Message_ClearBaseField(m: RawMessage, f: *const upb_MiniTableField); @@ -49,17 +49,17 @@ ) -> Option<RawMessage>; /// # Safety - /// - `m` and `mini_table` must be valid to deref - /// - `f` must be a field associated with `f` + /// - `m` and `f` must be valid to deref + /// - `f` must be a bool field associated with `m` pub fn upb_Message_GetBool( m: RawMessage, - mini_table: *const upb_MiniTableField, + f: *const upb_MiniTableField, default_val: bool, ) -> bool; /// # Safety - /// - `m` and `mini_table` must be valid to deref - /// - `f` must be a field associated with `m` + /// - `m` and `f` must be valid to deref + /// - `f` must be an i32 field associated with `m` pub fn upb_Message_GetInt32( m: RawMessage, f: *const upb_MiniTableField, @@ -67,8 +67,8 @@ ) -> i32; /// # Safety - /// - `m` and `mini_table` must be valid to deref - /// - `f` must be a field associated with `m` + /// - `m` and `f` must be valid to deref + /// - `f` must be an i64 field associated with `m` pub fn upb_Message_GetInt64( m: RawMessage, f: *const upb_MiniTableField, @@ -76,8 +76,8 @@ ) -> i64; /// # Safety - /// - `m` and `mini_table` must be valid to deref - /// - `f` must be a field associated with `m` + /// - `m` and `f` must be valid to deref + /// - `f` must be a u32 field associated with `m` pub fn upb_Message_GetUInt32( m: RawMessage, f: *const upb_MiniTableField, @@ -85,8 +85,8 @@ ) -> u32; /// # Safety - /// - `m` and `mini_table` must be valid to deref - /// - `f` must be a field associated with `m` + /// - `m` and `f` must be valid to deref + /// - `f` must be a u64 field associated with `m` pub fn upb_Message_GetUInt64( m: RawMessage, f: *const upb_MiniTableField, @@ -94,8 +94,8 @@ ) -> u64; /// # Safety - /// - `m` and `mini_table` must be valid to deref - /// - `f` must be a field associated with `m` + /// - `m` and `f` must be valid to deref + /// - `f` must be a f32 field associated with `m` pub fn upb_Message_GetFloat( m: RawMessage, f: *const upb_MiniTableField, @@ -103,8 +103,8 @@ ) -> f32; /// # Safety - /// - `m` and `mini_table` must be valid to deref - /// - `f` must be a field associated with `m` + /// - `m` and `f` must be valid to deref + /// - `f` must be a f64 field associated with `m` pub fn upb_Message_GetDouble( m: RawMessage, f: *const upb_MiniTableField, @@ -112,15 +112,34 @@ ) -> f64; /// # Safety - /// - `m` and `mini_table` must be valid to deref - /// - `mini_table` must be the MiniTable associated with `m` - pub fn upb_Message_HasBaseField(m: RawMessage, mini_table: *const upb_MiniTableField) -> bool; + /// - `m` and `f` must be valid to deref + /// - `f` must be a message-typed field associated with `m` + pub fn upb_Message_GetMessage( + m: RawMessage, + f: *const upb_MiniTableField, + ) -> Option<RawMessage>; /// # Safety - /// - `m` and `mini_table` must be valid to deref + /// - All arguments must be valid to deref + /// - `mini_table` must be the MiniTable associated with `m` + /// - `f` must be a message-typed field associated with `m` + pub fn upb_Message_GetOrCreateMutableMessage( + m: RawMessage, + mini_table: *const upb_MiniTable, + f: *const upb_MiniTableField, + arena: RawArena, + ) -> Option<RawMessage>; + + /// # Safety + /// - `m` and `f` must be valid to deref + /// - `mini_table` must be the MiniTable associated with `m` + pub fn upb_Message_HasBaseField(m: RawMessage, f: *const upb_MiniTableField) -> bool; + + /// # Safety + /// - `m` and `f` must be valid to deref /// - `f` must be a field associated with `m` /// - `val` must be a pointer to legally readable memory of the correct type - /// for the field described by `mini_table` + /// for the field described by `f` pub fn upb_Message_SetBaseField( m: RawMessage, f: *const upb_MiniTableField, @@ -151,41 +170,46 @@ ) -> bool; /// # Safety - /// - `m` and `mini_table` must be valid to deref - /// - `f` must be a field associated with `f` - pub fn upb_Message_SetBaseFieldBool( - m: RawMessage, - mini_table: *const upb_MiniTableField, - val: bool, - ); + /// - `m` and `f` must be valid to deref + /// - `f` must be a bool field associated with `f` + pub fn upb_Message_SetBaseFieldBool(m: RawMessage, f: *const upb_MiniTableField, val: bool); /// # Safety - /// - `m` and `mini_table` must be valid to deref - /// - `f` must be a field associated with `m` + /// - `m` and `f` must be valid to deref + /// - `f` must be an i32 field associated with `m` pub fn upb_Message_SetBaseFieldInt32(m: RawMessage, f: *const upb_MiniTableField, val: i32); /// # Safety - /// - `m` and `mini_table` must be valid to deref - /// - `f` must be a field associated with `m` + /// - `m` and `f` must be valid to deref + /// - `f` must be an i64 field associated with `m` pub fn upb_Message_SetBaseFieldInt64(m: RawMessage, f: *const upb_MiniTableField, val: i64); /// # Safety - /// - `m` and `mini_table` must be valid to deref - /// - `f` must be a field associated with `m` + /// - `m` and `f` must be valid to deref + /// - `f` must be a u32 field associated with `m` pub fn upb_Message_SetBaseFieldUInt32(m: RawMessage, f: *const upb_MiniTableField, val: u32); /// # Safety - /// - `m` and `mini_table` must be valid to deref - /// - `f` must be a field associated with `m` + /// - `m` and `f` must be valid to deref + /// - `f` must be a u64 field associated with `m` pub fn upb_Message_SetBaseFieldUInt64(m: RawMessage, f: *const upb_MiniTableField, val: u64); /// # Safety - /// - `m` and `mini_table` must be valid to deref - /// - `f` must be a field associated with `m` + /// - `m` and `f` must be valid to deref + /// - `f` must be an f32 field associated with `m` pub fn upb_Message_SetBaseFieldFloat(m: RawMessage, f: *const upb_MiniTableField, val: f32); /// # Safety - /// - `m` and `mini_table` must be valid to deref - /// - `f` must be a field associated with `m` + /// - `m` and `f` must be valid to deref + /// - `f` must be an f64 field associated with `m` pub fn upb_Message_SetBaseFieldDouble(m: RawMessage, f: *const upb_MiniTableField, val: f64); + + /// # Safety + /// - `m` and `f` must be valid to deref + /// - `f` must be a message-typed field associated with `m` + pub fn upb_Message_SetBaseFieldMessage( + m: RawMessage, + f: *const upb_MiniTableField, + val: RawMessage, + ); }
diff --git a/src/google/protobuf/compiler/rust/accessors/singular_message.cc b/src/google/protobuf/compiler/rust/accessors/singular_message.cc index b6752eb..cd51c2b 100644 --- a/src/google/protobuf/compiler/rust/accessors/singular_message.cc +++ b/src/google/protobuf/compiler/rust/accessors/singular_message.cc
@@ -7,6 +7,7 @@ #include <string> +#include "absl/log/absl_check.h" #include "absl/strings/string_view.h" #include "google/protobuf/compiler/cpp/helpers.h" #include "google/protobuf/compiler/rust/accessors/accessor_case.h" @@ -14,6 +15,7 @@ #include "google/protobuf/compiler/rust/accessors/with_presence.h" #include "google/protobuf/compiler/rust/context.h" #include "google/protobuf/compiler/rust/naming.h" +#include "google/protobuf/compiler/rust/upb_helpers.h" #include "google/protobuf/descriptor.h" namespace google { @@ -40,19 +42,25 @@ {"getter_thunk", ThunkName(ctx, field, "get")}, {"getter_mut_thunk", ThunkName(ctx, field, "get_mut")}, {"set_allocated_thunk", ThunkName(ctx, field, "set")}, + {"upb_mt_field_index", UpbMiniTableFieldIndex(field)}, { "getter_body", [&] { if (ctx.is_upb()) { ctx.Emit({}, R"rs( - let submsg = unsafe { $getter_thunk$(self.raw_msg()) }; + let submsg = unsafe { + let f = $pbr$::upb_MiniTable_GetFieldByIndex( + <Self as $pbr$::AssociatedMiniTable>::mini_table(), + $upb_mt_field_index$); + $pbr$::upb_Message_GetMessage(self.raw_msg(), f) + }; //~ For upb, getters return null if the field is unset, so we need //~ to check for null and return the default instance manually. //~ Note that a nullptr received from upb manifests as Option::None match submsg { //~ TODO:(b/304357029) None => $msg_type$View::new($pbi$::Private, $pbr$::ScratchSpace::zeroed_block()), - Some(field) => $msg_type$View::new($pbi$::Private, field), + Some(sub_raw_msg) => $msg_type$View::new($pbi$::Private, sub_raw_msg), } )rs"); } else { @@ -84,7 +92,10 @@ } else { ctx.Emit({}, R"rs( let raw_msg = unsafe { - $getter_mut_thunk$(self.raw_msg(), self.arena().raw()) + let mt = <Self as $pbr$::AssociatedMiniTable>::mini_table(); + let f = $pbr$::upb_MiniTable_GetFieldByIndex(mt, $upb_mt_field_index$); + $pbr$::upb_Message_GetOrCreateMutableMessage( + self.raw_msg(), mt, f, self.arena().raw()).unwrap() }; $msg_type$Mut::from_parent($pbi$::Private, self.as_mutator_message_ref($pbi$::Private), raw_msg) @@ -116,7 +127,12 @@ .fuse(msg.as_mutator_message_ref($pbi$::Private).arena()); unsafe { - $set_allocated_thunk$(self.as_mutator_message_ref($pbi$::Private).msg(), + let f = $pbr$::upb_MiniTable_GetFieldByIndex( + <Self as $pbr$::AssociatedMiniTable>::mini_table(), + $upb_mt_field_index$); + $pbr$::upb_Message_SetBaseFieldMessage( + self.as_mutator_message_ref($pbi$::Private).msg(), + f, msg.as_mutator_message_ref($pbi$::Private).msg()); } )rs"); @@ -153,6 +169,7 @@ void SingularMessage::InExternC(Context& ctx, const FieldDescriptor& field) const { + if (ctx.is_upb()) return; if (field.has_presence()) { WithPresenceAccessorsInExternC(ctx, field); } @@ -198,6 +215,7 @@ void SingularMessage::InThunkCc(Context& ctx, const FieldDescriptor& field) const { + ABSL_CHECK(ctx.is_cpp()); if (field.has_presence()) { WithPresenceAccessorsInThunkCc(ctx, field); }