Learn supertrait associated types when processing traits
authorMatt Corallo <git@bluematt.me>
Wed, 22 Sep 2021 04:13:16 +0000 (04:13 +0000)
committerMatt Corallo <git@bluematt.me>
Thu, 23 Sep 2021 17:36:18 +0000 (17:36 +0000)
c-bindings-gen/src/main.rs

index 5bc27eb9b7317301d2bf0151854a2a50ab613d2e..a114c2289c921d8156009942485ac453673c8709 100644 (file)
@@ -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: std::io::Write>(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 {