immunant / c2rust

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

analyze: fix bad rewrite `#[derive(&(Clone))]` #905

Closed spernsteiner closed 1 year ago

spernsteiner commented 1 year ago

This filecheck test currently fails:

use std::ptr;

// CHECK: #[derive(Copy, Clone)]
// CHECK-NEXT: struct Simple {
#[derive(Copy, Clone)]
struct Simple {
    x: i32,
    y: i32,
}

// CHECK: #[derive(Copy, Clone)]
// CHECK-NEXT: struct WithPtr {
#[derive(Copy, Clone)]
struct WithPtr {
    p: *mut i32,
    q: *mut i32,
}

unsafe fn f() {
    let mut s = Simple { x: 1, y: 2 };
    let wp = WithPtr { p: ptr::addr_of_mut!(s.x), q: ptr::addr_of_mut!(s.y) };
    *(wp.p) = *(wp.q) + 1;
}

c2rust-analyze mistakenly rewrites Clone to &(Clone) in the two structs' #[derive(...)] attributes. I suspect it's trying to rewrite x to &x inside the Clone impl, but is applying that rewrite in the wrong place due to the way derive(Clone) sets up the spans on the code it generates. If this is right, then we should suppress rewrites in derive-generated code (maybe by looking at the macro-expansion info in the expr/statement spans) to prevent this issue.

aneksteind commented 1 year ago

Resolved by #956