ocaml-rs icon indicating copy to clipboard operation
ocaml-rs copied to clipboard

missing lifetime specifier in pointer

Open crackcomm opened this issue 2 years ago • 2 comments

I stumbled onto a bizarre error raised by the borrow checker if at least one of the function arguments is a reference and return type is ocaml::Pointer.

Example code that raised the error:

struct Keypair(schnorrkel::Keypair);
ocaml::custom!(Keypair);
struct Signature(schnorrkel::Signature);
ocaml::custom!(Signature);

#[ocaml::func]
pub unsafe fn keypair_sign(
    keypair: ocaml::Pointer<Keypair>,
    ctx: &[u8],
    bytes: &[u8],
) -> ocaml::Pointer<Signature> {
    let ctx = schnorrkel::signing_context(ctx);
    let signature = keypair.as_ref().0.sign(ctx.bytes(bytes));
    ocaml::Pointer::alloc_custom(Signature(signature))
}

It's not possible to make keypair_sign<'a> (a hack anyways) since OCaml functions may not contain generics but it is possible to hack using in_band_lifetimes feature:

#![feature(in_band_lifetimes)]

struct Keypair(schnorrkel::Keypair);
ocaml::custom!(Keypair);
struct Signature(schnorrkel::Signature);
ocaml::custom!(Signature);

#[ocaml::func]
pub unsafe fn keypair_sign(
    keypair: ocaml::Pointer<Keypair>,
    ctx: &'a [u8],
    bytes: &[u8],
) -> ocaml::Pointer<'a, Signature> {
    let ctx = schnorrkel::signing_context(ctx);
    let signature = keypair.as_ref().0.sign(ctx.bytes(bytes));
    ocaml::Pointer::alloc_custom(Signature(signature))
}

This error is not raised when arguments are Vec<u8> etc.

crackcomm avatar Nov 10 '21 09:11 crackcomm

Hm, interesting. I don't have an immediate fix for this but will try to come up with something. Thanks for reporting!

zshipko avatar Nov 10 '21 22:11 zshipko

~~feature(in_band_lifetimes) was removed from nightly when rust-lang/rust#93845 landed; this issue can probably be closed~~ ah, but the issue with lifetimes still isn't resolved 🤦

cdmistman avatar Dec 06 '22 02:12 cdmistman