diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index bf07d0b9c3..b45d5a46ff 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -596,6 +596,13 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) { } void Converter::ConvertVarDeclInitializer(clang::VarDecl *decl) { + // Rust-only argument conversions can need temporaries even when Clang + // supplies no ExprWithCleanups for the initializer. + std::string declarations; + ScopedValue scope(full_expression_temporaries_, + in_const_initializer_ ? &declarations + : full_expression_temporaries_); + auto start = rs_code_->size(); bool registers_destructor = NeedsStaticDestructor(decl); if (registers_destructor) { StrCat("{ let __static = "); @@ -612,6 +619,10 @@ void Converter::ConvertVarDeclInitializer(clang::VarDecl *decl) { "; libcc2rs::register_static_destructor(|| {}); __static }}", StaticDestructorCall(decl))); } + if (!declarations.empty()) { + rs_code_->insert(start, "{ " + declarations); + StrCat('}'); + } } void Converter::EmitHoistedInArmAssignment(clang::VarDecl *decl) { @@ -2225,8 +2236,14 @@ void Converter::EmitHoistedArgs(CallInfo &info) { StrCat(";"); break; case Kind::Materialized: { + auto *declarations = + in_const_initializer_ ? full_expression_temporaries_ : nullptr; + if (declarations) { + // Other calls in the initializer may bind the same parameter. + ca.param_name = NewTempName(); + } auto [binding, ref] = - MaterializeTemp(ca.param_name, ca.param_type, ca.expr); + MaterializeTemp(ca.param_name, ca.param_type, ca.expr, declarations); StrCat(binding); ca.ref_temp_name = std::move(ref); break; @@ -5510,7 +5527,11 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg, auto materialized = ph_ctx.materialize_ctx->GetOrMaterialize( static_cast(ph_ctx.materialize_idx), [this, arg](clang::QualType type) { - return MaterializeTemp(NewTempName(), type, arg); + // Call arguments live through the full expression, not as statics + // alongside the global whose initializer evaluates the call. + return MaterializeTemp( + NewTempName(), type, arg, + in_const_initializer_ ? full_expression_temporaries_ : nullptr); }); // The temporary is initialized in a statement, which ends its borrows. if (!materialized.empty()) { diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 882423717a..09409dacb4 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -2486,6 +2486,12 @@ bool ConverterRefCount::EmitGlobalValueAssign(clang::Expr *lhs, if (!var || !IsGlobalVar(var) || var->getType()->isReferenceType()) { return false; } + PushBrace brace(*this, in_const_initializer_); + if (in_const_initializer_) { + // Initialize full-expression temporaries outside the thread-local closure. + StrCat(keyword::kLet, "__rhs", token::kAssign, rhs, token::kSemiColon); + rhs = "__rhs"; + } StrCat(std::format("{}.with(|rc| *rc.borrow_mut() {} {})", GetNamedDeclAsString(var), assign_operator, rhs)); return true; diff --git a/tests/unit/global_call_temporaries.cpp b/tests/unit/global_call_temporaries.cpp new file mode 100644 index 0000000000..c23ce64f6f --- /dev/null +++ b/tests/unit/global_call_temporaries.cpp @@ -0,0 +1,48 @@ +#include +#include +#include + +int read(const int &value) { return value; } + +// The operands must survive until the returned reference has been consumed. +int selected = read(std::min(0, 0)); + +int x = 0, y = 0; +int assigned = (x = std::min(5, 6)); +int accumulated = (y += std::max(1, 2)); +int call_assigned = (x = read(5)); + +struct Item { + int value; +}; + +int read_array(const int (&value)[1]) { return value[0]; } +int read_record(const Item &value) { return value.value; } + +// Enclosing argument owners share the full expression, not the parameter name. +int aggregate_selected = + read_array({std::min(3, 4)}) + read_record({std::max(5, 6)}); + +// These C++ types agree, but their Rust mappings require a cast temporary. +unsigned long left = 3; +std::size_t right = 7; +std::size_t mapped_selected = std::min(left, right); + +std::size_t get() { + static std::size_t value = std::max(left, right); + return value; +} + +int main() { + assert(selected == 0); + assert(assigned == 5); + assert(accumulated == 2); + assert(call_assigned == 5); + assert(aggregate_selected == 9); + static int aggregate_static = + read_array({std::max(7, 8)}) + read_record({std::min(9, 10)}); + assert(aggregate_static == 17); + assert(mapped_selected == 3); + assert(get() == 7); + return 0; +} diff --git a/tests/unit/out/refcount/global_call_temporaries.rs b/tests/unit/out/refcount/global_call_temporaries.rs new file mode 100644 index 0000000000..6beec818a6 --- /dev/null +++ b/tests/unit/out/refcount/global_call_temporaries.rs @@ -0,0 +1,249 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn read_0(value: Ptr) -> i32 { + return (value.read()); +} +thread_local!( + pub static selected_1: Value = Rc::new(RefCell::new({ + let __tmp_0: Value; + let __tmp_1: Value; + ({ + read_0( + ({ + __tmp_0 = Rc::new(RefCell::new(0)); + __tmp_1 = Rc::new(RefCell::new(0)); + if CppLess::less(&__tmp_0.as_pointer(), &__tmp_1.as_pointer()) { + __tmp_0.as_pointer() + } else { + __tmp_1.as_pointer() + } + }), + ) + }) + })); +); +thread_local!( + pub static x_2: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static y_3: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static assigned_4: Value = Rc::new(RefCell::new({ + let __tmp_2: Value; + let __tmp_3: Value; + ({ + { + let __rhs = { + __tmp_2 = Rc::new(RefCell::new(6)); + __tmp_3 = Rc::new(RefCell::new(5)); + (if CppLess::less(&__tmp_2.as_pointer(), &__tmp_3.as_pointer()) { + __tmp_2.as_pointer() + } else { + __tmp_3.as_pointer() + } + .read()) + }; + x_2.with(|rc| *rc.borrow_mut() = __rhs) + }; + x_2.with(|rc| *rc.borrow()) + }) + })); +); +thread_local!( + pub static accumulated_5: Value = Rc::new(RefCell::new({ + let __tmp_4: Value; + let __tmp_5: Value; + ({ + { + let __rhs = { + __tmp_4 = Rc::new(RefCell::new(1)); + __tmp_5 = Rc::new(RefCell::new(2)); + (if CppLess::less(&__tmp_4.as_pointer(), &__tmp_5.as_pointer()) { + __tmp_5.as_pointer() + } else { + __tmp_4.as_pointer() + } + .read()) + }; + y_3.with(|rc| *rc.borrow_mut() += __rhs) + }; + y_3.with(|rc| *rc.borrow()) + }) + })); +); +thread_local!( + pub static call_assigned_6: Value = Rc::new(RefCell::new({ + let __tmp_6: Value; + ({ + { + let __rhs = ({ + __tmp_6 = Rc::new(RefCell::new(5)); + read_0(__tmp_6.as_pointer()) + }); + x_2.with(|rc| *rc.borrow_mut() = __rhs) + }; + x_2.with(|rc| *rc.borrow()) + }) + })); +); +#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(4)] +pub struct Item { + #[offset(0)] + pub value: i32, +} +pub fn read_array_7(value: Ptr) -> i32 { + return (elem!(value, 0).read()); +} +pub fn read_record_8(value: Ptr) -> i32 { + return value.with(|__s| __s.value); +} +thread_local!( + pub static aggregate_selected_9: Value = Rc::new(RefCell::new({ + let __tmp_8: Value; + let __tmp_9: Value; + let __tmp_7: Value>; + let __tmp_11: Value; + let __tmp_12: Value; + let __tmp_10: Value; + (({ + __tmp_7 = Rc::new(RefCell::new(Box::new([{ + __tmp_8 = Rc::new(RefCell::new(4)); + __tmp_9 = Rc::new(RefCell::new(3)); + (if CppLess::less(&__tmp_8.as_pointer(), &__tmp_9.as_pointer()) { + __tmp_8.as_pointer() + } else { + __tmp_9.as_pointer() + } + .read()) + }]))); + read_array_7(__tmp_7.decay()) + }) + ({ + __tmp_10 = Rc::new(RefCell::new(Item { + value: { + __tmp_11 = Rc::new(RefCell::new(5)); + __tmp_12 = Rc::new(RefCell::new(6)); + (if CppLess::less(&__tmp_11.as_pointer(), &__tmp_12.as_pointer()) { + __tmp_12.as_pointer() + } else { + __tmp_11.as_pointer() + } + .read()) + }, + })); + read_record_8(__tmp_10.as_pointer()) + })) + })); +); +thread_local!( + pub static left_10: Value = Rc::new(RefCell::new(3_u64)); +); +thread_local!( + pub static right_11: Value = Rc::new(RefCell::new(7_usize)); +); +thread_local!( + pub static mapped_selected_12: Value = { + let __tmp_13: Value; + Rc::new(RefCell::new( + (({ + __tmp_13 = Rc::new(RefCell::new((right_11.with(|rc| *rc.borrow()) as u64))); + (if CppLess::less(&__tmp_13.as_pointer(), &left_10.with(|v| v.as_pointer())) { + __tmp_13.as_pointer() + } else { + left_10.with(|v| v.as_pointer()) + } + .read()) + }) as usize), + )) + }; +); +pub fn get_13() -> usize { + return value_14.with(|rc| *rc.borrow()); +} +thread_local!( + static value_14: Value = { + let __tmp_14: Value; + Rc::new(RefCell::new( + (({ + __tmp_14 = Rc::new(RefCell::new((right_11.with(|rc| *rc.borrow()) as u64))); + (if CppLess::less(&left_10.with(|v| v.as_pointer()), &__tmp_14.as_pointer()) { + __tmp_14.as_pointer() + } else { + left_10.with(|v| v.as_pointer()) + } + .read()) + }) as usize), + )) + }; +); +pub fn main() { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0()); +} +fn main_0() -> i32 { + assert!((selected_1.with(|rc| *rc.borrow()) == 0)); + assert!((assigned_4.with(|rc| *rc.borrow()) == 5)); + assert!((accumulated_5.with(|rc| *rc.borrow()) == 2)); + assert!((call_assigned_6.with(|rc| *rc.borrow()) == 5)); + assert!((aggregate_selected_9.with(|rc| *rc.borrow()) == 9)); + assert!((aggregate_static_15.with(|rc| *rc.borrow()) == 17)); + assert!((mapped_selected_12.with(|rc| *rc.borrow()) == 3_usize)); + assert!((({ get_13() }) == 7_usize)); + return 0; +} +thread_local!( + static aggregate_static_15: Value = Rc::new(RefCell::new({ + let __tmp_16: Value; + let __tmp_17: Value; + let __tmp_15: Value>; + let __tmp_19: Value; + let __tmp_20: Value; + let __tmp_18: Value; + (({ + __tmp_15 = Rc::new(RefCell::new(Box::new([{ + __tmp_16 = Rc::new(RefCell::new(7)); + __tmp_17 = Rc::new(RefCell::new(8)); + (if CppLess::less(&__tmp_16.as_pointer(), &__tmp_17.as_pointer()) { + __tmp_17.as_pointer() + } else { + __tmp_16.as_pointer() + } + .read()) + }]))); + read_array_7(__tmp_15.decay()) + }) + ({ + __tmp_18 = Rc::new(RefCell::new(Item { + value: { + __tmp_19 = Rc::new(RefCell::new(10)); + __tmp_20 = Rc::new(RefCell::new(9)); + (if CppLess::less(&__tmp_19.as_pointer(), &__tmp_20.as_pointer()) { + __tmp_19.as_pointer() + } else { + __tmp_20.as_pointer() + } + .read()) + }, + })); + read_record_8(__tmp_18.as_pointer()) + })) + })); +); +pub fn __cpp2rust_init_globals() { + let _ = selected_1.with(|_| ()); + let _ = x_2.with(|_| ()); + let _ = y_3.with(|_| ()); + let _ = assigned_4.with(|_| ()); + let _ = accumulated_5.with(|_| ()); + let _ = call_assigned_6.with(|_| ()); + let _ = aggregate_selected_9.with(|_| ()); + let _ = left_10.with(|_| ()); + let _ = right_11.with(|_| ()); + let _ = mapped_selected_12.with(|_| ()); +} diff --git a/tests/unit/out/unsafe/global_call_temporaries.rs b/tests/unit/out/unsafe/global_call_temporaries.rs new file mode 100644 index 0000000000..b466e55114 --- /dev/null +++ b/tests/unit/out/unsafe/global_call_temporaries.rs @@ -0,0 +1,223 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub unsafe fn read_0(value: *const i32) -> i32 { + return (*value); +} +pub static mut selected_1: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { + { + let mut __tmp_0: i32; + let mut __tmp_1: i32; + (unsafe { + read_0({ + __tmp_0 = 0; + __tmp_1 = 0; + { + let __a: *const _ = &mut __tmp_0; + let __b: *const _ = &mut __tmp_1; + if *__b < *__a { __b } else { __a } + } + }) + }) + } +}); +pub static mut x_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut y_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut assigned_4: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { + { + let mut __tmp_2: i32; + let mut __tmp_3: i32; + ({ + (*std::cell::LazyCell::force_mut(&mut *&raw mut x_2)) = { + __tmp_2 = 5; + __tmp_3 = 6; + (*{ + let __a: *const _ = &mut __tmp_2; + let __b: *const _ = &mut __tmp_3; + if *__b < *__a { __b } else { __a } + }) + }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut x_2)) + }) + } +}); +pub static mut accumulated_5: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { + { + let mut __tmp_4: i32; + let mut __tmp_5: i32; + ({ + (*std::cell::LazyCell::force_mut(&mut *&raw mut y_3)) += { + __tmp_4 = 1; + __tmp_5 = 2; + (*{ + let __a: *const _ = &mut __tmp_4; + let __b: *const _ = &mut __tmp_5; + if *__a < *__b { __b } else { __a } + }) + }; + (*std::cell::LazyCell::force_mut(&mut *&raw mut y_3)) + }) + } +}); +pub static mut call_assigned_6: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { + { + let mut __tmp_6: i32; + ({ + (*std::cell::LazyCell::force_mut(&mut *&raw mut x_2)) = (unsafe { + __tmp_6 = 5; + read_0(&mut __tmp_6) + }); + (*std::cell::LazyCell::force_mut(&mut *&raw mut x_2)) + }) + } +}); +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct Item { + pub value: i32, +} +pub unsafe fn read_array_7(value: *const [i32; 1]) -> i32 { + return (*value)[(0) as usize]; +} +pub unsafe fn read_record_8(value: *const Item) -> i32 { + return (*value).value; +} +pub static mut aggregate_selected_9: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { + { + let mut __tmp_8: i32; + let mut __tmp_9: i32; + let mut __tmp_7: [i32; 1]; + let mut __tmp_11: i32; + let mut __tmp_12: i32; + let mut __tmp_10: Item; + ((unsafe { + __tmp_7 = [{ + __tmp_8 = 3; + __tmp_9 = 4; + (*{ + let __a: *const _ = &mut __tmp_8; + let __b: *const _ = &mut __tmp_9; + if *__b < *__a { __b } else { __a } + }) + }]; + read_array_7(&mut __tmp_7) + }) + (unsafe { + __tmp_10 = Item { + value: { + __tmp_11 = 5; + __tmp_12 = 6; + (*{ + let __a: *const _ = &mut __tmp_11; + let __b: *const _ = &mut __tmp_12; + if *__a < *__b { __b } else { __a } + }) + }, + }; + read_record_8(&mut __tmp_10) + })) + } + }); +pub static mut left_10: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 3_u64 }); +pub static mut right_11: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { 7_usize }); +pub static mut mapped_selected_12: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { + { + let mut __tmp_13: u64; + (({ + __tmp_13 = ((*std::cell::LazyCell::force_mut(&mut *&raw mut right_11)) as u64); + (*{ + let __a: *const _ = + &mut (*std::cell::LazyCell::force_mut(&mut *&raw mut left_10)); + let __b: *const _ = &mut __tmp_13; + if *__b < *__a { __b } else { __a } + }) + }) as usize) + } + }); +pub unsafe fn get_13() -> usize { + return (*std::cell::LazyCell::force_mut(&mut *&raw mut value_14)); +} +static mut value_14: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { + { + let mut __tmp_14: u64; + (({ + __tmp_14 = ((*std::cell::LazyCell::force_mut(&mut *&raw mut right_11)) as u64); + (*{ + let __a: *const _ = &mut (*std::cell::LazyCell::force_mut(&mut *&raw mut left_10)); + let __b: *const _ = &mut __tmp_14; + if *__a < *__b { __b } else { __a } + }) + }) as usize) + } +}); +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut selected_1)) == (0))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut assigned_4)) == (5))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut accumulated_5)) == (2))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut call_assigned_6)) == (5))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut aggregate_selected_9)) == (9))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut aggregate_static_15)) == (17))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut mapped_selected_12)) == (3_usize))); + assert!(((unsafe { get_13() }) == (7_usize))); + return 0; +} +static mut aggregate_static_15: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { + { + let mut __tmp_16: i32; + let mut __tmp_17: i32; + let mut __tmp_15: [i32; 1]; + let mut __tmp_19: i32; + let mut __tmp_20: i32; + let mut __tmp_18: Item; + ((unsafe { + __tmp_15 = [{ + __tmp_16 = 7; + __tmp_17 = 8; + (*{ + let __a: *const _ = &mut __tmp_16; + let __b: *const _ = &mut __tmp_17; + if *__a < *__b { __b } else { __a } + }) + }]; + read_array_7(&mut __tmp_15) + }) + (unsafe { + __tmp_18 = Item { + value: { + __tmp_19 = 9; + __tmp_20 = 10; + (*{ + let __a: *const _ = &mut __tmp_19; + let __b: *const _ = &mut __tmp_20; + if *__b < *__a { __b } else { __a } + }) + }, + }; + read_record_8(&mut __tmp_18) + })) + } +}); +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const selected_1); + std::cell::LazyCell::force(&*&raw const x_2); + std::cell::LazyCell::force(&*&raw const y_3); + std::cell::LazyCell::force(&*&raw const assigned_4); + std::cell::LazyCell::force(&*&raw const accumulated_5); + std::cell::LazyCell::force(&*&raw const call_assigned_6); + std::cell::LazyCell::force(&*&raw const aggregate_selected_9); + std::cell::LazyCell::force(&*&raw const left_10); + std::cell::LazyCell::force(&*&raw const right_11); + std::cell::LazyCell::force(&*&raw const mapped_selected_12); +}