[bindings] Always resolve supertrait types during supertrait walks
authorMatt Corallo <git@bluematt.me>
Wed, 25 Nov 2020 16:59:58 +0000 (11:59 -0500)
committerMatt Corallo <git@bluematt.me>
Tue, 2 Feb 2021 22:04:31 +0000 (17:04 -0500)
This is a rather trivial cleanup to ensure we always have the full
path when we walk supertraits even if the supertrait is specified
with only a single ident.

c-bindings-gen/src/main.rs

index 7917c36d841256cc2c4cec5f5dba74cdc5fa1d4c..c0c5ecee1e9b0d4522d59c94658a52855b2051bf 100644 (file)
@@ -92,15 +92,18 @@ macro_rules! walk_supertraits { ($t: expr, $types: expr, ($( $pat: pat => $e: ex
                                        if supertrait.paren_token.is_some() || supertrait.lifetimes.is_some() {
                                                unimplemented!();
                                        }
-                                       if let Some(ident) = supertrait.path.get_ident() {
-                                               match (&format!("{}", ident) as &str, &ident) {
+                                       // First try to resolve path to find in-crate traits, but if that doesn't work
+                                       // assume its a prelude trait (eg Clone, etc) and just use the single ident.
+                                       if let Some(path) = $types.maybe_resolve_path(&supertrait.path, None) {
+                                               match (&path as &str, &supertrait.path.segments.iter().last().unwrap().ident) {
                                                        $( $pat => $e, )*
                                                }
-                                       } else {
-                                               let path = $types.resolve_path(&supertrait.path, None);
-                                               match (&path as &str, &supertrait.path.segments.iter().last().unwrap().ident) {
+                                       } else if let Some(ident) = supertrait.path.get_ident() {
+                                               match (&format!("{}", ident) as &str, &ident) {
                                                        $( $pat => $e, )*
                                                }
+                                       } else {
+                                               panic!("Supertrait unresolvable and not single-ident");
                                        }
                                },
                                syn::TypeParamBound::Lifetime(_) => unimplemented!(),
@@ -661,8 +664,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
                                                                writeln!(w, "\t\tclone: Some({}_clone_void),", ident).unwrap();
                                                        },
                                                        (s, t) => {
-                                                               if s.starts_with("util::") {
-                                                                       let supertrait_obj = types.crate_types.traits.get(s).unwrap();
+                                                               if let Some(supertrait_obj) = types.crate_types.traits.get(s) {
                                                                        writeln!(w, "\t\t{}: crate::{} {{", t, s).unwrap();
                                                                        writeln!(w, "\t\t\tthis_arg: unsafe {{ (*this_arg).inner as *mut c_void }},").unwrap();
                                                                        writeln!(w, "\t\t\tfree: None,").unwrap();
@@ -753,9 +755,8 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
                                                }
                                                walk_supertraits!(trait_obj, types, (
                                                        (s, t) => {
-                                                               if s.starts_with("util::") {
+                                                               if let Some(supertrait_obj) = types.crate_types.traits.get(s).cloned() {
                                                                        writeln!(w, "use {}::{} as native{}Trait;", types.orig_crate, s, t).unwrap();
-                                                                       let supertrait_obj = *types.crate_types.traits.get(s).unwrap();
                                                                        for item in supertrait_obj.items.iter() {
                                                                                match item {
                                                                                        syn::TraitItem::Method(m) => {