From: Matt Corallo Date: Fri, 17 Jun 2022 21:01:14 +0000 (+0000) Subject: Pass `with_ref_lifetime` through to rust-type-printing from C-path printing X-Git-Tag: v0.0.108.0^2~9 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=257e3956c377b9610bc01925ceecca450d8735cc;p=ldk-c-bindings Pass `with_ref_lifetime` through to rust-type-printing from C-path printing If we get to `write_c_path_intern` with `c_ty` unset, we delegate to `write_rust_path`, however it would lose the `with_ref_lifetime` flag, causing us to miss `'static` when printing references in generic arguments. Here we simply pipe it through. --- diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index b9dba8c..42fd470 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -163,7 +163,7 @@ fn maybe_convert_trait_impl(w: &mut W, trait_path: &syn::Path write!(w, "\tlet res: ").unwrap(); // At least in one case we need type annotations here, so provide them. - types.write_rust_type(w, Some(generics), &res_ty); + types.write_rust_type(w, Some(generics), &res_ty, false); if t == "lightning::util::ser::ReadableArgs" { writeln!(w, " = crate::c_types::deserialize_obj_arg(ser, arg_conv);").unwrap(); @@ -484,7 +484,7 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty } _ => panic!("5"), } - $type_resolver.write_rust_type(w, Some(&gen_types), &*arg.ty); + $type_resolver.write_rust_type(w, Some(&gen_types), &*arg.ty, false); } } } @@ -492,7 +492,7 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty match &m.sig.output { syn::ReturnType::Type(_, rtype) => { write!(w, " -> ").unwrap(); - $type_resolver.write_rust_type(w, Some(&gen_types), &*rtype) + $type_resolver.write_rust_type(w, Some(&gen_types), &*rtype, false) }, _ => {}, } diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index 8f5e667..5187477 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -1352,7 +1352,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { if let syn::Lit::Int(i) = &l.lit { if i.base10_digits().parse::().unwrap() >= 32 { let mut buf = Vec::new(); - self.write_rust_type(&mut buf, generics, &a.elem); + self.write_rust_type(&mut buf, generics, &a.elem, false); let ty = String::from_utf8(buf).unwrap(); ty == "u8" } else { @@ -1652,7 +1652,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { } } - fn write_rust_path(&self, w: &mut W, generics_resolver: Option<&GenericTypes>, path: &syn::Path) { + fn write_rust_path(&self, w: &mut W, generics_resolver: Option<&GenericTypes>, path: &syn::Path, with_ref_lifetime: bool) { if let Some(resolved) = self.maybe_resolve_path(&path, generics_resolver) { if self.is_primitive(&resolved) { write!(w, "{}", path.get_ident().unwrap()).unwrap(); @@ -1670,7 +1670,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { } } if let syn::PathArguments::AngleBracketed(args) = &path.segments.iter().last().unwrap().arguments { - self.write_rust_generic_arg(w, generics_resolver, args.args.iter()); + self.write_rust_generic_arg(w, generics_resolver, args.args.iter(), with_ref_lifetime); } } else { if path.leading_colon.is_some() { @@ -1680,7 +1680,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { if idx != 0 { write!(w, "::").unwrap(); } write!(w, "{}", seg.ident).unwrap(); if let syn::PathArguments::AngleBracketed(args) = &seg.arguments { - self.write_rust_generic_arg(w, generics_resolver, args.args.iter()); + self.write_rust_generic_arg(w, generics_resolver, args.args.iter(), with_ref_lifetime); } } } @@ -1700,7 +1700,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { match bound { syn::TypeParamBound::Trait(tb) => { if tb.paren_token.is_some() || tb.lifetimes.is_some() { unimplemented!(); } - self.write_rust_path(w, generics_resolver, &tb.path); + self.write_rust_path(w, generics_resolver, &tb.path, false); }, _ => unimplemented!(), } @@ -1713,38 +1713,40 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { if had_params { write!(w, ">").unwrap(); } } - pub fn write_rust_generic_arg<'b, W: std::io::Write>(&self, w: &mut W, generics_resolver: Option<&GenericTypes>, generics: impl Iterator) { + pub fn write_rust_generic_arg<'b, W: std::io::Write>(&self, w: &mut W, generics_resolver: Option<&GenericTypes>, generics: impl Iterator, with_ref_lifetime: bool) { write!(w, "<").unwrap(); for (idx, arg) in generics.enumerate() { if idx != 0 { write!(w, ", ").unwrap(); } match arg { - syn::GenericArgument::Type(t) => self.write_rust_type(w, generics_resolver, t), + syn::GenericArgument::Type(t) => self.write_rust_type(w, generics_resolver, t, with_ref_lifetime), _ => unimplemented!(), } } write!(w, ">").unwrap(); } - pub fn write_rust_type(&self, w: &mut W, generics: Option<&GenericTypes>, t: &syn::Type) { + pub fn write_rust_type(&self, w: &mut W, generics: Option<&GenericTypes>, t: &syn::Type, with_ref_lifetime: bool) { match generics.resolve_type(t) { syn::Type::Path(p) => { if p.qself.is_some() { unimplemented!(); } - self.write_rust_path(w, generics, &p.path); + self.write_rust_path(w, generics, &p.path, with_ref_lifetime); }, syn::Type::Reference(r) => { write!(w, "&").unwrap(); if let Some(lft) = &r.lifetime { write!(w, "'{} ", lft.ident).unwrap(); + } else if with_ref_lifetime { + write!(w, "'static ").unwrap(); } if r.mutability.is_some() { write!(w, "mut ").unwrap(); } - self.write_rust_type(w, generics, &*r.elem); + self.write_rust_type(w, generics, &*r.elem, with_ref_lifetime); }, syn::Type::Array(a) => { write!(w, "[").unwrap(); - self.write_rust_type(w, generics, &a.elem); + self.write_rust_type(w, generics, &a.elem, with_ref_lifetime); if let syn::Expr::Lit(l) = &a.len { if let syn::Lit::Int(i) = &l.lit { write!(w, "; {}]", i).unwrap(); @@ -1753,14 +1755,14 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { } syn::Type::Slice(s) => { write!(w, "[").unwrap(); - self.write_rust_type(w, generics, &s.elem); + self.write_rust_type(w, generics, &s.elem, with_ref_lifetime); write!(w, "]").unwrap(); }, syn::Type::Tuple(s) => { write!(w, "(").unwrap(); for (idx, t) in s.elems.iter().enumerate() { if idx != 0 { write!(w, ", ").unwrap(); } - self.write_rust_type(w, generics, &t); + self.write_rust_type(w, generics, &t, with_ref_lifetime); } write!(w, ")").unwrap(); }, @@ -2759,7 +2761,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { // lifetime, of which the only real available choice is `static`, obviously. write!(w, "&'static {}", crate_pfx).unwrap(); if !c_ty { - self.write_rust_path(w, generics, path); + self.write_rust_path(w, generics, path, with_ref_lifetime); } else { // We shouldn't be mapping references in types, so panic here unimplemented!();