Skip to content

Commit e3697ba

Browse files
committed
fix(cpp): const_cast map keys during lowering for ownership transfer
std::map keys are const, but the ABI lowering needs to call leak() on string keys to transfer ownership to the flat buffer. Use const_cast since the map is consumed during the lowering operation.
1 parent 44cad6d commit e3697ba

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

crates/cpp/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3560,6 +3560,8 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
35603560
let ptr = format!("ptr{tmp}");
35613561
let len = format!("len{tmp}");
35623562
let idx = format!("idx{tmp}");
3563+
let entry_name = format!("entry{tmp}");
3564+
let key_type = self.r#gen.type_name(key, &self.namespace, Flavor::InStruct);
35633565
let entry = self.r#gen.sizes.record([*key, *value]);
35643566
let size = entry.size.format(POINTER_SIZE_EXPRESSION);
35653567
let align = entry.align.format(POINTER_SIZE_EXPRESSION);
@@ -3571,10 +3573,12 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
35713573
ptr_type = self.r#gen.r#gen.opts.ptr_type()
35723574
);
35733575
uwriteln!(self.src, "size_t {idx} = 0;");
3576+
uwriteln!(self.src, "for (auto& {entry_name} : {val}) {{");
35743577
uwriteln!(
35753578
self.src,
3576-
"for (auto&& [iter_map_key, iter_map_value] : {val}) {{"
3579+
"auto& iter_map_key = const_cast<{key_type}&>({entry_name}.first);"
35773580
);
3581+
uwriteln!(self.src, "auto& iter_map_value = {entry_name}.second;");
35783582
uwriteln!(self.src, "auto base = {ptr} + {idx} * {size};");
35793583
uwrite!(self.src, "{}", body.0);
35803584
uwriteln!(self.src, "++{idx};");

0 commit comments

Comments
 (0)