From 4edf514a055fe523970d99a8e8f06380c934010d Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Wed, 25 Nov 2020 11:59:58 -0500 Subject: [PATCH] [bindings] Always resolve supertrait types during supertrait walks 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 | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index 7917c36d..c0c5ecee 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -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: &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: &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) => { -- 2.30.2