From f303e5776622e785ae77de1cdf03a788bb98eb57 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Wed, 22 Sep 2021 04:13:16 +0000 Subject: [PATCH] Learn supertrait associated types when processing traits --- c-bindings-gen/src/main.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) 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 { -- 2.39.5