Skip to content

Commit 6c7a4f5

Browse files
committed
Update args on supertraits of supertraits when cloning trait impls
When cloning implementations of traits from in-crate structs, we forgot to update the arguments for supertraits of supertraits, causing methods on that trait to be called against the previous struct, not the cloned one. This was ultimately identified downstream in the Java bindings, fixes lightningdevkit/ldk-garbagecollected#138
1 parent 10d1bac commit 6c7a4f5

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

c-bindings-gen/src/main.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,15 +1254,22 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
12541254
writeln!(w, "extern \"C\" fn {}_{}_cloned(new_obj: &mut crate::{}) {{", trait_obj.ident, ident, full_trait_path).unwrap();
12551255
writeln!(w, "\tnew_obj.this_arg = {}_clone_void(new_obj.this_arg);", ident).unwrap();
12561256
writeln!(w, "\tnew_obj.free = Some({}_free_void);", ident).unwrap();
1257-
walk_supertraits!(trait_obj, Some(&types), (
1258-
(s, t, _) => {
1259-
if types.crate_types.traits.get(s).is_some() {
1260-
assert!(!types.is_clonable(s)); // We don't currently support cloning with a clonable supertrait
1261-
writeln!(w, "\tnew_obj.{}.this_arg = new_obj.this_arg;", t).unwrap();
1262-
writeln!(w, "\tnew_obj.{}.free = None;", t).unwrap();
1257+
1258+
fn seek_supertraits<W: std::io::Write>(w: &mut W, pfx: &str, tr: &syn::ItemTrait, types: &TypeResolver) {
1259+
walk_supertraits!(tr, Some(types), (
1260+
(s, t, _) => {
1261+
if types.crate_types.traits.get(s).is_some() {
1262+
assert!(!types.is_clonable(s)); // We don't currently support cloning with a clonable supertrait
1263+
writeln!(w, "\tnew_obj.{}{}.this_arg = new_obj.this_arg;", pfx, t).unwrap();
1264+
writeln!(w, "\tnew_obj.{}{}.free = None;", pfx, t).unwrap();
1265+
let tr = types.crate_types.traits.get(s).unwrap();
1266+
let resolver = get_module_type_resolver!(s, types.crate_types);
1267+
seek_supertraits(w, &format!("{}.", t), tr, &resolver);
1268+
}
12631269
}
1264-
}
1265-
) );
1270+
) );
1271+
}
1272+
seek_supertraits(w, "", trait_obj, types);
12661273
writeln!(w, "}}").unwrap();
12671274
}
12681275
write!(w, "\n").unwrap();

0 commit comments

Comments
 (0)