Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ");
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -5510,7 +5527,11 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg,
auto materialized = ph_ctx.materialize_ctx->GetOrMaterialize(
static_cast<unsigned>(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()) {
Expand Down
6 changes: 6 additions & 0 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/global_call_temporaries.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <algorithm>
#include <cassert>
#include <cstddef>

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<std::size_t>(left, right);

std::size_t get() {
static std::size_t value = std::max<std::size_t>(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;
}
249 changes: 249 additions & 0 deletions tests/unit/out/refcount/global_call_temporaries.rs
Original file line number Diff line number Diff line change
@@ -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>) -> i32 {
return (value.read());
}
thread_local!(
pub static selected_1: Value<i32> = Rc::new(RefCell::new({
let __tmp_0: Value<i32>;
let __tmp_1: Value<i32>;
({
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<i32> = Rc::new(RefCell::new(0));
);
thread_local!(
pub static y_3: Value<i32> = Rc::new(RefCell::new(0));
);
thread_local!(
pub static assigned_4: Value<i32> = Rc::new(RefCell::new({
let __tmp_2: Value<i32>;
let __tmp_3: Value<i32>;
({
{
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<i32> = Rc::new(RefCell::new({
let __tmp_4: Value<i32>;
let __tmp_5: Value<i32>;
({
{
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<i32> = Rc::new(RefCell::new({
let __tmp_6: Value<i32>;
({
{
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>) -> i32 {
return (elem!(value, 0).read());
}
pub fn read_record_8(value: Ptr<Item>) -> i32 {
return value.with(|__s| __s.value);
}
thread_local!(
pub static aggregate_selected_9: Value<i32> = Rc::new(RefCell::new({
let __tmp_8: Value<i32>;
let __tmp_9: Value<i32>;
let __tmp_7: Value<Box<[i32]>>;
let __tmp_11: Value<i32>;
let __tmp_12: Value<i32>;
let __tmp_10: Value<Item>;
(({
__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<u64> = Rc::new(RefCell::new(3_u64));
);
thread_local!(
pub static right_11: Value<usize> = Rc::new(RefCell::new(7_usize));
);
thread_local!(
pub static mapped_selected_12: Value<usize> = {
let __tmp_13: Value<u64>;
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<usize> = {
let __tmp_14: Value<u64>;
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<i32> = Rc::new(RefCell::new({
let __tmp_16: Value<i32>;
let __tmp_17: Value<i32>;
let __tmp_15: Value<Box<[i32]>>;
let __tmp_19: Value<i32>;
let __tmp_20: Value<i32>;
let __tmp_18: Value<Item>;
(({
__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(|_| ());
}
Loading
Loading