immunant / c2rust

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

analyze: missing hypothetical lifetimes in type alias RHS #991

Open spernsteiner opened 1 year ago

spernsteiner commented 1 year ago
struct NeedsLifetime {
    p: *mut u8,
}

type Alias = NeedsLifetime;

After rewriting:

struct NeedsLifetime<'h1> {
    p: &'h1 (u8),
}

type Alias = NeedsLifetime;

This output is invalid because the RHS of type Alias uses NeedsLifetime without passing a lifetime argument.

I'm not sure what's the best fix for this. One idea is to add hypothetical params to Alias corresponding to the hypothetical params of all the structs that appear in the RHS. But anywhere types are rewritten, Alias will get unfolded by rewrite::ty due to the mismatch between the HIR Ty and the ty::Ty (the HIR resolves to Alias, but the ty::Ty refers to NeedsLifetime directly), so the only remaining uses of Alias should be in non-rewritten functions. So maybe we should just fill in 'static for all the hypothetical arguments on the RHS, since the use in non-rewritten code will be unsafe regardless.

aneksteind commented 1 year ago

Related and not quite a duplicate: #926