Skip to content

Commit 4edf514

Browse files
committed
[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.
1 parent af3ad4b commit 4edf514

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

c-bindings-gen/src/main.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,18 @@ macro_rules! walk_supertraits { ($t: expr, $types: expr, ($( $pat: pat => $e: ex
9292
if supertrait.paren_token.is_some() || supertrait.lifetimes.is_some() {
9393
unimplemented!();
9494
}
95-
if let Some(ident) = supertrait.path.get_ident() {
96-
match (&format!("{}", ident) as &str, &ident) {
95+
// First try to resolve path to find in-crate traits, but if that doesn't work
96+
// assume its a prelude trait (eg Clone, etc) and just use the single ident.
97+
if let Some(path) = $types.maybe_resolve_path(&supertrait.path, None) {
98+
match (&path as &str, &supertrait.path.segments.iter().last().unwrap().ident) {
9799
$( $pat => $e, )*
98100
}
99-
} else {
100-
let path = $types.resolve_path(&supertrait.path, None);
101-
match (&path as &str, &supertrait.path.segments.iter().last().unwrap().ident) {
101+
} else if let Some(ident) = supertrait.path.get_ident() {
102+
match (&format!("{}", ident) as &str, &ident) {
102103
$( $pat => $e, )*
103104
}
105+
} else {
106+
panic!("Supertrait unresolvable and not single-ident");
104107
}
105108
},
106109
syn::TypeParamBound::Lifetime(_) => unimplemented!(),
@@ -661,8 +664,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
661664
writeln!(w, "\t\tclone: Some({}_clone_void),", ident).unwrap();
662665
},
663666
(s, t) => {
664-
if s.starts_with("util::") {
665-
let supertrait_obj = types.crate_types.traits.get(s).unwrap();
667+
if let Some(supertrait_obj) = types.crate_types.traits.get(s) {
666668
writeln!(w, "\t\t{}: crate::{} {{", t, s).unwrap();
667669
writeln!(w, "\t\t\tthis_arg: unsafe {{ (*this_arg).inner as *mut c_void }},").unwrap();
668670
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
753755
}
754756
walk_supertraits!(trait_obj, types, (
755757
(s, t) => {
756-
if s.starts_with("util::") {
758+
if let Some(supertrait_obj) = types.crate_types.traits.get(s).cloned() {
757759
writeln!(w, "use {}::{} as native{}Trait;", types.orig_crate, s, t).unwrap();
758-
let supertrait_obj = *types.crate_types.traits.get(s).unwrap();
759760
for item in supertrait_obj.items.iter() {
760761
match item {
761762
syn::TraitItem::Method(m) => {

0 commit comments

Comments
 (0)