From 8b185c03daf002c2243b1966d495ba39acc15587 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 25 Apr 2024 20:08:27 +0000 Subject: [PATCH] Write out real generics when calling trait methods This avoids issues where a trait has a generic which can't be resolved. --- c-bindings-gen/src/blocks.rs | 12 ++++++++---- c-bindings-gen/src/main.rs | 20 +++++--------------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/c-bindings-gen/src/blocks.rs b/c-bindings-gen/src/blocks.rs index d01da1c..dc160d2 100644 --- a/c-bindings-gen/src/blocks.rs +++ b/c-bindings-gen/src/blocks.rs @@ -780,14 +780,18 @@ pub fn write_method_call_params(w: &mut W, sig: &syn::Signatu /// Prints concrete generic parameters for a struct/trait/function, including the less-than and /// greater-than symbols, if any generic parameters are defined. pub fn maybe_write_generics(w: &mut W, generics: &syn::Generics, generics_impld: &syn::PathArguments, types: &TypeResolver, concrete_lifetimes: bool) { - maybe_write_generics_intern(w, generics, generics_impld, types, concrete_lifetimes, false); + maybe_write_generics_intern(w, generics, Some(generics_impld), types, concrete_lifetimes, false); } pub fn maybe_write_non_lifetime_generics(w: &mut W, generics: &syn::Generics, generics_impld: &syn::PathArguments, types: &TypeResolver) { - maybe_write_generics_intern(w, generics, generics_impld, types, false, true); + maybe_write_generics_intern(w, generics, Some(generics_impld), types, false, true); } -fn maybe_write_generics_intern(w: &mut W, generics: &syn::Generics, generics_impld: &syn::PathArguments, types: &TypeResolver, concrete_lifetimes: bool, dummy_lifetimes: bool) { +pub fn maybe_write_type_non_lifetime_generics(w: &mut W, generics: &syn::Generics, types: &TypeResolver) { + maybe_write_generics_intern(w, generics, None, types, false, true); +} + +fn maybe_write_generics_intern(w: &mut W, generics: &syn::Generics, generics_impld: Option<&syn::PathArguments>, types: &TypeResolver, concrete_lifetimes: bool, dummy_lifetimes: bool) { let mut gen_types = GenericTypes::new(None); assert!(gen_types.learn_generics(generics, types)); if generics.params.is_empty() { return; } @@ -821,7 +825,7 @@ fn maybe_write_generics_intern(w: &mut W, generics: &syn::Gen if types.understood_c_type(&syn::parse_quote!(#type_ident), Some(&gen_types)) { types.write_c_type_in_generic_param(&mut out, &syn::parse_quote!(#type_ident), Some(&gen_types), false); } else { - if let syn::PathArguments::AngleBracketed(args) = generics_impld { + if let Some(syn::PathArguments::AngleBracketed(args)) = generics_impld { if let syn::GenericArgument::Type(ty) = &args.args[idx] { types.write_c_type_in_generic_param(&mut out, &ty, Some(&gen_types), false); } diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index 358e30b..fd4ea8f 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -1201,23 +1201,13 @@ fn writeln_impl(w: &mut W, w_uses: &mut HashSet if the _ is actually a lifetime, so - // if all the parameters are lifetimes just skip it. - let mut nonlifetime_param = false; - for param in $trait.generics.params.iter() { - if let syn::GenericParam::Lifetime(_) = param {} - else { nonlifetime_param = true; } - } - if !nonlifetime_param { t_gen_args = String::new(); } + let mut t_gen_args_vec = Vec::new(); + maybe_write_type_non_lifetime_generics(&mut t_gen_args_vec, &$trait.generics, &trait_resolver); + let t_gen_args = String::from_utf8(t_gen_args_vec).unwrap(); if takes_self { - write!(w, ">::{}(unsafe {{ &mut *(this_arg as *mut native{}) }}, ", ident, $trait_path, t_gen_args, $m.sig.ident, ident).unwrap(); + write!(w, "::{}(unsafe {{ &mut *(this_arg as *mut native{}) }}, ", ident, $trait_path, t_gen_args, $m.sig.ident, ident).unwrap(); } else { - write!(w, ">::{}(", ident, $trait_path, t_gen_args, $m.sig.ident).unwrap(); + write!(w, "::{}(", ident, $trait_path, t_gen_args, $m.sig.ident).unwrap(); } let mut real_type = "".to_string(); -- 2.39.5