Skip to content

Commit

Permalink
analyze: check mut-to-imm Option downgrade in non_null_rewrites test
Browse files Browse the repository at this point in the history
  • Loading branch information
spernsteiner committed May 15, 2024
1 parent a6cc054 commit 7b13770
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion c2rust-analyze/tests/filecheck/non_null_rewrites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ unsafe fn call_use_mut(cond: bool) -> i32 {

// CHECK-LABEL: unsafe fn use_mut{{[<(]}}
// CHECK-SAME: p: core::option::Option<&{{('[^ ]* )?}}mut (i32)>
unsafe fn use_mut(p: *mut i32) -> i32 {
unsafe fn use_mut(mut p: *mut i32) -> i32 {
if !p.is_null() {
// CHECK: *(p).as_deref_mut().unwrap() = 1;
*p = 1;
Expand Down Expand Up @@ -89,3 +89,20 @@ unsafe fn use_slice(p: *const i32) -> i32 {
unsafe fn use_single(p: *const i32) -> i32 {
*p
}


// CHECK-LABEL: unsafe fn downgrade_mut_to_imm_on_deref{{[<(]}}
// CHECK-SAME: p: core::option::Option<&{{('[^ ]* )?}}mut (i32)>
unsafe fn downgrade_mut_to_imm_on_deref(cond: bool, mut p: *mut i32) -> i32 {
if cond {
// Ensure `p` is wrapped in `Option`.
p = ptr::null_mut();
}
// Ensure `p` is mutable.
*p = 1;
// This read needs a downgrade via `as_deref()` to avoid moving `p: Option<&mut _>`.
// CHECK: let x = *(p).as_deref().unwrap();
let x = *p;
*p = 2;
x
}

0 comments on commit 7b13770

Please sign in to comment.