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
5 changes: 5 additions & 0 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3464,6 +3464,11 @@ bool Converter::VisitInitListExpr(clang::InitListExpr *expr) {
if (auto form = expr->getSemanticForm())
expr = form;

if (expr->isTransparent()) {
Convert(expr->getInit(0));
return false;
}

auto qual_type = expr->getType();
if (qual_type->isScalarType()) {
assert(expr->getNumInits() < 2 && "Excess elements in scalar initializer");
Expand Down
5 changes: 5 additions & 0 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1840,6 +1840,11 @@ bool ConverterRefCount::VisitInitListExpr(clang::InitListExpr *expr) {
if (auto form = expr->getSemanticForm())
expr = form;

if (expr->isTransparent()) {
Convert(expr->getInit(0));
return false;
}

auto qual_type = expr->getType();
if (qual_type->isScalarType()) {
PushConversionKind push(*this, ConversionKind::Unboxed);
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/init_list.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
#include <array>
#include <cassert>
#include <initializer_list>
#include <vector>

void f(std::initializer_list<int> list) {}

struct Pair {
int a;
int b;
};

struct Holder {
Pair p{0, 0};
Holder &operator=(Pair &&o) {
p = o;
return *this;
}
};

int sum(const Pair &p) { return p.a + p.b; }

int main() {
int i1{3};
int i2{};
Expand All @@ -13,5 +29,11 @@ int main() {
std::vector<int> vec = {1, 2, 3};
f({1, 2, 3, 4});

Pair p{1, 2};
Holder h;
h = {std::move(p)};
assert(h.p.a == 1 && h.p.b == 2);
assert(sum({p}) == 3);

return 0;
}
39 changes: 39 additions & 0 deletions tests/unit/out/refcount/init_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@ use std::rc::{Rc, Weak};
pub fn f_0(list: Vec<i32>) {
let list: Value<Vec<i32>> = Rc::new(RefCell::new(list));
}
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
#[byte_size(8)]
pub struct Pair {
#[offset(0)]
pub a: i32,
#[offset(4)]
pub b: i32,
}
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)]
#[byte_size(8)]
pub struct Holder {
#[offset(0)]
#[byte_size(8)]
pub p: Pair,
}
impl Default for Holder {
fn default() -> Self {
Holder {
p: Pair { a: 0, b: 0 },
}
}
}
pub fn sum_1(p: Ptr<Pair>) -> i32 {
return ({ p.with(|__s| __s.a) } + { p.with(|__s| __s.b) });
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand All @@ -21,6 +46,20 @@ fn main_0() -> i32 {
let arr: Value<Vec<i32>> = Rc::new(RefCell::new(vec![1, 2, 3]));
let mut vec_: Vec<i32> = vec![1, 2, 3];
({ f_0(vec![1, 2, 3, 4]) });
let p: Value<Pair> = Rc::new(RefCell::new(Pair { a: 1, b: 2 }));
let h: Value<Holder> = Rc::new(RefCell::new(<Holder>::default()));
({ HolderImpl::operator_assign_1(&h.as_pointer(), p.as_pointer()) });
assert!(({ (*h.borrow()).p.a } == 1) && ({ (*h.borrow()).p.b } == 2));
assert!((({ sum_1(p.as_pointer(),) }) == 3));
return 0;
}
pub trait HolderImpl {
fn operator_assign_1(&self, o: Ptr<Pair>) -> Ptr<Holder>;
}
impl HolderImpl for Ptr<Holder> {
fn operator_assign_1(&self, o: Ptr<Pair>) -> Ptr<Holder> {
field!((*self), p).write({ (*o.upgrade().deref()).clone() });
return (*self).clone();
}
}
pub fn __cpp2rust_init_globals() {}
32 changes: 32 additions & 0 deletions tests/unit/out/unsafe/init_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,33 @@ use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
pub unsafe fn f_0(mut list: Vec<i32>) {}
#[repr(C)]
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
pub struct Pair {
pub a: i32,
pub b: i32,
}
#[repr(C)]
#[derive(Copy, Clone, VaArg, FnPtrArg)]
pub struct Holder {
pub p: Pair,
}
impl Holder {
pub unsafe fn operator_assign_1(&mut self, o: *mut Pair) -> *mut Holder {
self.p = (*o);
return &mut (*(self as *mut Holder));
}
}
impl Default for Holder {
fn default() -> Self {
Holder {
p: Pair { a: 0, b: 0 },
}
}
}
pub unsafe fn sum_1(p: *const Pair) -> i32 {
return (((*p).a) + ((*p).b));
}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
Expand All @@ -21,6 +48,11 @@ unsafe fn main_0() -> i32 {
let mut arr: Vec<i32> = vec![1, 2, 3];
let mut vec_: Vec<i32> = vec![1, 2, 3];
(unsafe { f_0(vec![1, 2, 3, 4]) });
let mut p: Pair = Pair { a: 1, b: 2 };
let mut h: Holder = <Holder>::default();
(unsafe { Holder::operator_assign_1(&mut h, &mut p) });
assert!(((h.p.a) == (1)) && ((h.p.b) == (2)));
assert!(((unsafe { sum_1(&p,) }) == (3)));
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
Loading