From: Matt Corallo Date: Fri, 23 Sep 2022 16:18:55 +0000 (+0000) Subject: Use trait, not impl, definition for X_as_Trait functions X-Git-Tag: v0.0.111.0~1^2~6 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=a5137ff3bd343300a902be14aa4315fd4ac37353;p=ldk-c-bindings Use trait, not impl, definition for X_as_Trait functions When we map a trait impl block, we historically didn't know how to resolve the types in the trait context, so we just used the impl context definitions and assumed the types were the same. Sadly, if we have an associated type that is bounded by a trait, we need to use the trait bound to figure out what to write for the X_as_Trait method declarations. Since we now have the ability to generate full trait-context type resolvers, we do so here. --- diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index 9c11cf3..2aeadaf 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -947,7 +947,7 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ // 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 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_libs, 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() { @@ -1107,17 +1107,12 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ _ => {} } } - if uncallable_function { - let mut trait_resolver = get_module_type_resolver!(full_trait_path, $types.crate_libs, $types.crate_types); - write_method_params(w, &$trait_meth.sig, "c_void", &mut trait_resolver, Some(&meth_gen_types), true, true); - } else { - write_method_params(w, &$m.sig, "c_void", $types, Some(&meth_gen_types), true, true); - } + write_method_params(w, &$trait_meth.sig, "c_void", &mut trait_resolver, Some(&meth_gen_types), true, true); write!(w, " {{\n\t").unwrap(); if uncallable_function { write!(w, "unreachable!();").unwrap(); } else { - write_method_var_decl_body(w, &$m.sig, "", $types, Some(&meth_gen_types), false); + write_method_var_decl_body(w, &$trait_meth.sig, "", &mut trait_resolver, Some(&meth_gen_types), false); let mut takes_self = false; for inp in $m.sig.inputs.iter() { if let syn::FnArg::Receiver(_) = inp { @@ -1147,7 +1142,7 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ }, _ => {}, } - write_method_call_params(w, &$m.sig, "", $types, Some(&meth_gen_types), &real_type, false); + write_method_call_params(w, &$trait_meth.sig, "", &mut trait_resolver, Some(&meth_gen_types), &real_type, false); } write!(w, "\n}}\n").unwrap(); if let syn::ReturnType::Type(_, rtype) = &$m.sig.output { @@ -1185,7 +1180,6 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ assert!(meth.default.is_some()); let old_gen_types = gen_types; gen_types = GenericTypes::new(Some(resolved_path.clone())); - let mut trait_resolver = get_module_type_resolver!(full_trait_path, types.crate_libs, types.crate_types); impl_meth!(meth, meth, full_trait_path, trait_obj, "", &mut trait_resolver); gen_types = old_gen_types; },