immunant / c2rust

Migrate C code to Rust
https://c2rust.com/
Other
3.91k stars 229 forks source link

Feat.rewrite.lifetime #915

Closed aneksteind closed 1 year ago

aneksteind commented 1 year ago

Rewrites structs to reflect hypothetical lifetimes accepted by polonius.

e.g.

pub struct Data<'d> {
    pub pi: *mut i32,
    pub pa: *mut A<'d>,
    pub a: A<'d>,
}
pub struct A<'a> {
    pub rd: &'a Data<'a>,
    pub pra: *mut &'a mut A<'a>,
}

struct VecTup<'a> {
    bar: *mut Vec<(VecTup<'a>, *mut A<'a>)>,
}

gets rewritten to

pub struct Data<'d,'h0,'h1,'h2> {
    pub pi: &'h0 mut (i32),
    pub pa: &'h1 mut (A<'d,'h0,'h1,'h2>),
    pub a: A<'d,'h0,'h1,'h2>,
}

pub struct A<'a,'h0,'h1,'h2> {
    pub rd: &'a (Data<'a,'h0,'h1,'h2>),
    pub pra: &'h2 core::cell::Cell<(&'a (A<'a,'h0,'h1,'h2>))>,
}

struct VecTup<'a,'h3,'h4,'h0,'h1,'h2> {
    bar: &'h3 (Vec<(VecTup<'a,'h3,'h4,'h0,'h1,'h2>, &'h4 (A<'a,'h0,'h1,'h2>))>),
}
aneksteind commented 1 year ago

@spernsteiner would you mind taking another look at this?

spernsteiner commented 1 year ago

I think ideally we would be able to either insert additional lifetime arguments before/after some existing argument (<T> -> <'h0, T> or <'a> -> <'a, 'h0>, with the rewrite being placed on a zero-width span <{}T> or <'a{}>) or generate a whole generics list from scratch if none is present (Foo -> Foo<'h0>, again attaching the rewrite to a zero-width span Foo{}). This would minimize disruption of the existing source code. But it might be better to just add a TODO or open an issue to avoid this complexity for now.