From: Matt Corallo Date: Wed, 22 Sep 2021 04:13:16 +0000 (+0000) Subject: Learn supertrait associated types when processing traits X-Git-Tag: v0.0.101.1~1^2~17 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;ds=inline;h=f303e5776622e785ae77de1cdf03a788bb98eb57;p=ldk-c-bindings Learn supertrait associated types when processing traits --- diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index 5bc27eb..a114c22 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -241,6 +241,22 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty writeln_docs(w, &t.attrs, ""); let mut gen_types = GenericTypes::new(None); + + // Add functions which may be required for supertrait implementations. + // Due to borrow checker limitations, we only support one in-crate supertrait here. + let supertrait_name; + let supertrait_resolver; + walk_supertraits!(t, Some(&types), ( + (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); + gen_types.learn_associated_types(&supertrait, &supertrait_resolver); + break; + } + } + ) ); + assert!(gen_types.learn_generics(&t.generics, types)); gen_types.learn_associated_types(&t, types); @@ -757,11 +773,26 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ if types.understood_c_path(&trait_path.1) { let full_trait_path = types.resolve_path(&trait_path.1, None); let trait_obj = *types.crate_types.traits.get(&full_trait_path).unwrap(); + + let supertrait_name; + let supertrait_resolver; + walk_supertraits!(trait_obj, Some(&types), ( + (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); + gen_types.learn_associated_types(&supertrait, &supertrait_resolver); + break; + } + } + ) ); // We learn the associated types maping from the original trait object. // That's great, except that they are unresolved idents, so if we learn // mappings from a trai defined in a different file, we may mis-resolve or - // fail to resolve the mapped types. - gen_types.learn_associated_types(trait_obj, types); + // 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); + gen_types.learn_associated_types(trait_obj, &trait_resolver); let mut impl_associated_types = HashMap::new(); for item in i.items.iter() { match item {