diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 483c7fdb..950f7579 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -1315,6 +1315,7 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) { return false; } if (expr->getCastKind() == clang::CK_NullToPointer) { + PushConversionKind push(*this, ConversionKind::Unboxed); StrCat(GetDefaultAsString(expr->getType())); computed_expr_type_ = ComputedExprType::FreshPointer; return false; diff --git a/tests/unit/out/refcount/ptr_cast_init.rs b/tests/unit/out/refcount/ptr_cast_init.rs index c851fd84..7e0042e4 100644 --- a/tests/unit/out/refcount/ptr_cast_init.rs +++ b/tests/unit/out/refcount/ptr_cast_init.rs @@ -74,5 +74,30 @@ fn main_0() -> i32 { let hp: Value> = Rc::new(RefCell::new((h.as_pointer()))); let v: Value> = Rc::new(RefCell::new((*hp.borrow()).reinterpret_cast::())); assert!(((((*(*(*v.borrow()).upgrade().deref()).tag.borrow()) == 7) as i32) != 0)); + let data: Value> = Rc::new(RefCell::new(Box::from(*b"hi\0"))); + let vp: Value = Rc::new(RefCell::new( + ((data.as_pointer() as Ptr) as Ptr).to_any(), + )); + let n: Value = Rc::new(RefCell::new(2)); + let sel: Value> = Rc::new(RefCell::new( + if ((((*n.borrow()) < 100) as i32) != 0) { + (*vp.borrow()).clone() + } else { + (AnyPtr::default()) + } + .reinterpret_cast::(), + )); + assert!((((!((*sel.borrow()).is_null())) as i32) != 0)); + assert!( + ((((((*sel.borrow()).offset((0) as isize).read()) as i32) == ('h' as i32)) as i32) != 0) + ); + (*n.borrow_mut()) = 200; + (*sel.borrow_mut()) = if ((((*n.borrow()) < 100) as i32) != 0) { + (*vp.borrow()).clone() + } else { + (AnyPtr::default()) + } + .reinterpret_cast::(); + assert!(((((*sel.borrow()).is_null()) as i32) != 0)); return 0; } diff --git a/tests/unit/out/unsafe/ptr_cast_init.rs b/tests/unit/out/unsafe/ptr_cast_init.rs index f61bd869..6afc27f8 100644 --- a/tests/unit/out/unsafe/ptr_cast_init.rs +++ b/tests/unit/out/unsafe/ptr_cast_init.rs @@ -32,5 +32,23 @@ unsafe fn main_0() -> i32 { let mut hp: *mut header = (&mut h as *mut header); let mut v: *mut view = (hp as *mut view); assert!((((((*v).tag) == (7)) as i32) != 0)); + let mut data: [libc::c_char; 3] = std::mem::transmute(*b"hi\0"); + let mut vp: *mut ::libc::c_void = + (data.as_mut_ptr() as *mut libc::c_char as *mut ::libc::c_void); + let mut n: i32 = 2; + let mut sel: *mut libc::c_char = (if ((((n) < (100)) as i32) != 0) { + vp + } else { + (0 as *mut ::libc::c_void) + } as *mut libc::c_char); + assert!((((!((sel).is_null())) as i32) != 0)); + assert!((((((*sel.offset((0) as isize)) as i32) == ('h' as i32)) as i32) != 0)); + n = 200; + sel = (if ((((n) < (100)) as i32) != 0) { + vp + } else { + (0 as *mut ::libc::c_void) + } as *mut libc::c_char); + assert!(((((sel).is_null()) as i32) != 0)); return 0; } diff --git a/tests/unit/ptr_cast_init.c b/tests/unit/ptr_cast_init.c index 78543c61..c788175a 100644 --- a/tests/unit/ptr_cast_init.c +++ b/tests/unit/ptr_cast_init.c @@ -1,4 +1,5 @@ #include +#include struct header { int tag; @@ -20,5 +21,15 @@ int main(void) { struct header *hp = &h; struct view *v = (struct view *)hp; assert(v->tag == 7); + + char data[] = "hi"; + void *vp = data; + int n = 2; + char *sel = (n < 100) ? vp : NULL; + assert(sel != 0); + assert(sel[0] == 'h'); + n = 200; + sel = (n < 100) ? vp : NULL; + assert(sel == 0); return 0; }