Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update args on supertraits of supertraits when cloning trait impls #113

Merged
merged 3 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions c-bindings-gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ macro_rules! walk_supertraits { ($t: expr, $types: expr, ($( $($pat: pat)|* => $
} } }

macro_rules! get_module_type_resolver {
($module: expr, $crate_libs: expr, $crate_types: expr) => { {
let module: &str = &$module;
($type_in_module: expr, $crate_types: expr) => { {
let module: &str = &$type_in_module;
let mut module_iter = module.rsplitn(2, "::");
module_iter.next().unwrap();
let module = module_iter.next().unwrap();
Expand Down Expand Up @@ -304,7 +304,7 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
(s, _i, _) => {
if let Some(supertrait) = types.crate_types.traits.get(s) {
supertrait_name = s.to_string();
supertrait_resolver = get_module_type_resolver!(supertrait_name, types.crate_libs, types.crate_types);
supertrait_resolver = get_module_type_resolver!(supertrait_name, types.crate_types);
gen_types.learn_associated_types(&supertrait, &supertrait_resolver);
break;
}
Expand Down Expand Up @@ -600,10 +600,10 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
},
(s, i, generic_args) => {
if let Some(supertrait) = types.crate_types.traits.get(s) {
let resolver = get_module_type_resolver!(s, types.crate_libs, types.crate_types);
let resolver = get_module_type_resolver!(s, types.crate_types);
macro_rules! impl_supertrait {
($s: expr, $supertrait: expr, $i: expr, $generic_args: expr) => {
let resolver = get_module_type_resolver!($s, types.crate_libs, types.crate_types);
let resolver = get_module_type_resolver!($s, types.crate_types);

// Blindly assume that the same imports where `supertrait` is defined are also
// imported here. This will almost certainly break at some point, but it should be
Expand Down Expand Up @@ -971,7 +971,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
(s, _i, _) => {
if let Some(supertrait) = types.crate_types.traits.get(s) {
supertrait_name = s.to_string();
supertrait_resolver = get_module_type_resolver!(supertrait_name, types.crate_libs, types.crate_types);
supertrait_resolver = get_module_type_resolver!(supertrait_name, types.crate_types);
gen_types.learn_associated_types(&supertrait, &supertrait_resolver);
break;
}
Expand All @@ -982,7 +982,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
// mappings from a trai defined in a different file, we may mis-resolve or
// fail to resolve the mapped types. Thus, we have to construct a new
// resolver for the module that the trait was defined in here first.
let mut trait_resolver = get_module_type_resolver!(full_trait_path, types.crate_libs, types.crate_types);
let mut trait_resolver = get_module_type_resolver!(full_trait_path, types.crate_types);
gen_types.learn_associated_types(trait_obj, &trait_resolver);
let mut impl_associated_types = HashMap::new();
for item in i.items.iter() {
Expand Down Expand Up @@ -1115,7 +1115,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
}
write_impl_fields!(s, supertrait_obj, t, "\t", types);

let resolver = get_module_type_resolver!(s, types.crate_libs, types.crate_types);
let resolver = get_module_type_resolver!(s, types.crate_types);
walk_supertraits!(supertrait_obj, Some(&resolver), (
(s, t, _) => {
if let Some(supertrait_obj) = types.crate_types.traits.get(s) {
Expand Down Expand Up @@ -1254,15 +1254,22 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
writeln!(w, "extern \"C\" fn {}_{}_cloned(new_obj: &mut crate::{}) {{", trait_obj.ident, ident, full_trait_path).unwrap();
writeln!(w, "\tnew_obj.this_arg = {}_clone_void(new_obj.this_arg);", ident).unwrap();
writeln!(w, "\tnew_obj.free = Some({}_free_void);", ident).unwrap();
walk_supertraits!(trait_obj, Some(&types), (
(s, t, _) => {
if types.crate_types.traits.get(s).is_some() {
assert!(!types.is_clonable(s)); // We don't currently support cloning with a clonable supertrait
writeln!(w, "\tnew_obj.{}.this_arg = new_obj.this_arg;", t).unwrap();
writeln!(w, "\tnew_obj.{}.free = None;", t).unwrap();

fn seek_supertraits<W: std::io::Write>(w: &mut W, pfx: &str, tr: &syn::ItemTrait, types: &TypeResolver) {
walk_supertraits!(tr, Some(types), (
(s, t, _) => {
if types.crate_types.traits.get(s).is_some() {
assert!(!types.is_clonable(s)); // We don't currently support cloning with a clonable supertrait
writeln!(w, "\tnew_obj.{}{}.this_arg = new_obj.this_arg;", pfx, t).unwrap();
writeln!(w, "\tnew_obj.{}{}.free = None;", pfx, t).unwrap();
let tr = types.crate_types.traits.get(s).unwrap();
let resolver = get_module_type_resolver!(s, types.crate_types);
seek_supertraits(w, &format!("{}.", t), tr, &resolver);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non-blocking nit: Would be a bit clearer if we made pfx an Option<&str> and adjusted the format string accordingly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That feels less readable to me - it's more code and not doing anything other than maintaining the prefix (which still can be arbitrarily long). Would you prefer a different variable name instead?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, then feel free to leave as is.

}
}
}
) );
) );
}
seek_supertraits(w, "", trait_obj, types);
writeln!(w, "}}").unwrap();
}
write!(w, "\n").unwrap();
Expand Down
2 changes: 2 additions & 0 deletions lightning-c-bindings/src/lightning/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2188,6 +2188,8 @@ extern "C" fn WriteableEcdsaChannelSigner_InMemorySigner_cloned(new_obj: &mut cr
new_obj.free = Some(InMemorySigner_free_void);
new_obj.EcdsaChannelSigner.this_arg = new_obj.this_arg;
new_obj.EcdsaChannelSigner.free = None;
new_obj.EcdsaChannelSigner.ChannelSigner.this_arg = new_obj.this_arg;
new_obj.EcdsaChannelSigner.ChannelSigner.free = None;
}

#[no_mangle]
Expand Down
Loading