immunant / c2rust

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

analyze: preserve refs #923

Closed spernsteiner closed 1 year ago

spernsteiner commented 1 year ago

This branch updates mir_op to respect the new FIXED flag, thereby stopping most rewriting of already-safe code.

The main effect is that when emit_cast_* sees a FIXED type on one side of the cast, it tries to obtain a TypeDesc by deconstructing the existing Ty, rather than by looking at the perms and flags the analysis came up with. This ensure we keep the original Ty unchanged, even if the perms and flags would result in a different type. For example, given a FIXED pointer of type &[T], we would produce (Ownership::Ref, Quantity::Slice, T), which accurately reflects the type &[T], regardless of what the perms and flags say.

Currently this is only used to preserve existing refs, and in that case, the perms and flags should generally agree with the FIXED Ty. But in the future we'll extend this to raw pointers (such as FFI and unanalyzed functions), where perms and flags will normally suggest some kind of safe rewrite (e.g. *mut T -> &[T]), but we don't actually want to perform that rewrite.

Depends on #920; once #920 is merged, I'll rebase this onto master

spernsteiner commented 1 year ago

Rebased onto master

aneksteind commented 1 year ago

@spernsteiner i still approve, but would like to see responses to the comments