]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Pass `with_ref_lifetime` through to rust-type-printing from C-path printing
authorMatt Corallo <git@bluematt.me>
Fri, 17 Jun 2022 21:01:14 +0000 (21:01 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 28 Jun 2022 17:16:28 +0000 (17:16 +0000)
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.

c-bindings-gen/src/main.rs
c-bindings-gen/src/types.rs

index b9dba8c92e78188d3bb4abf4a80779fcb9ec2732..42fd470da778207a5dcd039c56dc869e670b3c1d 100644 (file)
@@ -163,7 +163,7 @@ fn maybe_convert_trait_impl<W: std::io::Write>(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)
                                                        },
                                                        _ => {},
                                                }
index 8f5e66735d5df40784fef02306ed95d2ec27cb14..5187477d7b6824cb353fc0ca8235349c889631e8 100644 (file)
@@ -1352,7 +1352,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                                if let syn::Lit::Int(i) = &l.lit {
                                                        if i.base10_digits().parse::<usize>().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<W: std::io::Write>(&self, w: &mut W, generics_resolver: Option<&GenericTypes>, path: &syn::Path) {
+       fn write_rust_path<W: std::io::Write>(&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<Item=&'b syn::GenericArgument>) {
+       pub fn write_rust_generic_arg<'b, W: std::io::Write>(&self, w: &mut W, generics_resolver: Option<&GenericTypes>, generics: impl Iterator<Item=&'b syn::GenericArgument>, 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<W: std::io::Write>(&self, w: &mut W, generics: Option<&GenericTypes>, t: &syn::Type) {
+       pub fn write_rust_type<W: std::io::Write>(&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!();