Skip to content

Commit 3c38452

Browse files
committed
fix(cpp): copy map keys by value instead of const_cast during lowering
const_cast fails when the map key type differs between contexts (e.g. std::string_view in imports vs wit::string in exports). Copying by value works universally and is safe since the map is consumed.
1 parent e3697ba commit 3c38452

File tree

1 file changed

+1
-5
lines changed

1 file changed

+1
-5
lines changed

crates/cpp/src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3561,7 +3561,6 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
35613561
let len = format!("len{tmp}");
35623562
let idx = format!("idx{tmp}");
35633563
let entry_name = format!("entry{tmp}");
3564-
let key_type = self.r#gen.type_name(key, &self.namespace, Flavor::InStruct);
35653564
let entry = self.r#gen.sizes.record([*key, *value]);
35663565
let size = entry.size.format(POINTER_SIZE_EXPRESSION);
35673566
let align = entry.align.format(POINTER_SIZE_EXPRESSION);
@@ -3574,10 +3573,7 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
35743573
);
35753574
uwriteln!(self.src, "size_t {idx} = 0;");
35763575
uwriteln!(self.src, "for (auto& {entry_name} : {val}) {{");
3577-
uwriteln!(
3578-
self.src,
3579-
"auto& iter_map_key = const_cast<{key_type}&>({entry_name}.first);"
3580-
);
3576+
uwriteln!(self.src, "auto iter_map_key = {entry_name}.first;");
35813577
uwriteln!(self.src, "auto& iter_map_value = {entry_name}.second;");
35823578
uwriteln!(self.src, "auto base = {ptr} + {idx} * {size};");
35833579
uwrite!(self.src, "{}", body.0);

0 commit comments

Comments
 (0)